Upload
trantram
View
219
Download
0
Embed Size (px)
Citation preview
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 )
{
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-- ]
}
}
3IF-UTAMA
Mergesort: Illustration
85 24 63 45 17 31 96 50
4IF-UTAMA
Mergesort: Illustration
85 24 63 45 17 31 96 50
5IF-UTAMA
Mergesort: Illustration
85 24 63 45
17 31 96 50
6IF-UTAMA
4/24/2012
2
Mergesort: Illustration
63 45
85 24
17 31 96 50
7IF-UTAMA
Mergesort: Illustration
63 45
85 24
17 31 96 50
8IF-UTAMA
Mergesort: Illustration
24 63 45
85
17 31 96 50
9IF-UTAMA
Mergesort: Illustration
24 63 45
85
17 31 96 50
10IF-UTAMA
Mergesort: Illustration
24 85 63 45
17 31 96 50
11IF-UTAMA
Mergesort: Illustration
24 85 63 45
17 31 96 50
12IF-UTAMA
4/24/2012
3
Mergesort: Illustration
24 85 63 45
17 31 96 50
13IF-UTAMA
Mergesort: Illustration
24 85
63 45
17 31 96 50
14IF-UTAMA
Mergesort: Illustration
24 85
63 45
17 31 96 50
15IF-UTAMA
Mergesort: Illustration
24 85 45
63
17 31 96 50
16IF-UTAMA
Mergesort: Illustration
24 85 45
63
17 31 96 50
17IF-UTAMA
Mergesort: Illustration
24 85 45 63
17 31 96 50
18IF-UTAMA
4/24/2012
4
Mergesort: Illustration
24 85 45 63
17 31 96 50
19IF-UTAMA
Mergesort: Illustration
24 85 45 63
17 31 96 50
20IF-UTAMA
Mergesort: Illustration
24 85 45 63
17 31 96 50
21IF-UTAMA
Mergesort: Illustration
24
85 45 63
17 31 96 50
22IF-UTAMA
Mergesort: Illustration
24
85 45 63
17 31 96 50
23IF-UTAMA
Mergesort: Illustration
24 45
85 63
17 31 96 50
24IF-UTAMA
4/24/2012
5
Mergesort: Illustration
24 45
85 63
17 31 96 50
25IF-UTAMA
Mergesort: Illustration
24 45 63
85
17 31 96 50
26IF-UTAMA
Mergesort: Illustration
24 45 63
85
17 31 96 50
27IF-UTAMA
Mergesort: Illustration
24 45 63 85 17 31 96 50
28IF-UTAMA
Mergesort: Illustration
24 45 63 85 17 31 96 50
29IF-UTAMA
Mergesort: Illustration
24 45 63 85 17 31 96 50
30IF-UTAMA
4/24/2012
6
Mergesort: Illustration
24 45 63 85
17 31 96 50
31IF-UTAMA
Mergesort: Illustration
24 45 63 85
96 50
17 31
32IF-UTAMA
Mergesort: Illustration
24 45 63 85
96 50
17 31
33IF-UTAMA
Mergesort: Illustration
24 45 63 85
17 96 50
31
34IF-UTAMA
Mergesort: Illustration
24 45 63 85
17 96 50
31
35IF-UTAMA
Mergesort: Illustration
24 45 63 85
17 31 96 50
36IF-UTAMA
4/24/2012
7
Mergesort: Illustration
24 45 63 85
17 31 96 50
37IF-UTAMA
Mergesort: Illustration
24 45 63 85
17 31 96 50
38IF-UTAMA
Mergesort: Illustration
24 45 63 85
17 31
96 50
39IF-UTAMA
Mergesort: Illustration
24 45 63 85
17 31
96 50
40IF-UTAMA
Mergesort: Illustration
24 45 63 85
17 31 50
96
41IF-UTAMA
Mergesort: Illustration
24 45 63 85
17 31 50
96
42IF-UTAMA
4/24/2012
8
Mergesort: Illustration
24 45 63 85
17 31 50 96
43IF-UTAMA
Mergesort: Illustration
24 45 63 85
17 31 50 96
44IF-UTAMA
Mergesort: Illustration
24 45 63 85
17 31 50 96
45IF-UTAMA
Mergesort: Illustration
24 45 63 85
17 31 50 96
46IF-UTAMA
Mergesort: Illustration
24 45 63 85 17
31 50 96
47IF-UTAMA
Mergesort: Illustration
24 45 63 85 17
31 50 96
48IF-UTAMA
4/24/2012
9
Mergesort: Illustration
24 45 63 85 17 31
50 96
49IF-UTAMA
Mergesort: Illustration
24 45 63 85 17 31
50 96
50IF-UTAMA
Mergesort: Illustration
24 45 63 85 17 31 50 96
51IF-UTAMA
Mergesort: Illustration
24 45 63 85 17 31 50 96
52IF-UTAMA
Mergesort: Illustration
24 45 63 85 17 31 50 96
53IF-UTAMA
Mergesort: Illustration
24 45 63 85 17 31 50 96
54IF-UTAMA
4/24/2012
10
Mergesort: Illustration
17
24 45 63 85 31 50 96
55IF-UTAMA
Mergesort: Illustration
17
24 45 63 85 31 50 96
56IF-UTAMA
Mergesort: Illustration
17 24
45 63 85 31 50 96
57IF-UTAMA
Mergesort: Illustration
17 24
45 63 85 31 50 96
58IF-UTAMA
Mergesort: Illustration
17 24 31
45 63 85 50 96
59IF-UTAMA
Mergesort: Illustration
17 24 31
45 63 85 50 96
60IF-UTAMA
4/24/2012
11
Mergesort: Illustration
17 24 31 45
63 85 50 96
61IF-UTAMA
Mergesort: Illustration
17 24 31 45
63 85 50 96
62IF-UTAMA
Mergesort: Illustration
17 24 31 45 50
63 85 96
63IF-UTAMA
Mergesort: Illustration
17 24 31 45 50
63 85 96
64IF-UTAMA
Mergesort: Illustration
17 24 31 45 50 63
85 96
65IF-UTAMA
Mergesort: Illustration
17 24 31 45 50 63
85 96
66IF-UTAMA
4/24/2012
12
Mergesort: Illustration
17 24 31 45 50 63 85
96
67IF-UTAMA
Mergesort: Illustration
17 24 31 45 50 63 85
96
68IF-UTAMA
Mergesort: Illustration
17 24 31 45 50 63 85 96
69IF-UTAMA
Mergesort: Illustration
17 24 31 45 50 63 85 96
70IF-UTAMA
Mergesort: Illustration
17 24 31 45 50 63 85 96
71IF-UTAMA
Mergesort: Time complexity
� Best, worst, average-case
– 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
}
The cost for partition is Θ(n).
114IF-UTAMA
4/24/2012
20
Referensi
1. http://curry.ateneo.net/~jpv/DS/DSJul04/MergeSort.ppt
2. http://www.cs.nyu.edu/courses/fall09/V22.0102-002/lectures/mergesort-102-fa09.ppt
3. http://cset.sp.utoledo.edu/cset3150/cset3150_shell_sort.ppt
4. http://courses.cs.vt.edu/~cs2604/fall05/mcpherson/note/InternalSorting.ppt
5. http://www.cs.bu.edu/fac/gkollios/cs113/Slides/quicksort.ppt
115IF-UTAMA