1 / 10

Quick Sort Array-Based Lists

- The quick sort algorithm uses the

divide-and-conquer technique to sort a list - The list is partitioned into two sublists, and

the two sublists are then sorted and combined

into one list in such a way that the combined

list is sorted

Quick Sort Array-Based Lists

- The general algorithm is
- if (list size is greater than 1)
- 1. Partition the list into two sublists, say

lowerSublist and upperSublist. - 2. Quick sort lowerSublist.
- 3. Quick sort upperSublist.
- 4. Combine the sorted lowerSublist and sorted

upperSublist.

Program 7.1 (modified)

- template ltclass Item, class Item2, class Item3gt
- void TableltItem, Item2, Item3gtQuickSort ()
- DoQuickSort (0, used-1)
- template ltclass Item, class Item2, class Item3gt
- void TableltItem, Item2, Item3gtDoQuickSort(int

first, int last) - int splitPoint
- if (last gt first)
- splitPoint Split (first, last)
- DoQuickSort (first, splitPoint - 1)
- DoQuickSort (splitPoint 1, last)

Program 7.2

- template ltclass Item, class Item2, class Item3gt
- int TableltItem, Item2, Item3gt Partition (int

first, int last) - int right first 1
- int left last
- Item v datafirst
- Item2 k2
- do
- v.GetKey(k2)
- while ((right lt left) (dataright.CompareKe

ys(k2)! 1)) - right
- while ((right lt left) (dataleft.CompareKey

s(k2)1)) - left--
- if (right lt left)
- Swap(dataleft, dataright)

Analysis of Quick Sort Algorithm for List of

length n

Number of Comparisons Number of Swaps

Average Case (1.39)nlog2nO(n) O(nlog2n) (0.69)nlog2nO(n) O(nlog2n)

Worst Case n2/2 - n/2 O(n2) n2/2 3n/2 - 2 O(n2)

Merge Sort Linked List-Based

- Merge sort uses the divide-and-conquer technique

to sort a list - It partitions the list into two sublists, and

then combines the sorted sublists into one sorted

list - It partitions the list into nearly equal sizes
- For example, consider the list
- List 35 28 18 45 62 48 30 38
- Merge sort partitions this list into two sublists

as follows - first sublist 35 28 18 45
- second sublist 62 48 30 38

Merge sort algorithm

Program 8.3

- template ltclass Item, class Item2, class Item3gt
- void TableltItem, Item2, Item3gtMergeSort ()
- DoMergeSort (0, used-1)
- template ltclass Item, class Item2, class Item3gt
- void TableltItem, Item2, Item3gtDoMergeSort(int

first, int last) - if (last gt first)
- int m (last first ) /2
- DoMergeSort (first, m)
- DoMergeSort(m 1, last)
- Merge (first, m, last)

Program 8.2

- template ltclass Item, class Item2, class Item3gt
- int TableltItem, Item2, Item3gt Merge (int first,

int m, int last) - int i, j //need LCVs

after loop - Item aux new last-first1 //need a

temp array - //copy first half of data into aux, leaves i

at first - for (i m1 i gt first i--)
- auxi-1 datai-1
- //copy second half of data into aux, leaves j

at last - for (j m j lt last j)
- auxlastm-j dataj1
- //merge compare item in first half of aux

with item in second, // copy smallest

to data array - for (int k first klt last k)
- if (auxj lt auxi)
- datak auxj--

Quick Sort and Merge Sort Tradeoffs

- Quick Sort
- Benefit
- Easy to understand
- Disadvantage
- Worst case on ordered array is O(N2)
- Recursion overhead, but there are iterative

versions - Merge Sort
- Benefit
- O(NlogN) no matter what the input
- Faster than Quicksort in best case
- Disadvantage
- temporary array (there are improved versions, but

more complex and costly)