39 views

Uploaded by saimanobhiram

Sorting techniues

save

- Ge8161 Python Programming Lab Record
- Comparison and Enhancement of Sorting Algorithms
- External Memory Algorithms and Data Structures
- comp1927 .docx
- 2TP
- Tancet cse
- Status Messages
- 36778524 Data Structures and Algorithms
- Arry
- Divide and Conquer-3
- Lecture-39 Internal Sorting Techniques
- ALGO- Lect9.pdf
- Time Series Prdiction
- Ppsc Paper It
- Unit 2 - Analysis of Algorithm
- PS1
- Quiz1F18_BasicProgrammingSolution.pdf
- snehal
- JAVAINDEX_1 (1)
- Decision Science
- Yahoo Inc Placement Process Details
- Linked-List-C
- 6 Queues
- IEEE-RWEP Error Correction Cod Bkgrd-Lect
- Characterstics of Static Relay Undervoltageovervoltage (1)
- grey orange
- Quick Sort Explanation
- Con Currency Control
- MCQs
- CS F342 ComputerArchitecture Lab5
- CS_IS_F213_L3_25May
- Security Analysis and Portfolio Management
- CS_IS_F213_L1_22May
- Cs f213 is f213 Handout
- A Study on Technical Analysis of Stocks Listed in Nse With Reference to Pharmaceutical Industries-2-3
- CS_IS_F213_L2_23May
- DSP Carlos Arredondo
- Devoir SR OmarCheikhrouhou 0910
- IJIEEB-V5-N1-8
- criptografia
- Taller Controladores Optimos
- Data Structures PPT
- 12
- DMDS_U3_A2_ALCA.docx
- A Fuzzy Goal Programming Approach for Vendor Selection
- Finite Element Analysis
- Cours C2SI.pdf
- MCT Book - Chapter 4
- pkcs-8
- criptografia
- AI Assignment
- 7 Conjuntos First e Follow
- Credibility Theory
- 9-Jan2015
- 05591634
- Crypto Appliquee 2009
- Simulation Optimization Using Balanced Explorative and Exploitative Search
- 2b Busqueda Informada y Exploracion (Es)(1)
- lab4_EG
- inequalities
- Automatic Control Systems by Benjamin C. Kuo
- An Introduction to SimEvents
- Algorithms in Everyday Mathematics
- Chapter 4 Discrete Fourier Transform
- Meshfree Shape Function From Moving Least Square
- Svm Tricia

You are on page 1of 8

Quick Sort

Quick sort was discovered by Tony Hoare in 1960. In general it has much better performance than brute force

sorting methods like bubble sort or selection sort - O(n log2 n) versus O(n2) . It works by first of all by

partitioning the array around a pivot value and then dealing with the 2 smaller partitions separately.

Partitioning is the most complex part of quick sort. The simplest thing is to use the first value in the array, a[l]

(or a[0] as l = 0 to begin with) as the pivot. After the partitioning, all values to the left of the pivot are <= pivot

and all values to the right are > pivot.

For example, consider

k

a[k]

l

0

8

1

2

2

5

3

13

4

4

5 6

19 12

7

6

8

3

r

9 10 11 12

11 10 7 9

where the value of a[l], namely 8, is chosen as pivot. Then the partition function moves along the array from

the lhs until it finds a value > pivot. Next it moves from the rhs, passing values > pivot and stops when it finds

a value <= pivot. This is done by the following piece of code:

i = l; j = r+1;

do ++i;

while( a[i] <= pivot && i <= r );

do --j;

while( a[j] > pivot );

Then if the lhs value is to the left of the rhs value, they are swapped. Variable i keeps track of the current

position on moving from left to right and j does the same for moving from right to left. You then get

a[k]

l

8

2

5

i

13

5

i

7

4

19 12

3

11 10

j

7

r

9

6

3

j

11 10 13

r

9

6

j

3

11 10 13

r

9

6

**a[i] and a[j] are swapped to give
**

a[k]

l

8

2

4

19 12

4

i

19 12

4

i

3

**This process is repeated with i and j to get
**

a[k]

l

8

2

5

7

**and do a swap to get
**

l

a[k] 8

2

5

7

12 6

1

j

r

19 11 10 13 9

0. The pivot is left alone as it is in the correct position. 7. At this stage the partition code breaks out of the main while loop. The lhs array is similarly partitioned from l 6 r 2 5 7 i 7 i 3 l 6 2 5 l 6 2 5 l 6 2 5 3 4 5 3 3 rj 4 4 j 2 3 rj j l 4 4 6 7 i r 7 i r 7 Next do quickSort( a. 5) – no more partition in rhs subarray. It is a waste of computation power to partition an array of size 2 and call quickSort twice more. 2 . All a[k] <= pivot where k <= j. 0. l. 3) quickSort( a. 5) quickSort( a. 5. j-1) or quickSort( a.Quick sort explained Move i and j again to get a[k] l 8 a[k] l 8 a[k] l 8 2 5 7 4 3 i 12 j 6 19 11 10 13 r 9 3 i 6 j 12 19 11 10 13 r 9 3 j 6 i 12 19 11 10 13 r 9 and swap again 2 5 7 4 Move i and j again. So quickSort divides the problem into two smaller ones and recursively calls itself on each of then as in: quickSort( a. 2 5 7 4 When j passes i. 12) The smaller arrays are again partitioned in the same way as described above and so on. j+1. All that we do next is swap the pivot with a[j] to get j 8 l a[k] 6 2 5 7 4 3 i 12 19 11 10 13 r 9 Then quickSort is called again separately on the two smaller arrays (here a[0] to a[5] and a[7] to a[12]). Eventually quickSort will arrive at arrays of size 1 or 2 or 0. r) or quickSort( a. Arrays of size 0 or 1 are already so to say sorted. while an array of size 2 can be sorted with an if statement. the partitioning is finished.

0. 0) – no more partition in lhs subarray. 12)is tackled which partitions subarray as follows: 13 19 13 19 All the partitioning in now over with the result that in the meantime the array has been sorted. The lhs subarray is again partitioned and a swap done 3 2 2 3 Next do quickSort( a. 7. 3) is tackled which partitions subarray as follows: 4 4 4 3 2 2 2 2 5 5 3 4 3 3 5 5 Next do quickSort( a. 0. 12). 0. Number of computations: I counted: comparisons = 40 swaps = 13 3 . 9)is tackled which partitions subarray as follows: 10 9 11 9 10 11 No further partitioning occurs on these two subarrays. 11. 1) quickSort( a. 7. Next quickSort( a.Quick sort explained Next quickSort( a. 11. 3. 3) – no more partition in rhs subarray. 7. Next quickSort( a. 12) which partitions subarray as follows: 12 19 11 10 13 9 12 19 11 10 13 9 12 9 11 10 13 19 10 9 11 12 13 19 and calls quickSort( a. 9) and quickSort( a. Next we return to the original lhs subarray from the first partition quickSort( a.

4 .Quick sort explained Overview 2 8 2 5 4 3 6 2 5 7 4 2 5 3 3 2 13 19 12 6 3 8 6 4 4 7 10 5 11 12 19 9 11 9 10 10 11 7 9 10 13 12 13 11 13 9 19 19 3 2 3 4 5 6 7 8 9 10 11 12 13 19 Note: Quick sort works its way down through partitions. creating smaller ones all the time before it deals with all partitions on a given level. This is referred to as a depth-first approach.

int. 9}. printf("\n\nUnsorted array is: for(i = 0. j+1. 0. "). i < 9. 0. 1. } void quickSort( int a[].h> void quickSort( int[]. 12. 8).c // quickSort. int). ++i) printf(" %d ". int). if( l < r ) { // divide and conquer j = partition( a. a[i]).Quick sort explained quickSort. "). printf("\n\nSorted array is: for(i = 0. 15. a[i]). int. } } 5 . int r) { int j. ++i) printf(" %d ". int i. void main() { int a[] = { 7. quickSort( a. -2. 4. int partition( int[].cpp #include <stdio. r). l. quickSort( a. i < 9. r). quickSort( a. 11. int l. j-1). l.

return j. a[j] = temp. pivot = a[l]. do --j. } temp = a[l]. a[l] = a[j]). temp = a[i]. i = l. } 6 . int l. i. while( a[j] > pivot ). if( i >= j ) break.Quick sort explained int partition( int a[]. a[j] = temp. while( 1) { do ++i. a[i] = a[j]). temp. int r) { int pivot. j. j = r+1. while( a[i] <= pivot && i <= r ).

9}. 0. } t = a[l]. 15. j = r+1. i < 9. i = l. quickSort( a. t. 12. 11. r). quickSort( a. a[i]). do --j. int i. t = a[i]. } } 7 .h> void quickSort( int[]. "). j-1). void main() { int a[] = { 7. "). 4. printf("\n\nSorted array is: for(i = 0. i < 9. a[l] = a[j]. quickSort( a. int. int r) { int pivot. j+1. ++i) printf(" %d ". while( 1) { do ++i. } void quickSort( int a[]. while( a[i] <= pivot && i <= r ). a[j] = t. int l. a[i]). if( i >= j ) break.Quick sort explained Version from Sedgewick // quickSort_Sedgew. j. a[i] = a[j]. 0. 1. l. while( a[j] > pivot ). -2. if( l < r ) { pivot = a[l]. printf("\n\nUnsorted array is: for(i = 0. int). i. 8).cpp #include <stdio. a[j] = t. ++i) printf(" %d ".

quickSort( a. r). a[j] = t. } return. j+1.Quick sort explained quickSort_Ver2. a[l] = a[j]. // array of size 2. // sub array of size 1 or 0 . l. a[l] = a[r]. while( a[i] <= pivot && i <= r ). } pivot = a[l]. int l. i. int r) { int pivot.cpp This version is more efficient when dealing with small subarrays. sort directly with if if( l+1 == r) { if(a[l] > a[r]) { t = a[l]. // quickSort_Ver2. t. quickSort( a.cpp // more efficient near end of sorting // main() etc left out void quickSort( int a[]. t = a[i]. j = r+1. if( i >= j ) break. j-1). while( a[j] > pivot ). } 8 . do --j.already sorted if(l >= r ) return. i = l. a[j] = t. while( 1) { do ++i. j. a[i] = a[j]. a[r] = t. } t = a[l].

- Ge8161 Python Programming Lab RecordUploaded bythanammal indu
- Comparison and Enhancement of Sorting AlgorithmsUploaded byEditor IJRITCC
- External Memory Algorithms and Data StructuresUploaded bygafoorkadost
- comp1927 .docxUploaded byvem
- 2TPUploaded byUtsyo Chakraborty
- Tancet cseUploaded byJulie Soosai
- Status MessagesUploaded byPratheev S
- 36778524 Data Structures and AlgorithmsUploaded byPriya Balakrishnan
- ArryUploaded bychaitu215
- Divide and Conquer-3Uploaded bymdhuq1
- Lecture-39 Internal Sorting TechniquesUploaded byTaqi Shah
- ALGO- Lect9.pdfUploaded bymahfooz3
- Time Series PrdictionUploaded byJit Saha
- Ppsc Paper ItUploaded bytanishgooel
- Unit 2 - Analysis of AlgorithmUploaded byVaibhav Makkar
- PS1Uploaded byLight Stark
- Quiz1F18_BasicProgrammingSolution.pdfUploaded byMayank Agrawal
- snehalUploaded byKaran Patel
- JAVAINDEX_1 (1)Uploaded bySudeep Sharma
- Decision ScienceUploaded byAbhiyash Jain
- Yahoo Inc Placement Process DetailsUploaded byKalyan Sagar

- Linked-List-CUploaded byrajarathinam82
- 6 QueuesUploaded bysaimanobhiram
- IEEE-RWEP Error Correction Cod Bkgrd-LectUploaded bysaimanobhiram
- Characterstics of Static Relay Undervoltageovervoltage (1)Uploaded bysaimanobhiram
- grey orangeUploaded bysaimanobhiram
- Quick Sort ExplanationUploaded bysaimanobhiram
- Con Currency ControlUploaded bysaimanobhiram
- MCQsUploaded bysaimanobhiram
- CS F342 ComputerArchitecture Lab5Uploaded bysaimanobhiram
- CS_IS_F213_L3_25MayUploaded bysaimanobhiram
- Security Analysis and Portfolio ManagementUploaded bythe_randomist
- CS_IS_F213_L1_22MayUploaded bysaimanobhiram
- Cs f213 is f213 HandoutUploaded bysaimanobhiram
- A Study on Technical Analysis of Stocks Listed in Nse With Reference to Pharmaceutical Industries-2-3Uploaded bysaimanobhiram
- CS_IS_F213_L2_23MayUploaded bysaimanobhiram

- DSP Carlos ArredondoUploaded byIxbr0kenxI
- Devoir SR OmarCheikhrouhou 0910Uploaded byAhmed Bendahmane
- IJIEEB-V5-N1-8Uploaded bysaeed
- criptografiaUploaded byPERUHACKING
- Taller Controladores OptimosUploaded byPAOLA ANACONA
- Data Structures PPTUploaded byGuru Samy
- 12Uploaded byapi-240724606
- DMDS_U3_A2_ALCA.docxUploaded byAlfredo De Jesus Cordova Abarca
- A Fuzzy Goal Programming Approach for Vendor SelectionUploaded byHakan Pabuçcu
- Finite Element AnalysisUploaded bySathish Kumar
- Cours C2SI.pdfUploaded byKhalil Bentayeb
- MCT Book - Chapter 4Uploaded byAmmar Naveed Bajwa
- pkcs-8Uploaded byKranthi Reddy
- criptografiaUploaded byjazmin
- AI AssignmentUploaded bykallady
- 7 Conjuntos First e FollowUploaded byClearlison Costa
- Credibility TheoryUploaded byadau
- 9-Jan2015Uploaded bysasddasafsfafaf
- 05591634Uploaded byRoshan Jayswal
- Crypto Appliquee 2009Uploaded bySuley Paterson
- Simulation Optimization Using Balanced Explorative and Exploitative SearchUploaded byravg10
- 2b Busqueda Informada y Exploracion (Es)(1)Uploaded byRudolf Felipez
- lab4_EGUploaded bydeui
- inequalitiesUploaded byapi-246010442
- Automatic Control Systems by Benjamin C. KuoUploaded byMuhammadFahadIjaz
- An Introduction to SimEventsUploaded byAryo Daniel
- Algorithms in Everyday MathematicsUploaded byTFT
- Chapter 4 Discrete Fourier TransformUploaded byMuhd Hafizuddin
- Meshfree Shape Function From Moving Least SquareUploaded byJeetender Singh Kushawaha
- Svm TriciaUploaded byconikeec