Download presentation
Presentation is loading. Please wait.
Published byLeslie Cook Modified over 8 years ago
1
quickSort Algorithm 1. If first < last then begin 2. Partition the elements in the subarray first..last so that the pivot value is in place (in position pivIndex) 3. Apply quickSort to the first subarray first..pivIndex-1 4. Apply quickSort to the second subarray pivIndex+1..last end The two stopping cases are: 1. (first = last) -- only one value in subarray so sorted! 2. (first > last) -- no values in subarray so sorted!
2
How do we Partition? 1. Define the pivot value as the contents of val[first] 2. Initialize up to first and uown to last 3. Repeat 4. Increment up until up selects the first element greater than the pivot value 5. Decrement down until it selects the first element less than or equal to the pivot value 6. if up < down exchange their values until up meets or passes down 7. Exchange val[first] and val[down] 8. Define pivIndex as down
3
quickSort Example 447523435512647733 012345678 firstlast
4
Has first exceeded last? 447523435512647733 firstlast 012345678
5
Has first exceeded last? 447523435512647733 firstlast NO! 012345678
6
Define the value in position first to be the pivot 447523435512647733 firstlast 012345678
7
Define the value in position first to be the pivot 447523435512647733 firstlast pivot 44 012345678
8
Define up to be first and down to be last 447523435512647733 firstlast pivot 44 012345678
9
Define up to be first and down to be last 447523435512647733 updown pivot 44 firstlast 012345678
10
Move up to the first value > pivot 447523435512647733 updown pivot 44 firstlast 012345678
11
Move up to the first value > pivot 447523435512647733 updown pivot 44 firstlast 012345678
12
Move down to the first value <= pivot 447523435512647733 updown pivot 44 firstlast 012345678
13
Move down to the first value <= pivot 447523435512647733 updown pivot 44 firstlast 012345678
14
Exchange these values 447523435512647733 updown pivot 44 firstlast 012345678
15
Exchange these values 443323435512647775 updown pivot 44 firstlast 012345678
16
Move up to the first value > pivot 443323435512647775 updown pivot 44 firstlast 012345678
17
Move up to the first value > pivot 443323435512647775 updown pivot 44 firstlast 012345678
18
Move down to the first value <= pivot 443323435512647775 updown pivot 44 firstlast 012345678
19
Move down to the first value <= pivot 443323435512647775 updown pivot 44 firstlast 012345678
20
Exchange them 443323431255647775 updown pivot 44 firstlast 012345678
21
Move up to the first value > pivot 443323431255647775 updown pivot 44 firstlast 012345678
22
Move up to the first value > pivot 443323431255647775 updown pivot 44 firstlast 012345678
23
Move down to the first value <= pivot 443323431255647775 updown pivot 44 firstlast 012345678
24
Move down to the first value <= pivot 443323431255647775 updown pivot 44 firstlast 012345678
25
up and down have passed each other, so exchange the pivot value and the value in down 443323431255647775 updown pivot 44 firstlast 012345678
26
up and down have passed each other, so exchange the pivot value and the value in down 443323431255647775 updown pivot 44 firstlast 012345678
27
up and down have passed each other, so exchange the pivot value and the value in down 123323434455647775 pivIndex down pivot 44 firstlast 012345678
28
Note that all values below pivIndex are <= pivot 123323434455647775 pivIndex pivot 44 firstlast 012345678
29
and all values above pivIndex are > pivot 123323434455647775 pivIndex pivot 44 firstlast 012345678
30
This gives us two new subarrays to Partition 123323434455647775 pivIndex pivot 44 first 1 last 2 first 2 last 1 012345678
31
quickSort Procedure Code public void quickSort ( int [ ] a, int first, int last ) { int pivIndex; if ( first < last ) { pivIndex = partition ( a, first, last ); quickSort ( a, first, pivIndex-1 ); quickSort ( a, pivIndex+1, last ); }
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.