Title: Sorting: Advanced Techniques
1Sorting Advanced Techniques
2Outline
- Divide-and-conquer sorting algorithms
- Mergesort
- Quicksort
- For each algorithm
- Idea
- Example
- Implementation
- Running time for each algorithm
3Mergesort Basic Idea
- Divide and Conquer approach
- Idea
- Merging two sorted array takes O(n) time
- Split an array into two takes O(1) time
4Mergesort Merge Implementation
- Implement operation to merge two sorted arrays
into one sorted array - public static void merge(int A, int B, int
C) -
Assume A and B are sorted and C A B
5Mergesort Algorithm
- If the number of items to sort is 0 or 1, return.
- Recursively sort the first and second half
separately. - Merge the two sorted halves into a sorted group.
6Mergesort Example
split
7Mergesort Example
split
merge
8Mergesort Example
merge
9Mergesort Implementation
- MergeSort implementation (and driver method)
- void mergeSort(int array)
- mergeSort(array, 0, a.length-1)
- void mergeSort(int a, int left, int right)
-
- if(left lt right)
-
- int centre (left right)/2
- mergeSort(a, left, centre)
- mergeSort(a, center1, right)
- merge(a, left, center1, right)
-
How to merge the two subarrays of A without any
temporary space?
10Mergesort Merge Implementation
- Implement operation to merge two sorted
subarrays - public static void merge(int A, int l, int c,
int r) -
11Mergesort Analysis
- Running Time O(n log n)
- Why?
12Quicksort Basic Idea
- Divide and Conquer approach
- Quicksort(S) algorithm
- If the number of items in S is 0 or 1, return.
- Pick any element v ? S. This element is called
the pivot. - Partition S v into two disjoint groups
- L x ? S v x ? v and
- R x ? S v x ? v
- Return the result of Quicksort(L), followed by v,
followed by Quicksort(R).
13Quicksort Select Pivot
-1
58
4
2
42
3
43
40
0
1
3
65
14Quicksort Partition
65
2
42
0
3
40
4
43
-1
58
3
1
15Quicksort Recursive Sort Merge
43
65
58
42
2
1
3
0
-1
3
4
16Quicksort Partition Algorithm 1
17Quicksort Partition Algorithm 1
18Quicksort Partition Algorithm 1
19Quicksort Partition Algorithm 2
move pivot out of the way
while gt pivot right--
while lt pivot left
20Quicksort Partition Algorithm 2
CROSSING
move pivot back
Quicksort recursively
Quicksort recursively
21Quicksort Implementation
- static void QuickSort(int a, int low, int high)
-
- if(high lt low) return // base case
- pivot choosePivot(a) // select best
pivot - int ilow, jhigh-1
- swap(a,pivot,aj) // move pivot out of
the way - while(i lt j)
-
- // find large element starting from left
- while(ilthigh ailtpivot) i
- // find small element starting from right
- while(jgtlow ajgtpivot) j--
- // if the indexes have not crossed, swap
- if(igtj) swap(a, i, j)
-
- swap(a,i,high-1) // restore pivot
22Quicksort Analysis
- Partitioning takes
- O(n)
- Merging takes
- O(1)
- So, for each recursive call, the algorithm takes
O(n) - How many recursive calls does a quick sort need?
23Quicksort Choosing The Pivot
- Ideal pivot
- Median element
- Common pivot
- First element
- Element at the middle
- Median of three
24Further Reading
- http//telaga.cs.ui.ac.id/WebKuliah/IKI10100/resou
rces/animation/ - Chapter 8 Sorting Algorithm