4/24/2012 1 Top Down Solution Strategies Sesi - 11 1 IF-UTAMA The family of sorting methods Main sorting themes Comparison-based sorting Transposition sorting BubbleSort Insert and keep sorted Insertion sort Tree sort Divide and conquer QuickSort MergeSort Proxmap Sort RadixSort ShellSort Diminishing increment sorting Address- -based sorting Selection sort Heap sort Priority queue sorting 2 IF-UTAMA Mergesort: Algorithm void mergesort( Elem[] a, Elem[] temp, int Ieft, int right ) { int I, j, k, mid = (left+right)/2 if( left == right ) return mergesort( a, temp, left, mid); mergesort( a, temp, mid+1, right); // do the merge operation for( i = left; i <= mid; i++ ) temp[i] = a[i] for( j = 1; j <= right-mid; j++ ) temp[ right-j+1 ] = a[ j+mid ] // merge sublists back to array for( i=left, j=right, k=left; k<=right; k++ ) { if( temp[ i ] < temp[ j ] ) a[k] = temp[ i++ ] else a[k]=temp[ j-- ] } } 3 IF-UTAMA Mergesort: Illustration 85 24 63 45 17 31 96 50 4 IF-UTAMA Mergesort: Illustration 85 24 63 45 17 31 96 50 5 IF-UTAMA Mergesort: Illustration 85 24 63 45 17 31 96 50 6 IF-UTAMA
20
Embed
Top Down Solution Strategies - Kuliah bersama Dosen Stress ... · sort Heap sort Priority queue sorting ... Algoritma & Pemrograman II - sesi 11 Top Down Solution Strategies.pptx
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
4/24/2012
1
Top Down Solution
Strategies
Sesi - 11
1IF-UTAMA
The family of sorting methods
Main sorting themes
Comparison-basedsorting
Transpositionsorting
BubbleSort
Insert andkeep sorted
Insertionsort
Treesort
Divide andconquer
QuickSort MergeSort
ProxmapSort
RadixSort
ShellSort
Diminishingincrement
sorting
Address--basedsorting
Selectionsort
Heapsort
Priority queuesorting
2IF-UTAMA
Mergesort: Algorithmvoid mergesort( Elem[] a, Elem[] temp, int Ieft, int right )
– Each merge operation takes 0(k) time for 2 lists each k/2 elements long (merged into one list k elements long)
– There will be log2n levels
• 1st level: 2 n/2 long lists to be merged into 1 n long list
• 2nd level: 4 n/4 long lists to be merged into 2 n/2 long lists
• 3rd level: 8 n/8 long lists to be merged into 4 n/4 long lists
• …
– Time Complexity: O(nlog2n)
72IF-UTAMA
4/24/2012
13
73
Complexity of MergeSortPass
Number
Number of merges
Merge list length
# of comps / moves per
merge
1 2k-1 or n/2 1 or n/2k ≤≤≤≤ 21
2 2k-2 or n/4 2 or n/2k-1 ≤≤≤≤ 22
3 2k-3 or n/8 4 or n/2k-2 ≤≤≤≤ 23
. . . .
. . . .
. . . .
k – 1 21 or n/2k-1 2k-2 or n/4 ≤≤≤≤ 2k-1
k 20 or n/2k 2k-1 or n/2 ≤≤≤≤ 2k
k = log n
IF-UTAMA 74
Multiplying the number of merges by the maximum
number of comparisons per merge, we get:
(2k-1)21 = 2k
(2k-2)22 = 2k
•
•
•
(21)2k-1 = 2k
(20)2k = 2k
Complexity of MergeSort
k passes each require 2k comparisons (and moves). But k = lg n
and hence, we get lg(n) • n comparisons or O(n lgn)
IF-UTAMA
Shell Sort
� more efficient than Bubble, Insertion and Selection sorts
� compares distant items first and works way down to nearby items
� interval is called the gap
� in a simple implementation, gap begins at one-half the length of the list and is successively halved until each item is compared with neighbor
75IF-UTAMA
Shell Sort
� A variation of the insertion sort
– But faster than O(n2)
� Done by sorting subarrays of equally spaced indices
� Instead of moving to an adjacent location an element moves several locations away
– Results in an almost sorted array
– This array sorted efficiently with ordinary insertion sort
76IF-UTAMA
Shellsort// Modified version of Insertion Sorttemplate <class Elem>void inssort2(Elem A[], int n, int incr) {for (int i=incr; i<n; i+=incr)for (int j=i;
(j>=incr) &&(A[j] < A[j-incr]); j-=incr)
swap(A, j, j-incr);}
template <class Elem>void shellsort(Elem A[], int n) { // Shellsortfor (int i=n/2; i>2; i/=2) // For each incr
for (int j=0; j<i; j++) // Sort sublists
inssort2<Elem>(&A[j], n-j, i);inssort2<Elem>(A, n, 1);
}
77IF-UTAMA
Shellsort
78IF-UTAMA
4/24/2012
14
Another Shell Sort Example
� Consider sorting the following list by Shell
sort:
4523
4
96
7
918
12
24
9
24
45
91
6
8
23
4
7
12
96
4
248
7
4523
12
91
4
8
9
12
45
6
7
23
24
91
46
8
79
23
1224
45
91
46
7
89
12
2324
45
91
GAP=3 GAP=2 GAP=1
79IF-UTAMA
Quicksort PseudocodeQuicksort(L, low, high)
/* L = array, low = left-end, high = right-end */
{
if ( low < high)
{
pivotindex = split(L, low, high) // pivot in right place/* split is the workhorse;
to its left is everything < pivot,
to its right is everything >= pivot */
Quicksort(L, low, pivotindex – 1); // sort left sub-array
Quicksort(L, pivotindex + 1, high); // sort right sub-array
}
}
80IF-UTAMA
Quicksort Algorithm
Given an array of n elements (e.g., integers):
� If array only contains one element, return
� Else
– pick one element to use as pivot.
– Partition elements into two sub-arrays:
• Elements less than or equal to pivot
• Elements greater than pivot
– Quicksort two sub-arrays
– Return results
81IF-UTAMA
Example
We are given array of n integers to sort:
40 20 10 80 60 50 7 30 100
82IF-UTAMA
Pick Pivot Element
There are a number of ways to pick the pivot element. In this example, we will use the first element in the array:
40 20 10 80 60 50 7 30 100
83IF-UTAMA
Partitioning Array
Given a pivot, partition the elements of the array such that the resulting array consists of:
1. One sub-array that contains elements >= pivot
2. Another sub-array that contains elements < pivot
The sub-arrays are stored in the original data array.
Partitioning loops through, swapping elements below/above pivot.
84IF-UTAMA
4/24/2012
15
40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
85IF-UTAMA
40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
86IF-UTAMA
40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
87IF-UTAMA
40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
88IF-UTAMA
40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
89IF-UTAMA
40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
90IF-UTAMA
4/24/2012
16
40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
91IF-UTAMA
40 20 10 30 60 50 7 80 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
92IF-UTAMA
40 20 10 30 60 50 7 80 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
93IF-UTAMA
40 20 10 30 60 50 7 80 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
94IF-UTAMA
40 20 10 30 60 50 7 80 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
95IF-UTAMA
40 20 10 30 60 50 7 80 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
96IF-UTAMA
4/24/2012
17
40 20 10 30 60 50 7 80 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
97IF-UTAMA
40 20 10 30 60 50 7 80 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
98IF-UTAMA
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 7 50 60 80 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
99IF-UTAMA
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 7 50 60 80 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
100IF-UTAMA
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 7 50 60 80 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
101IF-UTAMA
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 7 50 60 80 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
102IF-UTAMA
4/24/2012
18
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 7 50 60 80 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
103IF-UTAMA
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 7 50 60 80 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
104IF-UTAMA
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 7 50 60 80 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
105IF-UTAMA
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 7 50 60 80 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
106IF-UTAMA
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 7 50 60 80 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
107IF-UTAMA
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
5. Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
108IF-UTAMA
4/24/2012
19
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
5. Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
109IF-UTAMA
Partition Result
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
<= data[pivot] > data[pivot]
110IF-UTAMA
Recursion: Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
<= data[pivot] > data[pivot]
111IF-UTAMA
Quicksort behaves poorly when sub-arrays are not “balanced” in size
9 8 7 6 5 4 3 2 1
1 8 7 6 5 4 3 2 9 pi = 8
1 8 7 6 5 4 3 2 9 pi = 0
1 2 7 6 5 4 3 8 9 pi = 7
1 2 7 6 5 4 3 8 9 pi = 1
1 2 3 6 5 4 7 8 9 pi = 6
1 2 3 6 5 4 7 8 9 pi = 2
1 2 3 4 5 6 7 8 9 pi = 5
1 2 3 4 5 6 7 8 9 pi = 3112IF-UTAMA
Quicksort
template <class Elem>void qsort(Elem A[], int i, int j) {if (j <= i) return; // List too smallint pivotindex = findpivot(A, i, j);swap(A, pivotindex, j); // Put pivot at end// k will be first position on right sideint k =
partition<Elem>(A, i-1, j, A[j]);swap(A, k, j); // Put pivot in placeqsort<Elem>(A, i, k-1);qsort<Elem>(A, k+1, j);
}
template <class Elem>int findpivot(Elem A[], int i, int j){ return (i+j)/2; }
113IF-UTAMA
Quicksort Partition
template <class Elem, class Comp>int partition(Elem A[], int l, int r,
Elem& pivot) {do { // Move the bounds in until they meetwhile (Comp::lt(A[++l], pivot));while ((r != 0) && Comp::gt(A[--r],
pivot));swap(A, l, r); // Swap out-of-place
values} while (l < r); // Stop when they crossswap(A, l, r); // Reverse last swapreturn l; // Return first pos on right