Quick Sort Algorithm Essay

1228 Words Oct 2nd, 2014 5 Pages
7.6.5

Quick Sort

Implementation
Next, recall that our goal is to partition all remaining elements based on whether they are smaller than or greater than the pivot We will find two entries:
– One larger than the pivot (staring from the front) – One smaller than the pivot (starting from the back)

which are out of order and then correct the ordering
– I.e., swap them

1

7.6.5

Quick Sort

Implementation
Continue doing so until the appropriate entries you find are actually in order The index to the larger entry we found would be the first large entry in the list (as seen from the left) Therefore, we could move this entry into the last entry of the list We can fill this spot with the pivot

2

7.6.5

Quick Sort
…show more content…
in order with respect to each other

12

7.6.5

Quick Sort

Quick Sort Example
We search forward until we find 68 > 57 We search backward until we find 9 < 57

13

7.6.5

Quick Sort

Quick Sort Example
We swap 68 and 9

14

7.6.5

Quick Sort

Quick Sort Example
We search forward until we find 76 > 57 We search backward until we find 9 < 57
– The indices are out of order, so we stop

15

7.6.5

Quick Sort

Quick Sort Example
We move the larger indexed item to the vacancy at the end of the array We fill the empty location with the pivot, 57 The pivot is now in the correct location

16

7.6.5

Quick Sort

Quick Sort Example
We will now recursively call quick sort on the first half of the list When we are finished, all entries < 57 will be sorted

17

7.6.5

Quick Sort

Quick Sort Example
We examine the first, middle, and last elements of this sub list

18

7.6.5

Quick Sort

Quick Sort Example
We choose 24 to be our pivot We move 9 into the first location in this sub-list

19

7.6.5

Quick Sort

Quick Sort Example
We search forward until we find 49 > 24 We search backward until we find 21 < 24

20

7.6.5

Quick Sort

Quick Sort Example
We swap 49 and 21, placing them in order with respect to eachother

21

7.6.5

Quick Sort

Quick Sort Example
We search forward until we find 38 > 24 We search backward until we find 16 < 24 The indices are reversed, so we stop

22

7.6.5

Related Documents