Unit 7_Searching, Sorting, Hashing
Unit 7_Searching, Sorting, Hashing
UNIT 7
SEARCHING, SORTING AND HASHING: 8 HOURS
SYLLABUS CONTENT
LINEAR SEARCH
Unordered List
One occurrence
All occurrences
ALGORITHM
LINEAR SEARCH
Unordered List All occurrences
One occurrence (with function and without int search(int arr[], int n, int x)
function)
{
int search(int arr[], int n, int x)
int i, count = 0;
{
for (i = 0; i < n; i++)
int i;
{
for (i = 0; i < n; i++) if (arr[i] == x)
if (arr[i] == x) {
return i; cout <<x<<“ is present at location ”<<i+1<<endl;
return -1; count++;
} }
}
return count;
DR. NEEPA SHAH 5
}
BINARY SEARCH
Given an integer X and integers A0 , A1 , …, AN-1, which are presorted, find i such that Ai = X, or
return i = -1 if X is not in the input.
6
DR. NEEPA SHAH 6
7
DR. NEEPA SHAH 7
8
DR. NEEPA SHAH 8
ALGORITHM: ITERATIVE
The work done inside the while loop clearly takes O (1) time, so the total running
time depends on the number of iterations of the while loop.
Each iteration reduces the number of elements remaining to be considered by half,
so clearly the number of iterations required is k.Thus T (N ) = O (log N ).
FIBONACCI SEARCH
Find the immediate Fibonacci number that is greater than or equal to the size of the input array. Then, also hold the two
preceding numbers of the selected Fibonacci number.
Initialize the offset value as -1
Until Fm2 is greater than 0
Compare the key element with the element at index [min(offset+Fm-2,n-1)]. If a match is found, return the index.
If the key element is lesser value than this element, reduce the range of the input from 0 to the index of this element. The Fibonacci
numbers are also updated with Fm = Fm2.
If the key is greater than the element at this index, remove the elements before this element from the search range. The Fibonacci
numbers are updated as Fm = Fm1.The offset value is set to the index of this element.
As there are two 1s in the Fibonacci series, there arises a case where two preceding numbers will become 1. So if Fm1
becomes 1, there is only one element left in the array to be searched. Compare the key element with that element and
return the 1st index. Otherwise, the algorithm returns an unsuccessful search.
EXAMPLE
Step 3:
update the offset value and the Fibonacci numbers
Offset = 4
Fm = Fm1 = 8; Fm1 = 5; Fm2 = 3
Step 1:
Compare it with the element at index = minimum (offset + Fm2, n – 1) = 7.
n = 10
The smallest Fibonacci number greater than 10 is 13.
Fm = 13, Fm1 = 8, Fm2 = 5
offset = -1
Step 2:
index = minimum (offset + Fm2, n – 1) = minimum (-1 +
5, 9) = 4
EXAMPLE CONTD.
Step 4:
discard the elements after the 7th index, so n = 7
offset value remains 4
Fibonacci numbers are pushed two steps backward, i.e. Fm = Fm2 = 3.
Fm1 = 2, Fm2 = 1
Compare it with the element at index = minimum (offset + Fm-2, n – 1) = 5.
EXAMPLE
WHAT IS SORTING?
Examples:
Sorting Books in Library
Sorting Individuals by Height (Feet and Inches)
Sorting Movies in Blockbuster (Alphabetical)
Sorting Numbers (Sequential)
CLASSIFICATION
SORTING ALGORITHMS
Bubble Sort
Selection Sort
Insertion Sort
Shell Sort
Merge Sort
Quick Sort
Heap Sort
Bucket Sort
Radix Sort
DR. NEEPA SHAH 22
BUBBLE SORT
9, 6, 2, 12, 11, 9, 3, 7
6, 9, 2, 12, 11, 9, 3, 7
6, 2, 9, 12, 11, 9, 3, 7
6, 2, 9, 12, 11, 9, 3, 7
6, 2, 9, 11, 12, 9, 3, 7
6, 2,
BUBBLE SORT EXAMPLE
9, 11, 9, 12, 3, 7
6, 2,
DR. NEEPA SHAH
9, 11, 9, 3, 12, 7 24
6, 2, 9, 11, 9, 3, 7, 12
6, 6,
2, 2, 9, 9,
11,3,
11,
9,7,
11,
3,11,
7, 12
Third Pass
2, 6, 9, 9, 3, 7, 11, 12
2, 6, 9, 3,
9, 7,
3, 9,
9, 7, 11, 12
Third Pass
2, 6, 9, 9, 3, 7, 11, 12
Fourth Pass
2, 6, 9, 3, 7, 9, 11, 12
2, 6, 9,
3, 3, 9,
9,
7, 7, 9, 11, 12
Third Pass
2, 6, 9, 9, 3, 7, 11, 12
Fourth Pass
2, 6, 9, 3, 7, 9, 11, 12
Fifth Pass
2, 6, 3, 7, 9, 9, 11, 12
2, 6,
3, 3,
6, 7, 9, 9, 11, 12
DR. NEEPA SHAH 28
Third Pass
2, 6, 9, 9, 3, 7, 11, 12
Fourth Pass
2, 6, 9, 3, 7, 9, 11, 12
Fifth Pass
2, 6, 3, 7, 9, 9, 11, 12
Sixth Pass
2, 3, 6, 7, 9, 9, 11, 12
DR. NEEPA SHAH
2, 3, 6, 7, 9, 9, 11, 12 29
BUBBLE SORT
We can use a Boolean variable to determine if any swapping occurred during the “bubble up.”
AN EXAMPLE
N 8 did_swap true
to_do 7
index
98 23 45 14 6 67 33 42
1 2 3 4 5 6 7 8
AN EXAMPLE
N 8 did_swap false
to_do 7
index 1
98 23 45 14 6 67 33 42
1 2 3 4 5 6 7 8
AN EXAMPLE
N 8 did_swap false
to_do 7
index 1
Swap
98 23 45 14 6 67 33 42
1 2 3 4 5 6 7 8
AN EXAMPLE
N 8 did_swap true
to_do 7
index 1
Swap
23 98 45 14 6 67 33 42
1 2 3 4 5 6 7 8
AN EXAMPLE
N 8 did_swap true
to_do 7
index 2
23 98 45 14 6 67 33 42
1 2 3 4 5 6 7 8
AN EXAMPLE
N 8 did_swap true
to_do 7
index 2
Swap
23 98 45 14 6 67 33 42
1 2 3 4 5 6 7 8
AN EXAMPLE
N 8 did_swap true
to_do 7
index 2
Swap
23 45 98 14 6 67 33 42
1 2 3 4 5 6 7 8
AN EXAMPLE
N 8 did_swap true
to_do 7
index 3
23 45 98 14 6 67 33 42
1 2 3 4 5 6 7 8
AN EXAMPLE
N 8 did_swap true
to_do 7
index 3
Swap
23 45 98 14 6 67 33 42
1 2 3 4 5 6 7 8
AN EXAMPLE
N 8 did_swap true
to_do 7
index 3
Swap
23 45 14 98 6 67 33 42
1 2 3 4 5 6 7 8
AN EXAMPLE
N 8 did_swap true
to_do 7
index 4
23 45 14 98 6 67 33 42
1 2 3 4 5 6 7 8
AN EXAMPLE
N 8 did_swap true
to_do 7
index 4
Swap
23 45 14 98 6 67 33 42
1 2 3 4 5 6 7 8
AN EXAMPLE
N 8 did_swap true
to_do 7
index 4
Swap
23 45 14 6 98 67 33 42
1 2 3 4 5 6 7 8
AN EXAMPLE
N 8 did_swap true
to_do 7
index 5
23 45 14 6 98 67 33 42
1 2 3 4 5 6 7 8
AN EXAMPLE
N 8 did_swap true
to_do 7
index 5
Swap
23 45 14 6 98 67 33 42
1 2 3 4 5 6 7 8
AN EXAMPLE
N 8 did_swap true
to_do 7
index 5
Swap
23 45 14 6 67 98 33 42
1 2 3 4 5 6 7 8
AN EXAMPLE
N 8 did_swap true
to_do 7
index 6
23 45 14 6 67 98 33 42
1 2 3 4 5 6 7 8
AN EXAMPLE
N 8 did_swap true
to_do 7
index 6
Swap
23 45 14 6 67 98 33 42
1 2 3 4 5 6 7 8
AN EXAMPLE
N 8 did_swap true
to_do 7
index 6
Swap
23 45 14 6 67 33 98 42
1 2 3 4 5 6 7 8
AN EXAMPLE
N 8 did_swap true
to_do 7
index 7
23 45 14 6 67 33 98 42
1 2 3 4 5 6 7 8
AN EXAMPLE
N 8 did_swap true
to_do 7
index 7
Swap
23 45 14 6 67 33 98 42
1 2 3 4 5 6 7 8
AN EXAMPLE
N 8 did_swap true
to_do 7
index 7
Swap
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
to_do 7
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
to_do 6
index 1
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
to_do 6
index 1
No Swap
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
to_do 6
index 2
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
to_do 6
index 2
Swap
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
to_do 6
index 2
Swap
23 14 45 6 67 33 42 98
1 2 3 4 5 6 7 8
to_do 6
index 3
23 14 45 6 67 33 42 98
1 2 3 4 5 6 7 8
to_do 6
index 3
Swap
23 14 45 6 67 33 42 98
1 2 3 4 5 6 7 8
to_do 6
index 3
Swap
23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
to_do 6
index 4
23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
to_do 6
index 4
No Swap
23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
to_do 6
index 5
23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
to_do 6
index 5
Swap
23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
to_do 6
index 5
Swap
23 14 6 45 33 67 42 98
1 2 3 4 5 6 7 8
to_do 6
index 6
23 14 6 45 33 67 42 98
1 2 3 4 5 6 7 8
to_do 6
index 6
Swap
23 14 6 45 33 67 42 98
1 2 3 4 5 6 7 8
to_do 6
index 6
Swap
23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
to_do 6
23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
to_do 5
index 1
23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
to_do 5
index 1
Swap
23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
to_do 5
index 1
Swap
14 23 6 45 33 42 67 98
1 2 3 4 5 6 7 8
to_do 5
index 2
14 23 6 45 33 42 67 98
1 2 3 4 5 6 7 8
to_do 5
index 2
Swap
14 23 6 45 33 42 67 98
1 2 3 4 5 6 7 8
to_do 5
index 2
Swap
14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
to_do 5
index 3
14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
to_do 5
index 3
No Swap
14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
to_do 5
index 4
14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
to_do 5
index 4
Swap
14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
to_do 5
index 4
Swap
14 6 23 33 45 42 67 98
1 2 3 4 5 6 7 8
to_do 5
index 5
14 6 23 33 45 42 67 98
1 2 3 4 5 6 7 8
to_do 5
index 5
Swap
14 6 23 33 45 42 67 98
1 2 3 4 5 6 7 8
to_do 5
index 5
Swap
14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
to_do 5
14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
to_do 4
index 1
14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
to_do 4
index 1
Swap
14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
to_do 4
index 1
Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
to_do 4
index 2
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
to_do 4
index 2
No Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
to_do 4
index 3
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
to_do 4
index 3
No Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
to_do 4
index 4
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
to_do 4
index 4
No Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
to_do 4
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
to_do 3
index 1
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
to_do 3
index 1
No Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
to_do 3
index 2
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
to_do 3
index 2
No Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
to_do 3
index 3
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
to_do 3
index 3
No Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
to_do 3
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
FINISHED “EARLY”
N 8 did_swap false
to_do 3
We didn’t do any swapping,
index 4 so all of the other elements
must be correctly placed.
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
DR. NEEPA SHAH 106
if (flag==0) break;
}
SUMMARY
“Bubble Up” algorithm will move largest value to its correct location (to the right)
Repeat “Bubble Up” until all elements are correctly placed:
Maximum of N-1 times
Can finish early if no swapping occurs
We reduce the number of elements we compare each time one is correctly placed
SELECTION SORT
We repeatedly find the next largest (or smallest) element in the array and move it to its final position
in the sorted array.
We begin by selecting the largest element and moving it to the highest index position. We can do this
by swapping the element at the highest index and the largest element. The process stops when the
effective size of the array becomes 1
32 91 12 55 74 73 18
32 91 12 55 74 73 18
32 91 12 55 74 73 18
12 91 32 55 74 73 18
12 91 32 55 74 73 18
12 91 32 55 74 73 18
12 18 32 55 74 73 91
12 18 32 55 74 73 91
12 18 32 55 74 73 91
12 18 32 55 74 73 91
12 18 32 55 73 74 91
12 18 32 55 73 74 91
12 18 32 55 73 74 91
12 18 32 55 73 74 91
selectionSort(array, size)
repeat (size - 1) times
set the first unsorted element as the minimum
for each of the unsorted elements
if element < currentMinimum
set element as new minimum
swap minimum with first unsorted position
end selectionSort
INSERTION SORT
Insertion sort keeps making the left side of the array sorted until the whole array is sorted.
It sorts the values seen so far and repeatedly inserts unseen values in the array into the left
sorted array.
The insertion sort algorithm is the sort unknowingly used by most card players
69
38 45
60 60
66 45
66 79 47 13 74 36 21 94 22 57 16 29 81
3 4 7 12 14 14 20 21 33 38 10 55 9 23 28 16
temp
less than 10
10
3 4 7 10 12 14 14 20 21 33 38 55 9 23 28 16
sorted
2 8 4 9 3 6
2 8 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
2 3 4 8 9 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
2 3 4 8 9 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
2 3 4 8 9 6
2 3 4 6 8 9 done
DR. NEEPA SHAH 141
5 7 0 3 4 2 6 1 (0)
5 7 0 3 4 2 6 1 (0)
0 5 7 3 4 2 6 1 (2)
0 3 5 7 4 2 6 1 (2)
0 3 4 5 7 2 6 1 (2)
0 2 3 4 5 7 6 1 (4)
0 2 3 4 5 6 7 1 (1)
0 1 2 3 4 5 6 7 (6)
insertionSort(array)
mark first element as sorted
for each unsorted element X
‘hold' the element X
for j <- lastSortedIndex down to 0
if current element j > X
move sorted element to the right by 1
break loop and insert X here
end insertionSort
DR. NEEPA SHAH 145
void insertionSort(int[] a) {
for (int i = 1; i < SIZE; i++) {
int ai = a[i], j;
0 0 0
1 1 1
2 1 2
… … …
n-1 1 n-1
n-1 n(n-1)/2
If the sequence is nearly sorted, then insertion sort will run nearly in O(n) time.
DIVIDE-AND-CONQUER
MERGE-SORT
Algorithm:
Divide
Recur: Recursive sort sequences S1 and S2.
Conquer: Put back the elements into S by merging the sorted sequences S1 and S2 into a unique
sorted sequence.
MERGE-SORT EXAMPLE
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 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23
23
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23
23 98
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14
23 98
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14
23 98
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14
23 98 14
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14
23 98 14 45
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14
23 98 14 45
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14
23 98 14 45
14
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14
23 98 14 45
14 23
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14
23 98 14 45
14 23 45
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14
23 98 14 45
14 23 45 98
Merge
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
23 98 14 45
14 23 45 98
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
23 98 14 45
14 23 45 98
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
23 98 14 45
14 23 45 98 Merge
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
23 98 14 45 6
14 23 45 98 Merge
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
23 98 14 45 6 67
14 23 45 98 Merge
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
14 23 45 98
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
14 23 45 98 Merge
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
14 23 45 98 Merge
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
14 23 45 98 Merge
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
14 23 45 98
Merge
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
14 23 45 98 6
Merge
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
14 23 45 98 6 33
Merge
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
14 23 45 98 6 33 42
Merge
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
14 23 45 98 6 33 42 67
Merge
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
14 23 45 98 6 33 42 67
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
14 23 45 98 6 33 42 67
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
14 23 45 98 6 33 42 67
6 14
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
14 23 45 98 6 33 42 67
6 14 23
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
14 23 45 98 6 33 42 67
6 14 23 33
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
14 23 45 98 6 33 42 67
6 14 23 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
14 23 45 98 6 33 42 67
6 14 23 33 42 45
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
14 23 45 98 6 33 42 67
6 14 23 33 42 45 67
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
14 23 45 98 6 33 42 67
6 14 23 33 42 45 67 98
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
14 23 45 98 6 33 42 67
6 14 23 33 42 45 67 98
98 23 45 14 6 67 33 42
6 14 23 33 42 45 67 98
EXAMPLE
Overview:
Split array into two halves
Sort the left half (recursively)
Sort the right half (recursively)
Merge the two sorted halves
int m = (p + q) / 2;
sort(a, p, m);
sort(a, m+1, q);
merge (a, p, m , q);
}
MERGE FUNCTION
void merge(int arr[], int low, int mid, int high)
{
int i, m, k, l, temp[MAX];
l=low;
i=low;
m=mid+1;
while((l<=mid)&&(m<=high))
{
if(arr[l]<=arr[m])
{
temp[i]=arr[l];
l++;
}
else
{
temp[i]=arr[m];
m++;
}
i++;
}
for(k=low;k<=high;k++)
{
arr[k]=temp[k];
}
}
DR. NEEPA SHAH 197
At each level in the binary tree created for Merge Sort, there are n elements, with O(1) time spent at each
element
→ O(n) running time for processing one level
The height of the tree is O(log n)
QUICK-SORT
1) Divide : If the sequence S has 2 or more elements, select an element x from S to be your pivot. Any arbitrary
element, like the last, will do. Remove all the elements of S and divide them into 3 sequences:
L, holds S’s elements less than x
E, holds S’s elements equal to x
G, holds S’s elements greater than x
2) Recurse: Recursively sort L and G
3) Conquer: Finally, to put elements back into S in order, first inserts the elements of L, then those of E, and those of
G.
QUICK-SORT TREE
IN-PLACE QUICK-SORT
https://www.youtube.com/watch?v=-2VqW516BcI 203
int m = partition(a, p, q) ;
sort(a, p, m);
sort(a, m+1, q);
}
PARTITION ALGORITHM
int partition(int a[], int p, int q)
{
WHAT IS A “HEAP”?
A binary tree of depth n is balanced if all the nodes at depths 0 through n-2 have two children
n-2
n-1
n
Balanced Balanced Not balanced
or
it has 2k nodes at depth k, for all k < n, and all the leaves at depth n are as far left as possible
Top down
Bottom up
12 12 12
8 3 8 12 8 14
Blue node has heap Blue node has heap Blue node does not have
property property heap property
A node has the heap property if the value in the node is larger than the values in its children
All leaf nodes automatically have the heap property
A binary tree is a heap if all nodes in it have the heap property
SIFTUP
12 14
8 14 8 12
Blue node does not have Blue node has heap
heap property property
Given a node that does not have the heap property, you can give it the heap property
by exchanging its value with the value of the larger child
This is sometimes called sifting up
CONSTRUCTING A HEAP I
CONSTRUCTING A HEAP II
Each time we add a node, we may destroy the heap property of its parent node
To fix this, we sift up
But each time we sift up, the value of the topmost node in the sift may increase, and this may destroy the heap
property of its parent node
We repeat the sifting up process, moving up in the tree, until either
We reach nodes whose values don’t need to be swapped (because the parent is still larger than both children), or
We reach the root
10 8 8 5
1 2 3
10 10 12
8 5 12 5 10 5
12 8 8
4
DR. NEEPA SHAH 216
10 5 14 5 12 5
8 14 8 10 8 10
The node containing 8 is not affected because its parent gets larger, not smaller
The node containing 5 is not affected because its parent gets larger, not smaller
The node containing 8 is still not affected because, although its parent got smaller, its parent is still
greater than it was originally
A SAMPLE HEAP
Here’s a sample binary tree 25
after it has been heapified
22 17
Notice that heapified does not
mean sorted
Heapifying does not change the 19 22 14 15
shape of the binary tree; this
18 14 21 3 9 11
binary tree is balanced and left-
justified
22
Now the left child of the
11 17
root (still the number 11)
lacks the heap property
We can siftUp() this node 19 22 14 15
SORTING
22 17
19 22 14 15
18 14 21 3 9 11
0 1 2 3 4 5 6 7 8 9 10 11 12
25 22 17 19 22 14 15 18 14 21 3 9 11
The left child of index i is at index 2*i+1
DR. NEEPA SHAH The right child of index i is at index 2*i+2 225
0 1 2 3 4 5 6 7 8 9 10 11 12
11 22 17 19 22 14 15 18 14 21 3 9 25
0 1 2 3 4 5 6 7 8 9 10 11 12
22 22 17 19 21 14 15 18 14 11 3 9 25
0 1 2 3 4 5 6 7 8 9 10 11 12
9 22 17 19 22 14 15 18 14 21 3 22 25
Reheap the root node (index 0, containing 11) And again, remove and replace the root node
Remember, though, that the “last” array index is changed
DR. NEEPA SHAH 227
Repeat until the last becomes first, and the array is sorted!
ANALYSIS I
ANALYSIS II
We do the while loop n times (actually, n-1 times), because we remove one of the n nodes each time
Removing and replacing the root takes O(1) time
Therefore, the total time is n times however long it takes the reheap method
ANALYSIS III
To reheap the root node, we have to follow one path from the root to a leaf node (and we might stop before we
reach a leaf)
The binary tree is perfectly balanced
Therefore, this path is O(log n) long
And we only do O(1) operations at each node
Therefore, reheaping takes O(log n) times
Since we reheap inside a while loop that we do n times, the total time for the while loop is n*O(log n), or O(n
log n)
ANALYSIS IV
RADIX SORT
Radix sort uses bucket sort as the stable sorting algorithm, where the initial relative order of
equal keys is unchanged
RADIX SORT
ASCII (r = 26)
Decimal (r = 10)
Bit String (r = 2)
Octal (r = 8)
Hexadecimal (r = 16)
a b a b a c c a a a c b b a b c c a a a c b b a
a b a b a c c a a a c b b a b c c a a a c b b a
a b c
DR. NEEPA SHAH 237
a b a b a c c a a a c b b a b c c a a a c b b a
b b a Join piles.
c c a
c a a b a b a a c
a b a a c b b a c
a b c
DR. NEEPA SHAH 238
a b a c a a c c a b b a a c b b a b b a c a a c
a b c
DR. NEEPA SHAH 239
a b a c a a c c a b b a a c b b a b b a c a a c
a a c Join piles.
b a c
b a b b b a a c b
c a a a b a c c a
a b c
DR. NEEPA SHAH 240
c a a b a b b a c a a c a b a b b a c c a a c b
a b c
DR. NEEPA SHAH 241
c a a b a b b a c a a c a b a b b a c c a a c b
Join piles.
a c b b b a
a b a b a c c c a
a a c b a b c a a
a b c
DR. NEEPA SHAH 242
a a c a b a a c b b a b b a c b b a c a a c c a
RADIX-SORT CONTD.
Least-significant-digit-first
306, 208, 9, 33, 55, 859, 271, 179, 984, 93 (second pass) rear[9]
9, 33, 55, 93, 179, 208, 271, 306, 859, 984 (third pass) rear[9]
RADIX SORT
Algorithm
sort by the least significant digit first
=> 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
STEPS / PROCEDURE
3. For j=0 to 9 do
delete elements of Q[j] and place in next sequential position of queue x
ALGORITHM
// base 10
// FIFO