Sortings
Sortings
Algorithms
Motivation of Sorting
• The term list here is a collection of records.
• Each record has one or more fields.
• Each record has a key to distinguish one record with
another.
• For example, the phone directory is a list. Name,
phone number, and even address can be the key,
depending on the application or need.
Two Common Categories
Sorting
Sorting
Algorithms
Algorithms
of O(N^2)
of
• Bubble Sort O(N log N)
• Selection
Heap SortSort
• Insertion
Merge Sort
Sort
• Quick Sort
For small values of N
• It is important to note that all algorithms
appear to run equally as fast for small values
of N.
• Simple to understand
38 45
60 60
66 45
66 79 47 13 74 36 21 94 22 57 16 29 81
[1] 26 [1] 77
15 48 19 15 1 5
[8] [9] [10] [8] [9] [10]
[1] 61 [1] 59
5 1 5
• Merge Sort
• Quick Sort
17
Divide and Conquer
1. Base Case, solve the problem directly if it is
small enough
18
Divide and Conquer - Sort
Problem:
• Input: A[left..right] – unsorted array of integers
19
Divide and Conquer - Sort
1. Base case
at most one element (left ≥ right), return
2. Divide A into two subarrays: FirstPart, SecondPart
Two Subproblems:
sort the FirstPart
sort the SecondPart
3. Recursively
sort FirstPart
sort SecondPart
4. Combine sorted FirstPart and sorted SecondPart
20
Merge Sort: Idea
Divide into
A FirstPart SecondPart
two halves
Recursively sort
FirstPart SecondPart
Merge
A is sorted!
21
Merge Sort: Algorithm
Merge-Sort (A, left, right)
if left ≥ right return
else
middle =(left+right)/2
Merge-Sort(A, left, middle) Recursive Call
22
Merge-Sort: Merge
Sorted
A:
merge
Sorted Sorted
FirstPart SecondPart
A:
A: 25 35 7 28
15 8 30
1 46 5 14
10 6
L: R:
3 5 15 28 6 10 14 22
Temporary Arrays
24
Merge-Sort: Merge Example
A:
31 5 15 28 30 6 10 14
k=0
L: R:
3
2 15
3 28
7 30
8 6
1 10
4 14
5 22
6
i=0 j=0
25
Merge-Sort: Merge Example
A:
1 25 15 28 30 6 10 14
k=1
L: R:
3
2 5
3 15
7 28
8 6
1 10
4 14
5 22
6
i=0 j=1
26
Merge-Sort: Merge Example
A:
1 2 3 28 30
15 6 10 14
k=2
L: R:
2 3 7 8 6
1 10
4 14
5 22
6
i=1 j=1
27
Merge-Sort: Merge Example
A:
1 2 3 4 6 10 14
k=3
L: R:
2 3 7 8 6
1 10
4 14
5 22
6
i=2 j=1
28
Merge-Sort: Merge Example
A:
1 2 3 4 5 6 10 14
k=4
L: R:
2 3 7 8 6
1 10
4 14
5 22
6
i=2 j=2
29
Merge-Sort: Merge Example
A:
1 2 3 4 5 6 10 14
k=5
L: R:
2 3 7 8 6
1 10
4 14
5 22
6
i=2 j=3
30
Merge-Sort: Merge Example
A:
1 2 3 4 5 6 7 14
k=6
L: R:
2 3 7 8 6
1 10
4 14
5 22
6
i=2 j=4
31
Merge-Sort: Merge Example
A:
1 2 3 4 5 6 7 8
14
k=7
L: R:
3
2 5
3 15
7 28
8 6
1 10
4 14
5 22
6
i=3 j=4
32
Merge-Sort: Merge Example
A:
1 2 3 4 5 6 7 8
k=8
L: R:
3
2 5
3 15
7 28
8 6
1 10
4 14
5 22
6
i=4 j=4
33
Merge(A, left, middle, right)
1. n1 = middle – left + 1
2. n2 = right – middle
3. create array L[n1], R[n2]
4. for i = 0 to n1-1 do L[i] = A[left +i]
5. for j = 0 to n2-1 do R[j] = A[middle+j]
6. k = i = j = 0
7. while (i < n1 & j < n2)
8. if ( L[i] < R[j])
9. A[k++] = L[i++]
10. else
11. A[k++] = R[j++]
12. while i < n1
13. A[k++] = L[i++]
14. while j < n2
15. A[k++] = R[j++]
34
Merge-Sort(A, 0, 7)
Divide
A: 6 2 8 4 33 7 7 55 11
35
Merge-Sort(A, 0, 7)
Merge-Sort(A, 0, 3) , divide
A: 3 7 5 1
6 2 88 44
36
Merge-Sort(A, 0, 7)
Merge-Sort(A, 0, 1) , divide
A: 3 7 5 1
8 4
6 22
37
Merge-Sort(A, 0, 7)
Merge-Sort(A, 0, 0) , base case
A: 3 7 5 1
8 4
38
Merge-Sort(A, 0, 7)
Merge-Sort(A, 0, 0), return
A: 3 7 5 1
8 4
6 2
39
Merge-Sort(A, 0, 7)
Merge-Sort(A, 1, 1) , base case
A: 3 7 5 1
8 4
40
Merge-Sort(A, 0, 7)
Merge-Sort(A, 1, 1), return
A: 3 7 5 1
8 4
6 2
41
Merge-Sort(A, 0, 7)
Merge(A, 0, 0, 1)
A: 3 7 5 1
8 4
2 6
42
Merge-Sort(A, 0, 7)
Merge-Sort(A, 0, 1), return
A: 3 7 5 1
2 6 8 4
43
Merge-Sort(A, 0, 7)
Merge-Sort(A, 2, 3) , divide
A: 3 7 5 1
2 6
8 4
44
Merge-Sort(A, 0, 7)
Merge-Sort(A, 2, 2), base case
A: 3 7 5 1
2 6
45
Merge-Sort(A, 0, 7)
Merge-Sort(A, 2, 2), return
A: 3 7 5 1
2 6
8 4
46
Merge-Sort(A, 0, 7)
Merge-Sort(A, 3, 3), base case
A:
2 6
47
Merge-Sort(A, 0, 7)
Merge-Sort(A, 3, 3), return
A: 3 7 5 1
2 6
8 4
48
Merge-Sort(A, 0, 7)
Merge(A, 2, 2, 3)
A: 3 7 5 1
2 6
4 8
49
Merge-Sort(A, 0, 7)
Merge-Sort(A, 2, 3), return
A: 3 7 5 1
2 6 4 8
50
Merge-Sort(A, 0, 7)
Merge(A, 0, 1, 3)
A: 3 7 5 1
2 4 6 8
51
Merge-Sort(A, 0, 7)
Merge-Sort(A, 0, 3), return
A: 2 4 6 8 3 7 5 1
52
Merge-Sort(A, 0, 7)
Merge-Sort(A, 4, 7)
A: 2 4 6 8
3 7 5 1
53
Merge-Sort(A, 0, 7)
Merge (A, 4, 5, 7)
A: 2 4 6 8
1 3 5 7
54
Merge-Sort(A, 0, 7)
Merge-Sort(A, 4, 7), return
A: 2 4 6 8 1 3 5 7
55
Quick Sort
• Divide:
• Pick any element p as the pivot, e.g, the first element
• Partition the remaining elements into
FirstPart, which contains all elements < p
SecondPart, which contains all elements ≥ p
56
Quick Sort
A: p
pivot Partition
FirstPart SecondPart
x<p p p≤x
Recursive call
Sorted Sorted
FirstPart SecondPart
x<p p p≤x
Sorted 57
Quick Sort
Quick-Sort(A, left, right)
if left ≥ right return
else
middle = Partition(A, left, right)
Quick-Sort(A, left, middle–1 )
Quick-Sort(A, middle+1, right)
end if
58
Partition
A: p
A: p x<p p≤x
A:
p x<p p p≤x
59
Partition Example
A: 4 8 6 3 5 1 7 2
60
Partition Example
i=0
A: 4 8 6 3 5 1 7 2
j=1
61
Partition Example
i=0
A: 4 8 6 3 5 1 7 2
j=1
62
Partition Example
i=0
A: 4 8 6 3 5 1 7 2
j=2
63
Partition Example
i=0i=1
A: 4 8
3 6 83 5 1 7 2
j=3
64
Partition Example
i=1
A: 4 3 6 8 5 1 7 2
j=4
65
Partition Example
i=1
A: 4 3 6 8 5 1 7 2
j=5
66
Partition Example
i=2
A: 4 3 61 8 5 16 7 2
j=5
67
Partition Example
i=2
A: 4 3 1 8 5 6 7 2
j=6
68
Partition Example
i=2i=3
A: 4 3 1 82 5 6 7 82
j=7
69
Partition Example
i=3
A: 4 3 1 2 5 6 7 8
j=8
70
Partition Example
i=3
A: 24 3 1 24 5 6 7 8
71
Partition Example
pivot in
correct position
A: 2 3 1 4 5 6 7 8
x<4 4≤x
72
Partition(A, left, right)
1. x = A[left]
2. i = left
3. for j = left+1 to right
4. {
5. if (A[j] < x){
6. i = i + 1
7. swap(A[i], A[j])
8. }
9. }
10.swap(A[i], A[left])
11.return i
73
Quick-Sort(A, 0, 7)
Partition
A: 42 3
8 61 34 55 16 77 8
2
74
Quick-Sort(A, 0, 7)
Quick-Sort(A, 0, 2) , partition
A: 4 5 6 7 8
21 23 31
75
Quick-Sort(A, 0, 7)
Quick-Sort(A, 0, 0) , base
returncase
4 5 6 7 8
1 2 3
76
Quick-Sort(A, 0, 7)
Quick-Sort(A, 2, 2) , base case
4 5 6 7 8
1 2
77
Quick-Sort(A, 0, 7)
Quick-Sort(A,2,0,2),
Quick-Sort(A, 2),return
return
1 2 3 4 5 6 7 8
1 2 3
78
Quick-Sort(A, 0, 7)
Quick-Sort(A,2,4,2),
Quick-Sort(A, 7) return
, partition
1 2 3 4
55 6 7 8
79
Quick-Sort(A, 0, 7)
Quick-Sort(A, 5, 7) , partition
1 2 3 4
6 77 88
80
Quick-Sort(A, 0, 7)
Quick-Sort(A, 6, 7) , partition
1 2 3 4
7 88
81
Quick-Sort(A, 0, 7)
Quick-Sort(A, 7, 7) returncase
, base
1 2 3 4
7 8
82
Quick-Sort(A, 0, 7)
Quick-Sort(A, 6, 7) , return
1 2 3 4
6 7 8
83
Quick-Sort(A, 0, 7)
Quick-Sort(A, 5, 7) , return
1 2 3 4
5 6 7 8
84
Quick-Sort(A, 0, 7)
Quick-Sort(A, 4, 7) , return
1 2 3 4 5 6 7 8
85
Quick-Sort(A, 0, 7)
Quick-Sort(A, 0, 7) , done!
1 2 3 4 5 6 7 8
86
Radix Sort
Extra information: every integer can be
represented by at most k digits
– d1d2…dk where di are digits in base r
– d1: most significant digit
– dk: least significant digit
Radix Sort
• Algorithm
– sort by the least significant digit first (counting sort)
=> Numbers with the same digit go to same bin
– reorder all the numbers: the numbers in bin 0
precede the numbers in bin 1, which precede the
numbers in bin 2, and so on
– sort by the next least significant digit
– continue this process until the numbers have been
sorted on all k digits
Radix Sort
• Assume each key has a link field. Then the keys in the
same bin are linked together into a chain:
– f[i], 0 ≤ i ≤ r (the pointer to the first record in bin i)
– e[i], (the pointer to the end record in bin i)
– The chain will operate as a queue.
Radix Sort Example
list[1] list[2] list[3] list[4] list[5] list[6] list[7] list[8] list[9] list[10]
e[0] e[1] e[2] e[3] e[4] e[5] e[6] e[7] e[8] e[9]
33 859
f[0] f[1] f[2] f[3] f[4] f[5] f[6] f[7] f[8] f[9]
list[1] list[2] list[3] list[4] list[5] list[6] list[7] list[8] list[9] list[10]
list[1] list[2] list[3] list[4] list[5] list[6] list[7] list[8] list[9] list[10]
e[0] e[1] e[2] e[3] e[4] e[5] e[6] e[7] e[8] e[9]
f[0] f[1] f[2] f[3] f[4] f[5] f[6] f[7] f[8] f[9]
list[1] list[2] list[3] list[4] list[5] list[6] list[7] list[8] list[9] list[10]
list[1] list[2] list[3] list[4] list[5] list[6] list[7] list[8] list[9] list[10]
93
55
33 271
f[0] f[1] f[2] f[3] f[4] f[5] f[6] f[7] f[8] f[9]
list[1] list[2] list[3] list[4] list[5] list[6] list[7] list[8] list[9] list[10]