Module_3_Part_1
Module_3_Part_1
Why Sorting?
Practical application
People by last name
Countries by population
Search engine results by relevance
Externa
In memory sorting l
sorting
Comparison sorting Specialized
(N log N) Sorting
O(N log
O(N2) O(N)
N)
• Bubble • Merge • Bucket Sort • Simple
Sort Sort • Radix Sort External
4
• Selection • Quick Sort Merge
Sort • Heap Sort Sort
Comparison Sorting
0 1 2 3 4 5
42 Swap77
42 35 12 101 5
77
8
"Bubbling" largest element
0 1 2 3 4 5
42 35Swap35
77 77 12 101 5
9
"Bubbling" largest element
0 1 2 3 4 5
42 35 12Swap12
77 77 101 5
10
"Bubbling" largest element
0 1 2 3 4 5
42 35 12 77 101 5
No need to swap
11
"Bubbling" largest element
0 1 2 3 4 5
42 35 12 77 5 Swap101
101 5
12
"Bubbling" largest element
0 1 2 3 4 5
42 35 12 77 5 101
13
Bubble sort code
14
Selection Sort
Selection sort
more specifically:
find the smallest value in the list
switch it with the value in the first position
find the next smallest value in the list
switch it with the value in the second position
16
repeat until all values are in their proper places
Selection sort example
17
Selection sort example 2
Index
0 1 2 3 4 5 6 7
Value
27 63 1 72 64 58 14 9
1st pass
1 63 27 72 64 58 14 9
2nd pass
1 9 27 72 64 58 14 63
3rd pass
1 9 14 72 64 58 27 63
18 …
Selection sort code
more specifically:
consider the first item to be a sorted sublist of length 1
insert the second item into the sorted sublist, shifting the first item if
needed
insert the third item into the sorted sublist, shifting the other items
as needed
repeat until all values have been inserted into their proper positions
21
Insertion sort
2 8 15 1 17 10 12 5
after
0 1 2 3 4 5 6 7
pass 3
1 2 8 15 17 10 12 5
22
0 1 2 3 4 5 6 7
Insertion sort example
23
Insertion sort code
public static void insertionSort(int[] a) {
for (int i = 1; i < a.length; i++) {
int temp = a[i];
a[j] = temp;
24 }
MergeSort
MergeSort
26
MergeSort Algorithm
MergeSort is a recursive sorting procedure that
uses at most O(n lg(n)) comparisons.
To sort an array of n elements, we perform the
following steps in sequence:
If n < 2 then the array is already sorted.
Otherwise, n > 1, and we perform the following
three steps in sequence:
1. Sort the left half of the the array using MergeSort.
2. Sort the right half of the the array using MergeSort.
3. Merge the sorted left and right halves.
27
How to Merge
Here are two lists to be merged:
First: (12, 16, 17, 20, 21, 27)
Second: (9, 10, 11, 12, 19)
Compare12 and 9
First: (12, 16, 17, 20, 21, 27)
Second: (10, 11, 12, 19)
New: (9)
Compare 12 and 10
First: (12, 16, 17, 20, 21, 27)
Second: (11, 12, 19)
New: (9, 10)
28
Merge Example
Compare 12 and 11
First: (12, 16, 17, 20, 21, 27)
Second: (12, 19)
New: (9, 10, 11)
Compare 12 and 12
First: (16, 17, 20, 21, 27)
Second: (12, 19)
New: (9, 10, 11, 12)
29
Merge Example
Compare 16 and 12
First: (16, 17, 20, 21, 27)
Second: (19)
New: (9, 10, 11, 12, 12)
Compare 16 and 19
First: (17, 20, 21, 27)
Second: (19)
New: (9, 10, 11, 12, 12, 16)
30
Merge Example
Compare 17 and 19
First: (20, 21, 27)
Second: (19)
New: (9, 10, 11, 12, 12, 16, 17)
Compare 20 and 19
First: (20, 21, 27)
Second: ( )
New: (9, 10, 11, 12, 12, 16, 17, 19)
31
Merge Example
32
MergeSort
Original 24 13 26 1 12 27 38 15
Divide in 2 24 13 26 1 12 27 38 15
Divide in 4 24 13 26 1 12 27 38 15
Divide in 8 24 13 26 1 12 27 38 15
Merge 2 13 24 1 26 12 27 15 38
Merge 4 1 13 24 26 12 15 27 38
Merge 8 1 12 13 15 24 26 27 38
33
Merge-Sort Tree
An execution of merge-sort is depicted by a binary tree
• each node represents a recursive call of merge-sort and stores
unsorted sequence before the execution and its partition
sorted sequence at the end of the execution
• the root is the initial call
• the leaves are calls on subsequences of size 0 or 1
7 29 4 2 4 7 9
72 2 7 94 4 9
34
Execution Example
Partition
7 2 9 43 8 6 1
35
Execution Example (cont.)
7 29 4
36
Execution Example (cont.)
Recursive call, partition
7 2 9 43 8 6 1
7 29 4
72
37
Execution Example (cont.)
Recursive call, base case
7 2 9 43 8 6 1
7 29 4
72
77
38
Execution Example (cont.)
Recursive call, base case
7 2 9 43 8 6 1
7 29 4
72
77 22
39
Execution Example (cont.)
Merge
7 2 9 43 8 6 1
7 29 4
722 7
77 22
40
Execution Example (cont.)
Recursive call, …, base case, merge
7 2 9 43 8 6 1
7 29 4
722 7 9 4 4 9
41
Execution Example (cont.)
Merge
7 2 9 43 8 6 1
7 29 4 2 4 7 9
722 7 9 4 4 9
42
Execution Example (cont.)
Recursive call, …, merge, merge
7 2 9 43 8 6 1
7 29 4 2 4 7 9 3 8 6 1 1 3 6 8
722 7 9 4 4 9 3 8 3 8 6 1 1 6
43
Execution Example (cont.)
Merge
7 2 9 43 8 6 1 1 2 3 4 6 7 8 9
7 29 4 2 4 7 9 3 8 6 1 1 3 6 8
722 7 9 4 4 9 3 8 3 8 6 1 1 6
44
// A : Array that needs to be sorted
MergeSort(A)
{
n = length(A)
if n<2 return
mid = n/2
left = new_array_of_size(mid) // Creating temporary array for left
right = new_array_of_size(n-mid) // and right sub arrays
for(int i=0 ; i<=mid-1 ; ++i)
{
left[i] = A[i] // Copying elements from A to left
}
for(int i=mid ; i<=n-1 ; ++i)
{
right[i-mid] = A[i] // Copying elements from A to right
}
MergeSort(left) // Recursively solving for left sub array
MergeSort(right) // Recursively solving for right sub array
merge(left, right, A) // Merging two sorted left/right sub array to final array
}
Bitonic Sort
3, 4, 7, 8, 6, 5, 2, 1
Bitonic sort network
Algorithm
Complexity of MergeSort
Pass Number of Merge list # of comps /
Number merges length 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 = log n
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
58
Complexity of MergeSort
2 k
comparisons (and
moves). But k = lg n
and hence, we get
(21)2k-1 = 2k lg(n) n comparisons
(20)2k = 2k or O(n lgn)
59