Professional Documents
Culture Documents
Insertion, Merge, Radix, Quick and Bucket Sort
Insertion, Merge, Radix, Quick and Bucket Sort
Insertion Sort
Sorting Algorithms
2 Sorting Algorithms
1
6/9/2023
12 11 13 5 6
Example 2
4 3 2 10 12 1 5 6
2
6/9/2023
3
6/9/2023
4
6/9/2023
98 23 45 14 6 67 33 42
5
6/9/2023
98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42
98 23 45 14 98 23 45 14
98 23
6
6/9/2023
98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42
98 23 45 14 98 23 45 14
98 23 98 23
23
Merge Merge
7
6/9/2023
98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42
98 23 45 14 98 23 45 14
98 23 98 23 45 14
23 98 23 98
Merge
8
6/9/2023
98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42
98 23 45 14 98 23 45 14
98 23 45 14 98 23 45 14
23 98 23 98 14
Merge Merge
9
6/9/2023
98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42
98 23 45 14 98 23 45 14
98 23 45 14 98 23 45 14
23 98 14 45 23 98 14 45
Merge
Merge
19 Sorting Algorithms 20 Sorting Algorithms
10
6/9/2023
98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42
98 23 45 14 98 23 45 14
98 23 45 14 98 23 45 14
23 98 14 45 23 98 14 45
14 14 23
Merge Merge
21 Sorting Algorithms 22 Sorting Algorithms
11
6/9/2023
98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42
98 23 45 14 98 23 45 14
98 23 45 14 98 23 45 14
23 98 14 45 23 98 14 45
14 23 45 14 23 45 98
Merge Merge
23 Sorting Algorithms 24 Sorting Algorithms
12
6/9/2023
98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42
98 23 45 14 98 23 45 14 6 67
23 98 14 45 23 98 14 45
14 23 45 98 14 23 45 98
13
6/9/2023
98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42
98 23 45 14 6 67 98 23 45 14 6 67
23 98 14 45 23 98 14 45 6
14 23 45 98 14 23 45 98
Merge Merge
14
6/9/2023
98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42
98 23 45 14 6 67 98 23 45 14 6 67 33 42
23 98 14 45 6 67 23 98 14 45 6 67
14 23 45 98 14 23 45 98
Merge
15
6/9/2023
98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42
23 98 14 45 6 67 23 98 14 45 6 67 33
14 23 45 98 14 23 45 98
Merge Merge
16
6/9/2023
98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42 23 98 14 45 6 67 33 42
14 23 45 98 14 23 45 98
Merge
Merge
33 Sorting Algorithms 34 Sorting Algorithms
17
6/9/2023
98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42 23 98 14 45 6 67 33 42
14 23 45 98 6 14 23 45 98 6 33
Merge Merge
35 Sorting Algorithms 36 Sorting Algorithms
18
6/9/2023
98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42 23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 14 23 45 98 6 33 42 67
Merge Merge
37 Sorting Algorithms 38 Sorting Algorithms
19
6/9/2023
98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42 23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 67 14 23 45 98 6 33 42 67
39 Merge 40 Merge
20
6/9/2023
98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42 23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 67 14 23 45 98 6 33 42 67
6 14 6 14 23
41 Merge 42 Merge
21
6/9/2023
98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42 23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 67 14 23 45 98 6 33 42 67
6 14 23 33 6 14 23 33 42
43 Merge 44 Merge
22
6/9/2023
98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42 23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 67 14 23 45 98 6 33 42 67
6 14 23 33 42 45 6 14 23 33 42 45 67
45 Merge 46 Merge
23
6/9/2023
24
6/9/2023
25
6/9/2023
26
6/9/2023
27
6/9/2023
Sorting by least significant digit (1s place) gives: Sorting by least significant digit (1s place) gives:
170, 90, 802, 2, 24, 45, 75, 66 170, 90, 802, 2, 24, 45, 75, 66
Notice that we keep 802 before 2, because 802 occurred before 2 in the
original list, and similarly for pairs 170 & 90 and 45 & 75.
28
6/9/2023
Sorting by least significant digit (1s place) gives: Sorting by least significant digit (1s place) gives:
170, 90, 802, 2, 24, 45, 75, 66 170, 90, 802, 2, 24, 45, 75, 66
Sorting by next digit (10s place) gives: Sorting by next digit (10s place) gives:
802, 2, 24, 45, 66, 170, 75, 90 802, 2, 24, 45, 66, 170, 75, 90
Notice that 802 again comes before 2 as 802 comes before 2 in the
previous list.
29
6/9/2023
30
6/9/2023
31
6/9/2023
32
6/9/2023
33
6/9/2023
34
6/9/2023
35
6/9/2023
There are a number of ways to pick the pivot element. In this example, we will use the
first element in the array: Given a pivot, partition the elements of the array such that
the resulting array consists of:
40 20 10 80 60 50 7 30 100 1. One sub-array that contains elements >= pivot
2. Another sub-array that contains elements < pivot
36
6/9/2023
pivot_index = 0 40 20 10 80 60 50 7 30 100
[0] [1] [2] [3] [4] [5] [6] [7] [8] pivot_index = 0 40 20 10 80 60 50 7 30 100
37
6/9/2023
pivot_index = 0 40 20 10 80 60 50 7 30 100
pivot_index = 0 40 20 10 80 60 50 7 30 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
low_index high_index
low_index high_index
38
6/9/2023
pivot_index = 0 40 20 10 80 60 50 7 30 100
pivot_index = 0 40 20 10 80 60 50 7 30 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
low_index high_index
low_index high_index
39
6/9/2023
[0] [1] [2] [3] [4] [5] [6] [7] [8] [0] [1] [2] [3] [4] [5] [6] [7] [8]
40
6/9/2023
[0] [1] [2] [3] [4] [5] [6] [7] [8] [0] [1] [2] [3] [4] [5] [6] [7] [8]
41
6/9/2023
[0] [1] [2] [3] [4] [5] [6] [7] [8] [0] [1] [2] [3] [4] [5] [6] [7] [8]
42
6/9/2023
[0] [1] [2] [3] [4] [5] [6] [7] [8] [0] [1] [2] [3] [4] [5] [6] [7] [8]
43
6/9/2023
[0] [1] [2] [3] [4] [5] [6] [7] [8] [0] [1] [2] [3] [4] [5] [6] [7] [8]
44
6/9/2023
[0] [1] [2] [3] [4] [5] [6] [7] [8] [0] [1] [2] [3] [4] [5] [6] [7] [8]
45
6/9/2023
[0] [1] [2] [3] [4] [5] [6] [7] [8] [0] [1] [2] [3] [4] [5] [6] [7] [8]
46
6/9/2023
[0] [1] [2] [3] [4] [5] [6] [7] [8] [0] [1] [2] [3] [4] [5] [6] [7] [8]
47
6/9/2023
[0] [1] [2] [3] [4] [5] [6] [7] [8] [0] [1] [2] [3] [4] [5] [6] [7] [8]
48
6/9/2023
Partition Result
1. While data[low_index] <= data[pivot]
++low_index
2. While data[high_index] > data[pivot]
--high_index
3. If low_index < high_index
7 20 10 30 40 50 60 80 100
swap data[low_index] and data[high_index]
4. While high_index > low_index, go to 1. [0] [1] [2] [3] [4] [5] [6] [7] [8]
5. Swap data[high_index] and data[pivot_index]
low_index high_index
49
6/9/2023
7 20 10 30 40 50 60 80 100
50
6/9/2023
Assume that keys are random, uniformly Assume that keys are random, uniformly
distributed. distributed.
What is best case running time? What is best case running time?
Recursion: Recursion:
1. Partition splits array in two sub-arrays of size n/2 1. Partition splits array in two sub-arrays of size n/2
2. Quicksort each sub-array 2. Quicksort each sub-array
Depth of recursion tree?
51
6/9/2023
Assume that keys are random, uniformly Assume that keys are random, uniformly
distributed. distributed.
What is best case running time? What is best case running time?
Recursion: Recursion:
1. Partition splits array in two sub-arrays of size n/2 1. Partition splits array in two sub-arrays of size n/2
2. Quicksort each sub-array 2. Quicksort each sub-array
Depth of recursion tree? O(log2n) Depth of recursion tree? O(log2n)
Number of accesses in partition?
52
6/9/2023
Assume that keys are random, uniformly Assume that keys are random, uniformly
distributed. distributed.
What is best case running time? Best case running time: O(n log2n)
Recursion:
1. Partition splits array in two sub-arrays of size n/2
2. Quicksort each sub-array
Depth of recursion tree? O(log2n)
Number of accesses in partition? O(n)
53
6/9/2023
low_index high_index
54
6/9/2023
[0] [1] [2] [3] [4] [5] [6] [7] [8] [0] [1] [2] [3] [4] [5] [6] [7] [8]
55
6/9/2023
[0] [1] [2] [3] [4] [5] [6] [7] [8] [0] [1] [2] [3] [4] [5] [6] [7] [8]
56
6/9/2023
[0] [1] [2] [3] [4] [5] [6] [7] [8] [0] [1] [2] [3] [4] [5] [6] [7] [8]
57
6/9/2023
Quicksort Analysis
1. While data[low_index] <= data[pivot]
++low_index
2. While data[high_index] > data[pivot] Assume that keys are random, uniformly
--high_index
3. If low_index < high_index
distributed.
swap data[low_index] and data[high_index] Best case running time: O(n log2n)
4. While high_index > low_index, go to 1. Worst case running time?
5. Swap data[high_index] and data[pivot_index]
Recursion:
1. Partition splits array in two sub-arrays:
pivot_index = 0 2 4 10 12 13 50 57 63 100 • one sub-array of size 0
• the other sub-array of size n-1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
2. Quicksort each sub-array
Depth of recursion tree?
<= data[pivot] > data[pivot]
58
6/9/2023
59
6/9/2023
60