Lect Sorting
Lect Sorting
Sorting
• In computer science, arranging in an ordered sequence is
called "sorting".
• Sorting is a common operation in many applications, and
efficient algorithms to perform it have been developed. •
The most common uses of sorted sequences are:
• making lookup or search efficient;
• making merging of sequences efficient.
• enable processing of data in a defined order.
Selection Sort
• Idea:
• Find the smallest element in the array
• Exchange it with the element in the first position • Find the
second smallest element and exchange it with the element
in the second position
• Continue until the array is sorted
n ← length[A]
846923114692381269438
for j ← 0 to n - 2
1239468 smallest ← j
1234968
1234689 c11
4 c2 n-1
1234689 c3 n-1
Selection Sort ∑ −
n
2
Analysis
Alg.: SELECTION-SORT(A)
for i ← j + 1 to n-1 A[smallest]
=
0 ∑ −
if A[i] < c4 c5 c6 n
2j jt
A[smallest] j j
=
t =0
smallest ← i 0 ∑ −
exchange A[j] ↔ 2j tj
c7 n-1
tj: # of times the inner for loop statement is executed at iteration j
5
= T n c c n c n c t c t c t c nn
+ −−
= 1 + 2 − + 3 − + 4∑
+
∑ ∑
+
n
−
2
n
−
2
2
( ) ( 1) ( 1) ( 1) 7 5
j
6
j =
j 0
0 j
j
j
=0
Best/Worst/Average Case
Analysis n n T n c c n c n c
( 1)
−
( ) ( 1) ( 1) 1 2 3 4 5 6 +7 −− = + − + − + cnn n
nn
( 1)
−
( 1)
+
c
+
c
2
( 1)
2
2 ( 1)2
= an +bn+c nn
2 j
n
−
∑
=−
T(n) = Θ(n2) j
2
=
0
Disadvantage:
Θ(n2) running time in all cases
6
n
−
2
n
−
2
n
−
2
− + 4∑
+
∑ +∑ +−
T n c c n c n c t c t c t c n ( ) ( 1) ( 1) ( 1) 7
=1 +2 −+3 0
j
5 =
0
j j
=
0
j = j 6 j
Bubble Sort
Given a list of N elements, repeat the following steps N-1 times:
• For each pair of adjacent numbers, if number on the left is greater than the
number on the right, swap them.
• “Bubble” the largest value to the end using pair-wise comparisons and swapping.
2214 17
6 12 148
148 148 17
12 22
6 128 128 14 17 22
7
8 12 17
6 14 17 22
Bubble Sort c11
c2 n-1
Analysis ∑ −
n
2
BubbleSort(A)
1. n = Length[A];
2. for j = 0 to n-2
cost times
3. for i = 0 to A[i] > A[i+1] 6. A[i] =
n-j-2 4. if 5. temp = A[i] A[i+1] 7.
0 j
A[i+1] = temp
∑
=
− 0
c3 c4 c5 c6 c7
j t
n
2 ∑ −
n
j j t 2
= j
0 = j t
∑
0 j
−
∑
=
−
n
2
8. return A;
n
j t 2
c81
j jt 0
=
∑+ −
n
−
2
n
−
2
n
−
2
n
−
2
2
4
j
5
j
6
j
7
j
8
j
0
j
=
0
j
=
0
j
=
0
j
=
=0
0
− 2 2
n ( 1) = j
=
nn −
Tnccnc+ +
( 1)
−
nn
( 1)
−
nn
( 1)
−
nn
( ) ( 1) c c 1 2 3 4 5 6 7 82( 1)
nn
( 1)
=+−++ −
c
+
c
+
2 c
2
2
2
= an +bn+c a quadratic function of n
2
T(n) c c (n 1) c t c t c t c t c t c
123
4
j
5
j
6
j
7
j
8
j
0
j
=
0
j
=
0
j
=
0
j
=
0
=
=+−+
∑ j
+
∑ +∑ +∑ +∑ +
T(n) c c (n 1) c t c t c t c t c t c
123
4
j
5
j
6
j
7
j
8
j
0
j
=
0
j
=
0
j
=
0
j
=
0
=
Worst Case
Analysis (Modified) n
−
( 1) =2
nn −
Tnccnc+ +
( 1)
−
nn
( 1)
−
nn
( 1)
n n 12345678
( ) ( 1) c c 2( 1)
−
nn
( 1)
=+−++ −
c
+
c
+
2 c
2
2
2
= an +bn+c
2 a quadratic function of n
=+−+
∑ j
+
∑ +∑ +∑ +∑ +
T(n) c c (n 1) c t c t c t c t c t c
123
4
j
5
j
6
j
7
j
8
j
0
j
=
0
j
=
0
j
=
0
j
=
0
=
Insertion Sort
• Idea: like sorting a hand of playing cards
• Start with empty left hand and cards face down on the table. •
Remove one card at a time from the table, and insert it into the
correct position in the left hand
• compare it with each card already in the hand, from right to left •
The cards held in the left hand are sorted
• these cards were originally the top cards of the pile on the table
13
Insertion Sort
InsertionSort(A, n)
1. for i = 1 to n-1
2. key = A[i]
3. j = i – 1
4. while (j >= 0) and (A[j] > key)
5. A[j+1] = A[j]
6. j = j – 1
7. A[j+1] = key
Array index 0 1 2 3 4 5 6 7 8 9Value 2.78 7.42 0.56 1.12 1.17 0.32 6.21 4.42 3.14 7.71 14
Iteration 0: step 0.
Insertion Sort
InsertionSort(A, n)
1. for i = 1 to n-1
2. key = A[i]
3. j = i – 1
4. while (j >= 0) and (A[j] > key)
5. A[j+1] = A[j]
6. j = j – 1
7. A[j+1] = key
Array index 0 1 2 3 4 5 6 7 8 9Value 2.78 7.42 0.56 1.12 1.17 0.32 6.21 4.42 3.14 7.71 15
Iteration 1: step 0.
Insertion Sort
InsertionSort(A, n)
1. for i = 1 to n-1
2. key = A[i]
3. j = i – 1
4. while (j >= 0) and (A[j] > key)
5. A[j+1] = A[j]
6. j = j – 1
7. A[j+1] = key
Array index 0 1 2 3 4 5 6 7 8 9Value 2.78 7.42 0.56 1.12 1.17 0.32 6.21
4.42 3.14 7.71 0.56 7.42
16
Iteration 2: step 0.
Insertion Sort
InsertionSort(A, n)
1. for i = 1 to n-1
2. key = A[i]
3. j = i – 1
4. while (j >= 0) and (A[j] > key)
5. A[j+1] = A[j]
6. j = j – 1
7. A[j+1] = key
Array index 0 1 2 3 4 5 6 7 8 9Value 2.78 0.56 7.42 1.12 1.17 0.32 6.21 4.42
3.14 7.71 0.56 2.78
17
Iteration 2: step 1.
Insertion Sort
InsertionSort(A, n)
1. for i = 1 to n-1
2. key = A[i]
3. j = i – 1
4. while (j >= 0) and (A[j] > key)
5. A[j+1] = A[j]
6. j = j – 1
7. A[j+1] = key
Array index 0 1 2 3 4 5 6 7 8 9Value 2.78 0.56 7.42 1.12 1.17 0.32 6.21 4.42
3.14 7.71 0.56 2.78
18
Iteration 2: step 2.
Insertion Sort
InsertionSort(A, n)
1. for i = 1 to n-1
2. key = A[i]
3. j = i – 1
4. while (j >= 0) and (A[j] > key)
5. A[j+1] = A[j]
6. j = j – 1
7. A[j+1] = key
Array index 0 1 2 3 4 5 6 7 8 9Value 0.56 2.78 7.42 1.12 1.17 0.32 6.21
4.42 3.14 7.71 1.12 7.42
19
Iteration 3: step 0.
Insertion Sort
InsertionSort(A, n)
1. for i = 1 to n-1
2. key = A[i]
3. j = i – 1
4. while (j >= 0) and (A[j] > key)
5. A[j+1] = A[j]
6. j = j – 1
7. A[j+1] = key
Array index 0 1 2 3 4 5 6 7 8 9Value 0.56 2.78 1.12 7.42 1.17 0.32 6.21
4.42 3.14 7.71 1.12 2.78
20
Iteration 3: step 1.
Insertion Sort
InsertionSort(A, n)
1. for i = 1 to n-1
2. key = A[i]
3. j = i – 1
4. while (j >= 0) and (A[j] > key)
5. A[j+1] = A[j]
6. j = j – 1
7. A[j+1] = key
Array index 0 1 2 3 4 5 6 7 8 9Value 0.56 2.78 1.12 7.42 1.17 0.32 6.21
4.42 3.14 7.71 1.12 2.78
21
Iteration 3: step 2.
Insertion Sort
InsertionSort(A, n)
1. for i = 1 to n-1
2. key = A[i]
3. j = i – 1
4. while (j >= 0) and (A[j] > key)
5. A[j+1] = A[j]
6. j = j – 1
7. A[j+1] = key
Array index 0 1 2 3 4 5 6 7 8 9Value 0.56 1.12 2.78 7.42 1.17 0.32 6.21
4.42 3.14 7.71 1.17 7.42
22
Iteration 4: step 0.
Insertion Sort
InsertionSort(A, n)
1. for i = 1 to n-1
2. key = A[i]
3. j = i – 1
4. while (j >= 0) and (A[j] > key)
5. A[j+1] = A[j]
6. j = j – 1
7. A[j+1] = key
Array index 0 1 2 3 4 5 6 7 8 9 Value 0.56 1.12 2.78 1.17 7.42 0.32 6.21
4.42 3.14 7.71
1.17 2.78
23
Iteration 4: step 1.
Insertion Sort
InsertionSort(A, n)
1. for i = 1 to n-1
2. key = A[i]
3. j = i – 1
4. while (j >= 0) and (A[j] > key)
5. A[j+1] = A[j]
6. j = j – 1
7. A[j+1] = key
Array index 0 1 2 3 4 5 6 7 8 9Value 0.56 1.12 1.17 2.78 7.42 0.32 6.21 4.42 3.14 7.71 24
Iteration 4: step 2.
Insertion Sort
InsertionSort(A, n)
1. for i = 1 to n-1
2. key = A[i]
3. j = i – 1
4. while (j >= 0) and (A[j] > key)
5. A[j+1] = A[j]
6. j = j – 1
7. A[j+1] = key
Array index 0 1 2 3 4 5 6 7 8 9Value 0.56 1.12 1.17 2.78 7.42 0.32 6.21
4.42 3.14 7.71 0.32 7.42
25
Iteration 5: step 0.
Insertion Sort
InsertionSort(A, n)
1. for i = 1 to n-1
2. key = A[i]
3. j = i – 1
4. while (j >= 0) and (A[j] > key)
5. A[j+1] = A[j]
6. j = j – 1
7. A[j+1] = key
Array index 0 1 2 3 4 5 6 7 8 9Value 0.56 1.12 1.17 2.78 0.32 7.42 6.21
4.42 3.14 7.71 0.32 2.78
26
Iteration 5: step 1.
Insertion Sort
InsertionSort(A, n)
1. for i = 1 to n-1
2. key = A[i]
3. j = i – 1
4. while (j >= 0) and (A[j] > key)
5. A[j+1] = A[j]
6. j = j – 1
7. A[j+1] = key
Array index 0 1 2 3 4 5 6 7 8 9Value 0.56 1.12 1.17 0.32 2.78 7.42 6.21
4.42 3.14 7.71 0.32 1.17
27
Iteration 5: step 2.
Insertion Sort
InsertionSort(A, n)
1. for i = 1 to n-1
2. key = A[i]
3. j = i – 1
4. while (j >= 0) and (A[j] > key)
5. A[j+1] = A[j]
6. j = j – 1
7. A[j+1] = key
Array index 0 1 2 3 4 5 6 7 8 9Value 0.56 1.12 0.32 1.17 2.78 7.42 6.21
4.42 3.14 7.71 0.32 1.12
28
Iteration 5: step 3.
Insertion Sort
InsertionSort(A, n)
1. for i = 1 to n-1
2. key = A[i]
3. j = i – 1
4. while (j >= 0) and (A[j] > key)
5. A[j+1] = A[j]
6. j = j – 1
7. A[j+1] = key
Array index 0 1 2 3 4 5 6 7 8 9Value 0.56 0.32 1.12 1.17 2.78 7.42 6.21 4.42
3.14 7.71 0.32 0.56
29
Iteration 5: step 4.
Insertion Sort
InsertionSort(A, n)
1. for i = 1 to n-1
2. key = A[i]
3. j = i – 1
4. while (j >= 0) and (A[j] > key)
5. A[j+1] = A[j]
6. j = j – 1
7. A[j+1] = key
Array index 0 1 2 3 4 5 6 7 8 9Value 0.56 0.32 1.12 1.17 2.78 7.42 6.21 4.42
3.14 7.71 0.32 0.56
30
Iteration 5: step 5.
Insertion Sort
InsertionSort(A, n)
1. for i = 1 to n-1
2. key = A[i]
3. j = i – 1
4. while (j >= 0) and (A[j] > key)
5. A[j+1] = A[j]
6. j = j – 1
7. A[j+1] = key
Array index 0 1 2 3 4 5 6 7 8 9Value 0.32 0.56 1.12 1.17 2.78 7.42 6.21
4.42 3.14 7.71 6.21 7.42
31
Iteration 6: step 0.
Insertion Sort
InsertionSort(A, n)
1. for i = 1 to n-1
2. key = A[i]
3. j = i – 1
4. while (j >= 0) and (A[j] > key)
5. A[j+1] = A[j]
6. j = j – 1
7. A[j+1] = key
Array index 0 1 2 3 4 5 6 7 8 9Value 0.32 0.56 1.12 1.17 2.78 7.42 6.21
4.42 3.14 7.71 6.21 7.42
32
Iteration 6: step 1.
Insertion Sort
InsertionSort(A, n)
1. for i = 1 to n-1
2. key = A[i]
3. j = i – 1
4. while (j >= 0) and (A[j] > key)
5. A[j+1] = A[j]
6. j = j – 1
7. A[j+1] = key
Array index 0 1 2 3 4 5 6 7 8 9Value 0.32 0.56 1.12 1.17 2.78 6.21 7.42
4.42 3.14 7.71 4.42 7.42
33
Iteration 7: step 0.
Insertion Sort
InsertionSort(A, n)
1. for i = 1 to n-1
2. key = A[i]
3. j = i – 1
4. while (j >= 0) and (A[j] > key)
5. A[j+1] = A[j]
6. j = j – 1
7. A[j+1] = key
Array index 0 1 2 3 4 5 6 7 8 9Value 0.32 0.56 1.12 1.17 2.78 6.21 4.42
7.42 3.14 7.71 4.42 6.21
34
Iteration 7: step 1.
Insertion Sort
InsertionSort(A, n)
1. for i = 1 to n-1
2. key = A[i]
3. j = i – 1
4. while (j >= 0) and (A[j] > key)
5. A[j+1] = A[j]
6. j = j – 1
7. A[j+1] = key
Array index 0 1 2 3 4 5 6 7 8 9Value 0.32 0.56 1.12 1.17 2.78 6.21 4.42
7.42 3.14 7.71 4.42 6.21
35
Iteration 7: step 2.
Insertion Sort
InsertionSort(A, n)
1. for i = 1 to n-1
2. key = A[i]
3. j = i – 1
4. while (j >= 0) and (A[j] > key)
5. A[j+1] = A[j]
6. j = j – 1
7. A[j+1] = key
Array index 0 1 2 3 4 5 6 7 8 9Value 0.32 0.56 1.12 1.17 2.78 4.42 6.21
7.42 3.14 7.71 3.14 7.42
36
Iteration 8: step 0.
Insertion Sort
InsertionSort(A, n)
1. for i = 1 to n-1
2. key = A[i]
3. j = i – 1
4. while (j >= 0) and (A[j] > key)
5. A[j+1] = A[j]
6. j = j – 1
7. A[j+1] = key
Array index 0 1 2 3 4 5 6 7 8 9Value 0.32 0.56 1.12 1.17 2.78 4.42 6.21
3.14 7.42 7.71 3.14 6.21
37
Iteration 8: step 1.
Insertion Sort
InsertionSort(A, n)
1. for i = 1 to n-1
2. key = A[i]
3. j = i – 1
4. while (j >= 0) and (A[j] > key)
5. A[j+1] = A[j]
6. j = j – 1
7. A[j+1] = key
Array index 0 1 2 3 4 5 6 7 8 9Value 0.32 0.56 1.12 1.17 2.78 4.42 3.14
6.21 7.42 7.71 3.14 4.42
38
Iteration 8: step 2.
Insertion Sort
InsertionSort(A, n)
1. for i = 1 to n-1
2. key = A[i]
3. j = i – 1
4. while (j >= 0) and (A[j] > key)
5. A[j+1] = A[j]
6. j = j – 1
7. A[j+1] = key
Array index 0 1 2 3 4 5 6 7 8 9Value 0.32 0.56 1.12 1.17 2.78 4.42 3.14
6.21 7.42 7.71 3.14 4.42
39
Iteration 8: step 3.
Insertion Sort
InsertionSort(A, n)
1. for i = 1 to n-1
2. key = A[i]
3. j = i – 1
4. while (j >= 0) and (A[j] > key)
5. A[j+1] = A[j]
6. j = j – 1
7. A[j+1] = key
Array index 0 1 2 3 4 5 6 7 8 9Value 0.32 0.56 1.12 1.17 2.78 3.14 4.42 6.21 7.42 7.71 40
Iteration 9: step 0.
Insertion Sort
InsertionSort(A, n)
1. for i = 1 to n-1
2. key = A[i]
3. j = i – 1
4. while (j >= 0) and (A[j] > key)
5. A[j+1] = A[j]
6. j = j – 1
7. A[j+1] = key
Array index 0 1 2 3 4 5 6 7 8 9Value 0.32 0.56 1.12 1.17 2.78 3.14 4.42 6.21 7.42 7.71 41
Insertion Sort j = i - 1;
Analysis
cost times c1 n-1 c2 n-1 c3
InsertionSort(A, n) { n-1
for i = 1 to n-1 {
key = A[i]
while (j>=0) } ∑ − 1
= 1n
and (A[j] > c4 c5 c6 t
i
∑−=11n
i
key){ A[j+1] =
A[j] ∑ − 1
= 1n
it
j = j – 1 i i t
i
at iteration i
A[j+1] = key c7 n-1
}
}
42
ti: # of times the while statement is executed
+ −−
= T n c n c n c n c t c t c t c n = 1 − + 2 − + 3 − + 4∑
n +
∑ ∑
+
n
−
1
n
−
1
1
( ) ( 1) ( 1) ( 1) ( 1)7 5
i
6
i i
i i
1
=
1
i
=
1
Best Case
Analysis “while j >= 0 and A[j] > key”
T n c n c n c n c t c t c t c n ( ) ( 1) ( 1) ( 1) ( 1) 7
6 i
=1 i =
−+2 i 1
i i
−+3 = =
1
5
i 1
Worst Case
Analysis “while j >= 0 and A[j] > key”
• The array is in reverse sorted order
• The number of moves: 2*(n-1)+(1+2+...+n-1)= 2*(n-1)+ n*(n-1)/2
• The number of key comparisons: (1+2+...+n-1)= n*(n-1)/2 •
Always A[j] > key in while loop test
• Have to compare key with all elements to the left of the j-th position
⇒ compare with i-1 elements ⇒ tj = i −∑=−
n
nn
1 i
( 1) t
i
2
=
1
nn
Tncncncnccc
⎛−
( 1)
⎞
−
( ) ( 1) ( 1) ( 1) ( ) 1 2 3 4 5 6 ⎟ + 7 ⎠
( 1)
=−+−+−+++cn 2
⎜
2 ⎝
= an +bn+c a quadratic function of n
T(n) = Θ(n2) order of growth in n2
− 1 − 1 − 1 44
n n n
+−
− + 4∑
+
∑ ∑ +
T n c n c n c n c t c t c t c n ( ) ( 1) ( 1) ( 1) ( 1) 7
6 i
=1 i =
−+2 i 1
i i
−+3 = =
1
5
i 1
• Average-case: 🡺 O(n2)
• We have to look at all possible initial data organizations.
• Advantages
• Good running time for “almost sorted” arrays Θ(n)
• Disadvantages
• Θ(n2) running time in worst and average case
• ≈ n2/2 comparisons and exchanges 45
Merge Sort
• Idea: Order a list of values by recursively dividing the list in half until
each sub-list has one element, then recombining
• More specifically:
• Divide the n element sequence to be sorted into two subsequences
of n/2 elements each.
• Conquer: Sort the two subsequences to produce the sorted answer.
• Combine: Merge the two sorted sub sequences to produce the
sorted answer.
48
sort merge(0, n/2, n-1) sort
Merge Sort
Unsorted
Base Case: When the sequences to be sorted has length 1.
66 108 56 14 89 12 34 Sorted 12 14 34 56 66 89 108
Divide
89 12 34 Merge
Divide Merge 12 34 89 Divide BCas
56 14
Merge Merge 14 56
66 66 108 56 14 89 12
Merge Function
Given two sorted arrays, merge operation produces a sorted array
with all the elements of the two arrays
81432
..
Divide the array in half and conquer .
14823
Level 0 Level 1
Level 2
n/2 n/2 Merge n items:
Each level requires O(n)
operations
n)
Each level O(n) operations & O(log2n) levels 🡪 O(n*log2 54
• Advantage:
• Mergesort is an extremely fast algorithm.
• Disadvantage:
• Mergesort requires a second array as large as the original array. 55
Quick Sort
• Quick Sort: orders a list of values by partitioning the list around
one element called a pivot, then sorting each partition. It is based
on divide and conquer approach.
partition
6
40 37
10 12 217 18 32 35
quicksort quicksort
2 6 10 12 17 18 32 35 37 40
combine
57
2
6 10 12 17 18 32 35 37 40
Partitioning Algorithm
Original input : S = {8, 1, 4, 9, 0, 3, 5, 2, 7,
6} Pick the first/last element as pivot
8149035276
pivot
Have two ‘iterators’ – i and j
• i starts at first element and moves forward
• j starts at last element and moves backwards
8149035276
i jpivot
While (i < j)
• Move i to the right till we find a number greater than pivot
• Move j to the left till we find a number smaller than pivot
If (i<j) swap(S[i], S[j])
59
• The effect is to push larger elements to the right and smaller elements to the left
Swap the pivot with S[i]
i and j
Partitioning Pseudocode
What is the running time of
PARTITION(A, p, r) 1. pivot = partition()?
A[p];
2. leftPointer = p + 1 3. partition() runs in O(n) time
rightPointer = r 4. while (True)
5. while (A[leftPointer] < pivot)
6. leftPointer++;
7. while (A[rightPointer] >= pivot)
8. rightPointer--;
9. if leftPointer >= rightPointer
10. break;
11. else
12. A[leftPointer] 🡨🡪 A[rightPointer]
13. A[leftPointer] 🡨🡪 pivot
61
62
01
n-3 1
64
split) O (n lg n)
65
66
Counting Sort: Very useful when the keys have small range; stable;
memory space for counters and for 2n records.
Radix Sort: Appropriate for keys either rather short or with an
lexicographic collating sequence.
70