IT202-DS-Unit 1-Sorting-and-Searching
IT202-DS-Unit 1-Sorting-and-Searching
1
Sorting – Definition
Definition:
Arranging a set of members either in ascending or descending order.
If ‘A’ is an array of ‘N’ elements, then they are arranged in sorted order
(ascending order) such that A[0] < A[1] < A[2] < …. < A[N].
Example :
Array before sorting : A[5] = {34, 78, 22, 17, 90}
Array after sorting in ascending order: A[5] = {17, 22, 34, 78, 90}
Array after sorting in descending order: A[5] = {90,78, 34,22,17}
Classification of Sorting Techniques
1.1 BUBBLE SORT
❑In this sorting method, consecutive adjacent pairs of elements in
the array are compared and interchanged if they are not in order.
❑It takes (N-1) passes / iterations to sort the entire array.
❑In each pass,
❑The next largest element of the array sinks to its final position
❑Other small elements float or bubble up in the partially sorted list, hence the name
Bubble sort.
Exercise 1.1
Sort the following numbers using Bubble Sort
A[7] = {16, 36, 4, 22, 100, 1, 54}
Array A ↓
A[1] 16
A[2] 36
A[3] 4
A[4] 22
A[5] 100
A[6] 1
A[7] 54
Exercise 1.1
Sort the following numbers using Bubble Sort
A[7] = {16, 36, 4, 22, 100, 1, 54}
Iteration 1 – pair 1 compared – no swap required
Iteration 1
Array A ↓
Pair 1
A[1] 16 16
A[2] 36 36
A[3] 4 4
A[4] 22 22
A[5] 100 100
A[6] 1 1
A[7] 54 54
Exercise 1.1
Sort the following numbers using Bubble Sort
A[7] = {16, 36, 4, 22, 100, 1, 54}
Iteration 1 – pair 2 compared –swapping done
Iteration 1
Array A ↓
Pair 1 Pair 2
A[1] 16 16 16
A[2] 36 36 36
A[3] 4 4 4
A[4] 22 22 22
A[5] 100 100 100
A[6] 1 1 1
A[7] 54 54 54
Exercise 1.1
Sort the following numbers using Bubble Sort
A[7] = {16, 36, 4, 22, 100, 1, 54}
Iteration 1 – pair 3 compared – swapping done
Iteration 1
Array A ↓
Pair 1 Pair 2 Pair 3
A[1] 16 16 16 16
A[2] 36 36 36 4
A[3] 4 4 4 36
A[4] 22 22 22 22
A[5] 100 100 100 100
A[6] 1 1 1 1
A[7] 54 54 54 54
Exercise 1.1
Sort the following numbers using Bubble Sort
A[7] = {16, 36, 4, 22, 100, 1, 54}
Iteration 1 – pair 4 compared – no swap required
Iteration 1
Array A ↓
Pair 1 Pair 2 Pair 3 Pair 4
A[1] 16 16 16 16 16
A[2] 36 36 36 4 4
A[3] 4 4 4 36 22
A[4] 22 22 22 22 36
A[5] 100 100 100 100 100
A[6] 1 1 1 1 1
A[7] 54 54 54 54 54
Exercise 1.1
Sort the following numbers using Bubble Sort
A[7] = {16, 36, 4, 22, 100, 1, 54}
Iteration 1 – pair 5 compared – swapping done
Iteration 1
Array A ↓
Pair 1 Pair 2 Pair 3 Pair 4 Pair 5
A[1] 16 16 16 16 16 16
A[2] 36 36 36 4 4 4
A[3] 4 4 4 36 22 22
A[4] 22 22 22 22 36 36
A[5] 100 100 100 100 100 100
A[6] 1 1 1 1 1 1
A[7] 54 54 54 54 54 54
Exercise 1.1
Sort the following numbers using Bubble Sort
A[7] = {16, 36, 4, 22, 100, 1, 54}
Iteration 1 – pair 6 compared – swapping done
Iteration 1
Array A ↓
Pair 1 Pair 2 Pair 3 Pair 4 Pair 5 Pair 6
A[1] 16 16 16 16 16 16 16
A[2] 36 36 36 4 4 4 4
A[3] 4 4 4 36 22 22 22
A[4] 22 22 22 22 36 36 36
A[5] 100 100 100 100 100 100 1
A[6] 1 1 1 1 1 1 100
A[7] 54 54 54 54 54 54 54
Exercise 1.1
Sort the following numbers using Bubble Sort
A[7] = {16, 36, 4, 22, 100, 1, 54}
At the end of iteration 1 the largest of all elements is placed in last position
Iteration 1 Iteration
Array A ↓ 1 result
Pair 1 Pair 2 Pair 3 Pair 4 Pair 5 Pair 6
A[1] 16 16 16 16 16 16 16 16
A[2] 36 36 36 4 4 4 4 4
A[3] 4 4 4 36 22 22 22 22
A[4] 22 22 22 22 36 36 36 36
A[5] 100 100 100 100 100 100 1 1
A[6] 1 1 1 1 1 1 100 54
A[7] 54 54 54 54 54 54 54 100
Exercise 1.1
Sort the following numbers using Bubble Sort
16, 36, 4, 22, 100, 1, 54
At the end of pass 2 / iteration 2 - the 2nd largest element is placed in the last but one position
Iterations →
Pass 1 Pass 2
Array A ↓
A[1] 16 16 4
A[2] 36 4 16
A[3] 4 22 22
A[4] 22 36 1
A[5] 100 1 36
A[6] 1 54 54
A[7] 54 100 100
Exercise 1.1
Sort the following numbers using Bubble Sort
16, 36, 4, 22, 100, 1, 54
Iteration 3 brings third largest member to its final place
Iterations →
Pass 1 Pass 2 Pass 3
Array A ↓
A[1] 16 16 4 4
A[2] 36 4 16 16
A[3] 4 22 22 1
A[4] 22 36 1 22
A[5] 100 1 36 36
A[6] 1 54 54 54
A[7] 54 100 100 100
Exercise 1.1
Sort the following numbers using Bubble Sort
16, 36, 4, 22, 100, 1, 54
At the end of pass 4 / iteration 4 - the 4th largest element is placed in its final position
Iterations →
Pass 1 Pass 2 Pass 3 Pass 4
Array A ↓
A[1] 16 16 4 4 4
A[2] 36 4 16 16 1
A[3] 4 22 22 1 16
A[4] 22 36 1 22 22
A[5] 100 1 36 36 36
A[6] 1 54 54 54 54
A[7] 54 100 100 100 100
Exercise 1.1
Sort the following numbers using Bubble Sort
16, 36, 4, 22, 100, 1, 54
Iteration 5 brings 5th largest member to its final place
Iterations →
Pass 1 Pass 2 Pass 3 Pass 4 Pass 5
Array A ↓
A[1] 16 16 4 4 4 1
A[2] 36 4 16 16 1 4
A[3] 4 22 22 1 16 16
A[4] 22 36 1 22 22 22
A[5] 100 1 36 36 36 36
A[6] 1 54 54 54 54 54
A[7] 54 100 100 100 100 100
Exercise 1.1
Sort the following numbers using Bubble Sort
16, 36, 4, 22, 100, 1, 54 – sorting completes at (n-1) / 6th pass
Iterations →
Pass 1 Pass 2 Pass 3 Pass 4 Pass 5 Pass 6
Array A ↓
A[1] 16 16 4 4 4 1 1
A[2] 36 4 16 16 1 4 4
A[3] 4 22 22 1 16 16 16
A[4] 22 36 1 22 22 22 22
A[5] 100 1 36 36 36 36 36
A[6] 1 54 54 54 54 54 54
A[7] 54 100 100 100 100 100 100
Algorithm for Bubble Sort
Passes →
Pass 1
Array A ↓
A[1] 39 9
A[2] 9 39
A[3] 45
A[4] 63
A[5] 18
A[6] 81
A[7] 108
A[8] 54
A[9] 72
A[10] 36
Exercise 1.2
Sort the following numbers using Insertion Sort
39, 9, 45, 63, 18, 81, 108, 54, 72, 36
Iteration 2 – 3rd member inserted – upto 3rd position array members are sorted
Passes →
Pass 1 Pass 2
Array A ↓
A[1] 39 9 9
A[2] 9 39 39
A[3] 45 45
A[4] 63
A[5] 18
A[6] 81
A[7] 108
A[8] 54
A[9] 72
A[10] 36
Exercise 1.2
Sort the following numbers using Insertion Sort
39, 9, 45, 63, 18, 81, 108, 54, 72, 36
Iteration 3 – 4th member inserted – upto 4th position array members are sorted
Passes →
Pass 1 Pass 2 Pass 3
Array A ↓
A[1] 39 9 9 9
A[2] 9 39 39 39
A[3] 45 45 45
A[4] 63 63
A[5] 18
A[6] 81
A[7] 108
A[8] 54
A[9] 72
A[10] 36
Exercise 1.2
Sort the following numbers using Insertion Sort
39, 9, 45, 63, 18, 81, 108, 54, 72, 36
Iteration 4 – 5th member inserted – upto 5th position array members are sorted
Passes →
Pass 1 Pass 2 Pass 3 Pass 4
Array A ↓
A[1] 39 9 9 9 9
A[2] 9 39 39 39 18
A[3] 45 45 45 39
A[4] 63 63 45
A[5] 18 63
A[6] 81
A[7] 108
A[8] 54
A[9] 72
A[10] 36
Exercise 1.2
Sort the following numbers using Insertion Sort
39, 9, 45, 63, 18, 81, 108, 54, 72, 36
Pass 5 – 6th member inserted – upto 6th position array members are sorted
Passes →
Pass 1 Pass 2 Pass 3 Pass 4 Pass 5
Array A ↓
A[1] 39 9 9 9 9 9
A[2] 9 39 39 39 18 18
A[3] 45 45 45 39 39
A[4] 63 63 45 45
A[5] 18 63 63
A[6] 81 81
A[7] 108
A[8] 54
A[9] 72
A[10] 36
Exercise 1.2
Sort the following numbers using Insertion Sort
39, 9, 45, 63, 18, 81, 108, 54, 72, 36
Pass 6 – 7th member inserted – upto 7th position array members are sorted
Passes →
Pass 1 Pass 2 Pass 3 Pass 4 Pass 5 Pass 6
Array A ↓
A[1] 39 9 9 9 9 9 9
A[2] 9 39 39 39 18 18 18
A[3] 45 45 45 39 39 39
A[4] 63 63 45 45 45
A[5] 18 63 63 63
A[6] 81 81 81
A[7] 108 108
A[8] 54
A[9] 72
A[10] 36
Exercise 1.2
Sort the following numbers using Insertion Sort
39, 9, 45, 63, 18, 81, 108, 54, 72, 36
Pass 7 – 8th member inserted – upto 8th position array members are sorted
Passes →
Pass 1 Pass 2 Pass 3 Pass 4 Pass 5 Pass 6 Pass 7
Array A ↓
A[1] 39 9 9 9 9 9 9 9
A[2] 9 39 39 39 18 18 18 18
A[3] 45 45 45 39 39 39 39
A[4] 63 63 45 45 45 45
A[5] 18 63 63 63 54
A[6] 81 81 81 63
A[7] 108 108 81
A[8] 54 108
A[9] 72
A[10] 36
Exercise 1.2
Sort the following numbers using Insertion Sort
39, 9, 45, 63, 18, 81, 108, 54, 72, 36
Pass 8 – 9th member inserted – upto 9th position array members are sorted
Passes →
Pass 1 Pass 2 Pass 3 Pass 4 Pass 5 Pass 6 Pass 7 Pass 8
Array A ↓
A[1] 39 9 9 9 9 9 9 9 9
A[2] 9 39 39 39 18 18 18 18 18
A[3] 45 45 45 39 39 39 39 39
A[4] 63 63 45 45 45 45 45
A[5] 18 63 63 63 54 54
A[6] 81 81 81 63 63
A[7] 108 108 81 72
A[8] 54 108 81
A[9] 72 108
A[10] 36
Exercise 1.2
Sort the following numbers using Insertion Sort
39, 9, 45, 63, 18, 81, 108, 54, 72, 36
Pass 9 – 10th member inserted – upto 10th position array members are sorted
Passes →
Pass 1 Pass 2 Pass 3 Pass 4 Pass 5 Pass 6 Pass 7 Pass 8 Pass 9
Array A ↓
A[1] 39 9 9 9 9 9 9 9 9 9
A[2] 9 39 39 39 18 18 18 18 18 18
A[3] 45 45 45 39 39 39 39 39 36
A[4] 63 63 45 45 45 45 45 39
A[5] 18 63 63 63 54 54 45
A[6] 81 81 81 63 63 54
A[7] 108 108 81 72 63
A[8] 54 108 81 72
A[9] 72 108 81
A[10] 36 108
Note the un accessed data in the bottom left triangle, due to this the number of comparisons and swaps are
less in insertion sort compared to bubble sort
Algorithm for Insertion Sort
Passes →
Pass 1 Pass 2
Array A ↓
A[1] 35 10 10
A[2] 33 33 14
A[3] 42 42 42
A[4] 10 35 35
A[5] 14 14 33
A[6] 19 19 19
A[7] 26 26 26
A[8] 44 44 44
A[9] 25 25 25
A[10] 31 31 31
Exercise 1.3
Exercise 1.3 Sort the following numbers using Selection Sort
35, 33, 42, 10, 14, 19, 26, 44, 25, 31
Iteration 3 / pass 3 brings the 3rd least value to a[3] after swapping 42 to a[6]
Passes →
Pass 1 Pass 2 Pass 3
Array A ↓
A[1] 35 10 10 10
A[2] 33 33 14 14
A[3] 42 42 42 19
A[4] 10 35 35 35
A[5] 14 14 33 33
A[6] 19 19 19 42
A[7] 26 26 26 26
A[8] 44 44 44 44
A[9] 25 25 25 25
A[10] 31 31 31 31
Exercise 1.3
Exercise 1.3 Sort the following numbers using Selection Sort
35, 33, 42, 10, 14, 19, 26, 44, 25, 31
Iteration 4 / pass 4 brings the 4th least value to a[4] after swapping 35 to a[9]
Passes →
Pass 1 Pass 2 Pass 3 Pass 4
Array A ↓
A[1] 35 10 10 10 10
A[2] 33 33 14 14 14
A[3] 42 42 42 19 19
A[4] 10 35 35 35 25
A[5] 14 14 33 33 33
A[6] 19 19 19 42 42
A[7] 26 26 26 26 26
A[8] 44 44 44 44 44
A[9] 25 25 25 25 35
A[10] 31 31 31 31 31
Exercise 1.3
Exercise 1.3 Sort the following numbers using Selection Sort
35, 33, 42, 10, 14, 19, 26, 44, 25, 31
Iteration 5 / pass 5 brings the 5th least value to a[5] after swapping 33 to a[7]
Passes →
Pass 1 Pass 2 Pass 3 Pass 4 Pass 5
Array A ↓
A[1] 35 10 10 10 10 10
A[2] 33 33 14 14 14 14
A[3] 42 42 42 19 19 19
A[4] 10 35 35 35 25 25
A[5] 14 14 33 33 33 26
A[6] 19 19 19 42 42 42
A[7] 26 26 26 26 26 33
A[8] 44 44 44 44 44 44
A[9] 25 25 25 25 35 35
A[10] 31 31 31 31 31 31
Exercise 1.3
Exercise 1.3 Sort the following numbers using Selection Sort
35, 33, 42, 10, 14, 19, 26, 44, 25, 31
Iteration 6 / pass 6 brings the 6th least value to a[6] after swapping 42 to a[10]
Passes →
Pass 1 Pass 2 Pass 3 Pass 4 Pass 5 Pass 6
Array A ↓
A[1] 35 10 10 10 10 10 10
A[2] 33 33 14 14 14 14 14
A[3] 42 42 42 19 19 19 19
A[4] 10 35 35 35 25 25 25
A[5] 14 14 33 33 33 26 26
A[6] 19 19 19 42 42 42 31
A[7] 26 26 26 26 26 33 33
A[8] 44 44 44 44 44 44 44
A[9] 25 25 25 25 35 35 35
A[10] 31 31 31 31 31 31 42
Exercise 1.3
Exercise 1.3 Sort the following numbers using Selection Sort
35, 33, 42, 10, 14, 19, 26, 44, 25, 31
Iteration 7 / pass 7 brings the 7th least value to a[7] with no swapping
Passes →
Pass 1 Pass 2 Pass 3 Pass 4 Pass 5 Pass 6 Pass 7
Array A ↓
A[1] 35 10 10 10 10 10 10 10
A[2] 33 33 14 14 14 14 14 14
A[3] 42 42 42 19 19 19 19 19
A[4] 10 35 35 35 25 25 25 25
A[5] 14 14 33 33 33 26 26 26
A[6] 19 19 19 42 42 42 31 31
A[7] 26 26 26 26 26 33 33 33
A[8] 44 44 44 44 44 44 44 44
A[9] 25 25 25 25 35 35 35 35
A[10] 31 31 31 31 31 31 42 42
Exercise 1.3
Exercise 1.3 Sort the following numbers using Selection Sort
35, 33, 42, 10, 14, 19, 26, 44, 25, 31
Iteration 8 / pass 8 brings the 8th least value to a[8] after swapping 44 to a[9]
Passes →
Pass 1 Pass 2 Pass 3 Pass 4 Pass 5 Pass 6 Pass 7 Pass 8
Array A ↓
A[1] 35 10 10 10 10 10 10 10 10
A[2] 33 33 14 14 14 14 14 14 14
A[3] 42 42 42 19 19 19 19 19 19
A[4] 10 35 35 35 25 25 25 25 25
A[5] 14 14 33 33 33 26 26 26 26
A[6] 19 19 19 42 42 42 31 31 31
A[7] 26 26 26 26 26 33 33 33 33
A[8] 44 44 44 44 44 44 44 44 35
A[9] 25 25 25 25 35 35 35 35 44
A[10] 31 31 31 31 31 31 42 42 42
Exercise 1.3
Exercise 1.3 Sort the following numbers using Selection Sort
35, 33, 42, 10, 14, 19, 26, 44, 25, 31
Iteration 9/ pass 9 brings the 9th least value to a[9] after swapping 42 to a[10]
Passes →
Pass 1 Pass 2 Pass 3 Pass 4 Pass 5 Pass 6 Pass 7 Pass 8 Pass 9
Array A ↓
A[1] 35 10 10 10 10 10 10 10 10 10
A[2] 33 33 14 14 14 14 14 14 14 14
A[3] 42 42 42 19 19 19 19 19 19 19
A[4] 10 35 35 35 25 25 25 25 25 25
A[5] 14 14 33 33 33 26 26 26 26 26
A[6] 19 19 19 42 42 42 31 31 31 31
A[7] 26 26 26 26 26 33 33 33 33 33
A[8] 44 44 44 44 44 44 44 44 35 35
A[9] 25 25 25 25 35 35 35 35 44 42
A[10] 31 31 31 31 31 31 42 42 42 44
SELECTION SORT
Note:
❑Selection sort is easy to implement, but not better than Insertion
sort in certain cases.
❑The time complexity of the algorithm is O(N2).
❑Selection sort requires maximum of n-1 exchanges
1.4 RADIX SORT
❑It is also named as Bucket Sort.
❑The data in the array are repeatedly distributed into groups based
on the digits or the characters forming the data.
❑In general, the distribution proceeds from Least Significant Digit
(LSD) onwards and progresses digit after digit upto Most Significant
Digit(MSD).
Exercise 1.4
Sort the following numbers using Radix Sort
387, 690, 234, 435, 567, 123, 441, 702, 826, 999
n= 10; Radix r = 10; No. of Digits=3
Pass 1 of Radix Sort – LSD based processing of each data (separate the last digit – and in its respective bucket, place the data)
Bucket 0
Bucket 1
Bucket 2
Bucket 3
Bucket 4
Bucket 5
Bucket 6
Bucket 7 387 First data member placed in 7th bucket
Bucket 8
Bucket 9
Exercise 1.4
Sort the following numbers using Radix Sort
387, 690, 234, 435, 567, 123, 441, 702, 826, 999
n= 10; Radix r = 10; No. of Digits=3
Pass 1 of Radix Sort – LSD based processing of each data (separate the last digit – and in its respective bucket, place the data)
Bucket 0 690
Bucket 1
Bucket 2
Bucket 3
Bucket 4 234 3rd data member placed in 4th bucket
Bucket 5
Bucket 6
Bucket 7 387
Bucket 8
Bucket 9
Exercise 1.4
Sort the following numbers using Radix Sort
387, 690, 234, 435, 567, 123, 441, 702, 826, 999
n= 10; Radix r = 10; No. of Digits=3
Pass 1 of Radix Sort – LSD based processing of each data (separate the last digit – and in its respective bucket, place the data)
Bucket 0 690
Bucket 1
Bucket 2
Bucket 3
Bucket 4 234
Bucket 5 435 4th data member placed in 5h bucket
Bucket 6
Bucket 7 387
Bucket 8
Bucket 9
Exercise 1.4
Sort the following numbers using Radix Sort
387, 690, 234, 435, 567, 123, 441, 702, 826, 999
n= 10; Radix r = 10; No. of Digits=3
Pass 1 of Radix Sort – LSD based processing of each data (separate the last digit – and in its respective bucket, place the data)
Bucket 0 690
Bucket 1
Bucket 2
Bucket 3
Bucket 4 234
Bucket 5 435
Bucket 6
Bucket 7 387 567 5th data member placed in 7h bucket
Bucket 8
Bucket 9
Exercise 1.4
Sort the following numbers using Radix Sort
387, 690, 234, 435, 567, 123, 441, 702, 826, 999
n= 10; Radix r = 10; No. of Digits=3
Pass 1 of Radix Sort – LSD based processing of each data (separate the last digit – and in its respective bucket, place the data)
Bucket 0 690
Bucket 1
Bucket 2
Bucket 3 123 6th data member placed in 3rd bucket
Bucket 4 234
Bucket 5 435
Bucket 6
Bucket 7 387 567
Bucket 8
Bucket 9
Exercise 1.4
Sort the following numbers using Radix Sort
387, 690, 234, 435, 567, 123, 441, 702, 826, 999
n= 10; Radix r = 10; No. of Digits=3
Pass 1 of Radix Sort – LSD based processing of each data (separate the last digit – and in its respective bucket, place the data)
Bucket 0 690
Bucket 1 441 7th data member placed in 1st bucket
Bucket 2
Bucket 3 123
Bucket 4 234
Bucket 5 435
Bucket 6
Bucket 7 387 567
Bucket 8
Bucket 9
Exercise 1.4
Sort the following numbers using Radix Sort
387, 690, 234, 435, 567, 123, 441, 702, 826, 999
n= 10; Radix r = 10; No. of Digits=3
Pass 1 of Radix Sort – LSD based processing of each data (separate the last digit – and in its respective bucket, place the data)
Bucket 0 690
Bucket 1 441
Bucket 2 702 8th data member placed in 2nd bucket
Bucket 3 123
Bucket 4 234
Bucket 5 435
Bucket 6
Bucket 7 387 567
Bucket 8
Bucket 9
Exercise 1.4
Sort the following numbers using Radix Sort
387, 690, 234, 435, 567, 123, 441, 702, 826, 999
n= 10; Radix r = 10; No. of Digits=3
Pass 1 of Radix Sort – LSD based processing of each data (separate the last digit – and in its respective bucket, place the data)
Bucket 0 690
Bucket 1 441
Bucket 2 702
Bucket 3 123
Bucket 4 234
Bucket 5 435
Bucket 6 826 9th data member placed in 6th bucket
Bucket 7 387 567
Bucket 8
Bucket 9
Exercise 1.4
Sort the following numbers using Radix Sort
387, 690, 234, 435, 567, 123, 441, 702, 826, 999
n= 10; Radix r = 10; No. of Digits=3
Pass 1 of Radix Sort – LSD based processing of each data (separate the last digit – and in its respective bucket, place the data)
Bucket 0 690
Bucket 1 441
Bucket 2 702
Bucket 3 123
Bucket 4 234
Bucket 5 435
Bucket 6 826
Bucket 7 387 567
Bucket 8
Bucket 9 999 10th data member placed in 9th bucket
Exercise 1.4
Sort the following numbers using Radix Sort
387, 690, 234, 435, 567, 123, 441, 702, 826, 999
n= 10; Radix r = 10; No. of Digits=3
Pass 1 of Radix Sort – completion – collect the data from bucket 0 to bucket 9 (left to right) in order in to the resulting array
Bucket 0 690
Bucket 1 441
Bucket 2 702
Bucket 3 123
Bucket 4 234
Bucket 5 435
Bucket 6 826
Bucket 7 387 567
Bucket 8
Bucket 9 999
Array after Pass1: 690, 441, 702, 123, 234, 435, 826, 387, 567, 999
Exercise 1.4
Pass 1 Results: 690, 441, 702, 123, 234, 435, 826, 387, 567, 999
Pass 2 – of Radix Sort – 2nd Significant Digit based processing
Bucket 0 702
Bucket 1
Bucket 2 123 826
Bucket 3 234 435
Bucket 4 441
Bucket 5
Bucket 6 567
Bucket 7
Bucket 8 387
Bucket 9 690 999
Pass 2 Results: 702, 123, 826, 234, 435, 441, 567, 387, 690, 999
Exercise 1.4
Pass 2 Results: 702, 123, 826, 234, 435, 441, 567, 387, 690, 999
Pass 3 of Radix Sort – MSD based processing
Bucket 0
Bucket 1 123
Bucket 2 234
Bucket 3 387
Bucket 4 435 441
Bucket 5 567
Bucket 6 690
Bucket 7 702
Bucket 8 826
Bucket 9 999
Pass 3 (Final) Results: 123, 234, 387, 435, 441, 567, 690, 702, 826, 999
Radix Sorting
Note:
❑The time complexity of the radix sort algorithm is O(k×n).
❑k is the number of digits in the largest data and n is the number of data
a[2] a[3]
a[8] = {2, 4, 7, 8, 16, 9, 13, 11}
a[4]
a[5] a[6] a[7]
a[8]
- In every triangle subtree, maximum value brought up to the parent by swapping, if required with the highest valued child.
- The swapped value to the child will be adjusted with the below triangles if required to maintain maxheap
Identify Max Heaps from the following trees A, B, C, D
Identify Min Heaps from the following trees
Heapsort - Stepwise
Heapsort –steps
1.MaxHeap
2.Swap with last member
3.Adjust heap due to root swap or change
4. Detach last member
5.Repeat steps 1 to 4
Heapsort – Exercise 1.5
2 2 8
3 7 8 7 2 7
1 8 5 6 1 3 5 6 1 3 5 6
8 6 7
3 7 3 7 3 6
5 6 1 2 5 8 5 8
1 2 1 2
Input : (2, 3, 7, 1, 8, 5, 6)
After Max Heap: (8, 3, 7, 1, 2, 5, 6)
After swap and detach: (6, 3, 7, 1, 2, 5, 8)
After Adjust : (7, 3, 6, 1, 2, 5, 8)
Heapsort – Exercise 1.5
2 2 8
3 7 8 7 2 7
6 1 3 5 6 1 3 5 6
1 8 5
Max Heapify a[7] : (2, 8, 7, 1, 3, 5, 6) Max Heapify a[7]: (8, 2, 7, 1, 3, 5, 6)
Input a[7]: (2, 3, 7, 1, 8, 5, 6)
8 6 7
3 7 3 7 3 6
1 2 5 8 1 2 5 8
1 2 5 6 Iteration1 result a[7]: (7, 3, 6, 1, 2, 5, 8)
Swap & Detach largest a[7]: (6, 3, 7, 1, 2, 5, 8)
Max Heap and adjust a[7]: (8, 3, 7, 1, 2, 5, 6)
Heapsort – Exercise 1.5
Exercise 1.5
Sort the following numbers using Heap Sort
A[11] = {11, 1, 5, 7, 6, 12, 17, 8, 4, 10, 2}
Tree representation of the Data Completion of Heapify and swap
(before Pass 1) (After Pass 1)
11
2
/ \
/ \
10 5
10 11
/ \ / \ / \ / \
7 6 12 17 8 1 12 5
/ \ / \ / \ / \
8 4 10 2 7 4 6 17
Iterationwise Heapsort results
Passes →
Pass 1 Pass 2 Pass 3 Pass 4 Pass 5 Pass 6 Pass 7 Pass 8 Pass 9 Pass 10
Array A ↓
A[1] 11 2 1 4 7 1 2 1 2 1 1
A[2] 1 10 10 10 4 7 1 2 4 2 2
A[3] 5 11 2 1 5 5 5 5 1 4 4
A[4] 7 8 8 8 8 4 4 4 5 5 5
A[5] 6 1 6 6 6 6 6 6 6 6 6
A[6] 12 12 11 2 2 2 7 7 7 7 7
A[7] 17 5 5 5 1 8 8 8 8 8 8
A[8] 8 7 7 7 10 10 10 10 10 10 10
A[9] 4 4 4 11 11 11 11 11 11 11 11
A[10] 10 6 12 12 12 12 12 12 12 12 12
A[11] 2 17 17 17 17 17 17 17 17 17 17
Algorithm for Heap Sort
Heap-Sort(A, N)
heapify(A, I, N)
Step 1: buildHeap(A)
Step 1: left = 2*I + 1 and right = 2*I + 2
Step 2: FOR I = N downto 2 do
Step 2: IF (left <= N and A[left] > A[I])
Step 3: SWAP A[1] with A[I]
Step 3: largest = left
Step 4: heapify(A, 0, I)
Step 4: ELSE largest = I
Step 7: ENDFOR
Step 5: IF (right < max and A[right] > A[largest])
Step 8: END
Step 6: largest = right
Step 7: IF (largest != I)
buildHeap(A)
Step 8: SWAP A[I] and A[largest]
Step 1: FOR I = N/2 downto 1 do
Step 9: heapify(A, largest, max)
Step 2: heapify(A, I, N)
Step 10: END
Step 3: END
Heapsort
Note:
❑The complexity of Heapsort = O(n×logn).
❑It is a simple, fast and stable sorting algorithm.
❑It can be used for large set of data efficiently.
1.6 MERGE SORT
❑It uses divide and conquer strategy.
❑The objective of sorting is achieved by dividing the input array into
smaller partitions recursively and then merging the partitions to
order the data.
Exercise 1.6
Sort the following numbers using Merge Sort
A[8] = {12, 56, 1, 34, 89, 78, 43, 10}
Tree of recursive partitioning (Top to bottom)
Mergesort(A,1,8)
[12,56,1,34,89,78,43,10]
Mergesort(A,1,4) Mergesort(A,5,8)
[12,56,1,34] [89,78,43,10]
Merge(A,1,2,4) Merge(A,5,6,8)
[1,12,34,56] [10,43,78,89]
A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] A[10] A[11]
11 1 5 7 6 12 17 8 4 10 2
A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] A[10] A[11]
11 1 5 7 6 12 17 8 4 10 2
Exercise 1.6
Pass 1 (pair 3 from 2 partitions) - Sort every 6th element
Increment / gap_size = (11+1) / 2 = 6
A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] A[10] A[11]
11 1 5 7 6 12 17 8 4 10 2
Pass 1 (6th member in partition1 – no pair in partition 2 – no change) - Sort every 6th
element
Increment / gap_size = (11+1) / 2 = 6
A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] A[10] A[11]
11 1 4 7 2 12 17 8 5 10 6
Exercise 1.6
Pass 1 result
Increment / gap_size = (11+1) / 2 = 6
A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] A[10] A[11]
Before 11 1 5 7 6 12 17 8 4 10 2
After 11 1 4 7 2 12 17 8 5 10 6
Pass 1 Outcome A[11] = 11, 1, 4, 7, 2, 12, 17, 8, 5, 10, 6
Exercise 1.6
Pass 2 – input – Sort every 3rd element
A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] A[10] A[11]
11 1 4 7 2 12 17 8 5 10 6
Increment / Gap size = (6+1) / 2 = 3 (no. of members in each partition)
4 partitions – first members sorted in order
A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] A[10] A[11]
11 1 4 7 2 12 17 8 5 10 6
7 1 4 10 2 12 11 8 5 17 6
Exercise 1.6
Pass 2 - input
A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] A[10] A[11]
11 1 4 7 2 12 17 8 5 10 6
A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] A[10] A[11]
7 1 4 10 2 12 11 8 5 17 6
7 1 4 10 2 12 11 6 5 17 8
Exercise 1.6
Pass 2 - input
A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] A[10] A[11]
11 1 4 7 2 12 17 8 5 10 6
Increment / Gap size = (6+1) / 2 = 3 - (4 partitions – third members sorted in order)
A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] A[10] A[11]
7 1 4 10 2 12 11 6 5 17 8
7 1 4 10 2 5 11 6 12 17 8
Pass 2 Outcome A[11] = 7, 1, 4, 10, 2, 5, 11, 6, 12, 17, 8
Exercise 1.6
Pass 3 - input
A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] A[10] A[11]
7 1 4 10 2 5 11 6 12 17 8
Increment / Gap size = (3+1) / 2 = 2 (6 partitions each with 2 members except last with one
member)
First member in each partition sorted in order
A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] A[10] A[11]
7 1 4 10 2 5 11 6 12 17 8
2 1 4 10 7 5 8 6 11 17 12
Second member in each partition sorted in order
2 1 4 10 7 5 8 6 11 17 12
2 1 4 5 7 6 8 10 11 17 12
100
Quick Sort – Partitioning algorithm
Step 1 − Choose the first indexed value as pivot
Step 2 − Take two variables to point left and right of the list excluding pivot
Step 3 − left points to the low index
Step 4 − right points to the high index
Step 5 − while value at left is less than pivot move right
Step 6 − while value at right is greater than pivot move left
Step 7 − if both step 5 and step 6 does not match swap left and right
Step 8 − if left ≥ right, the point where they met is new pivot
101
Partitioning - example
102
1.8. Quick Sort
Note:
❑This algorithm is quite efficient for large-sized data sets.
❑Its average and worst-case complexity are O(nLogn) and O(n2),
respectively
Comparison of sorting algorithms
Linear search
❑A linear search sequentially checks each element of the list until it
finds an element that matches the target value.
❑If the algorithm reaches the end of the list, the search terminates
unsuccessfully
Linear Search Algorithm
Linear search for an element in an array of N elements :
• STEP 1 : Start from the first element of array and one by one compare the query element with each element
of the array.
• STEP 2 : If the query element matches with an element, return the index.
• STEP 3 : If the query element doesn’t match with any of elements, return failure.
106
Linear Search Algorithm
Successful Search:
Un Successful Search:
107
Binary Search - Algorithm
❑ The idea of binary search is to use the information that the array is sorted
and reduce the time complexity to O(Log n)
❑ Algorithm steps
❑Sort the array elements.
❑Search a sorted array by repeatedly dividing the search interval in half.
❑Begin with an interval covering the whole array.
❑If the value of the search key is less than the item in the middle of the interval, narrow the interval to the
lower half.
❑Otherwise narrow it to the upper half. Repeatedly check until the value is found or the interval is empty
108
Binary Search - Example
109
Linear Search Vs Binary Search
110