# The quick sort algorithm uses the divide-and-conquer technique to sort a list - PowerPoint PPT Presentation

1 / 10
Title:

## The quick sort algorithm uses the divide-and-conquer technique to sort a list

Description:

### 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 ... – PowerPoint PPT presentation

Number of Views:211
Avg rating:3.0/5.0
Slides: 11
Provided by: Sylv58
Category:
Tags:
Transcript and Presenter's Notes

Title: The quick sort algorithm uses the divide-and-conquer technique to sort a list

1
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

2
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.

3
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)

4
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)

5
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)
6
• 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

7
Merge sort algorithm
8
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)

9
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--

10
Quick Sort and Merge Sort Tradeoffs
• Quick Sort
• Benefit
• Easy to understand