Analysis and Design of Algorithms
Sorting Algorithms II
Slide 2
Slide 2 text
Analysis and Design of Algorithms
Counting Sort
Radix Sort
Merge Sort
Slide 3
Slide 3 text
Analysis and Design of Algorithms
Counting Sort
Slide 4
Slide 4 text
Analysis and Design of Algorithms
Counting sort is a sorting technique based on keys
between a specific range. It works by counting the
number of objects having distinct key values (kind of
hashing). Then doing some arithmetic to calculate
the position of each object in the output sequence.
Slide 5
Slide 5 text
Analysis and Design of Algorithms
Algorithm:
Step1: Create a count array to store the count of each unique
object
Step2 : Modify count array by adding the previous number.
Step3 : Create output array by decrease count array
Slide 6
Slide 6 text
Analysis and Design of Algorithms
Example 1 Assume the following Array in range of 0 to 5:
1 4 3 2 3 5 2
Slide 7
Slide 7 text
Analysis and Design of Algorithms
Create a count array to store the count of each unique object:
1 4 3 2 3 5 2
Slide 8
Slide 8 text
Analysis and Design of Algorithms
Create a count array to store the count of each unique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 0 0 0 0
Slide 9
Slide 9 text
Analysis and Design of Algorithms
Create a count array to store the count of each unique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 0 0 0 0
Slide 10
Slide 10 text
Analysis and Design of Algorithms
Create a count array to store the count of each unique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 0 0 0 0
Slide 11
Slide 11 text
Analysis and Design of Algorithms
Create a count array to store the count of each unique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 0 0 0 0
Slide 12
Slide 12 text
Analysis and Design of Algorithms
Create a count array to store the count of each unique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 0 0 0 0
Slide 13
Slide 13 text
Analysis and Design of Algorithms
Create a count array to store the count of each unique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 0 0 0 0
Slide 14
Slide 14 text
Analysis and Design of Algorithms
Create a count array to store the count of each unique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 0 0 0 0
Slide 15
Slide 15 text
Analysis and Design of Algorithms
Create a count array to store the count of each unique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 0 0 1 0
Slide 16
Slide 16 text
Analysis and Design of Algorithms
Create a count array to store the count of each unique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 0 0 1 0
Slide 17
Slide 17 text
Analysis and Design of Algorithms
Create a count array to store the count of each unique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 0 0 1 0
Slide 18
Slide 18 text
Analysis and Design of Algorithms
Create a count array to store the count of each unique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 0 1 1 0
Slide 19
Slide 19 text
Analysis and Design of Algorithms
Create a count array to store the count of each unique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 0 1 1 0
Slide 20
Slide 20 text
Analysis and Design of Algorithms
Create a count array to store the count of each unique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 0 1 1 0
Slide 21
Slide 21 text
Analysis and Design of Algorithms
Create a count array to store the count of each unique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 1 1 1 0
Slide 22
Slide 22 text
Analysis and Design of Algorithms
Create a count array to store the count of each unique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 1 1 1 0
Slide 23
Slide 23 text
Analysis and Design of Algorithms
Create a count array to store the count of each unique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 1 2 1 0
Slide 24
Slide 24 text
Analysis and Design of Algorithms
Create a count array to store the count of each unique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 1 2 1 0
Slide 25
Slide 25 text
Analysis and Design of Algorithms
Create a count array to store the count of each unique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 1 2 1 0
Slide 26
Slide 26 text
Analysis and Design of Algorithms
Create a count array to store the count of each unique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 1 2 1 1
Slide 27
Slide 27 text
Analysis and Design of Algorithms
Create a count array to store the count of each unique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 1 2 1 1
Slide 28
Slide 28 text
Analysis and Design of Algorithms
Create a count array to store the count of each unique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 1 2 1 1
Slide 29
Slide 29 text
Analysis and Design of Algorithms
Create a count array to store the count of each unique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 2 2 1 1
Slide 30
Slide 30 text
Analysis and Design of Algorithms
Modify count array by adding the previous number :
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 2 2 1 1
Slide 31
Slide 31 text
Analysis and Design of Algorithms
Modify count array by adding the previous number :
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 2 2 1 1
Slide 32
Slide 32 text
Analysis and Design of Algorithms
Modify count array by adding the previous number :
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 3 2 1 1
Slide 33
Slide 33 text
Analysis and Design of Algorithms
Modify count array by adding the previous number :
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 3 5 1 1
Slide 34
Slide 34 text
Analysis and Design of Algorithms
Modify count array by adding the previous number :
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 3 5 6 1
Slide 35
Slide 35 text
Analysis and Design of Algorithms
Modify count array by adding the previous number :
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 3 5 6 7
Slide 36
Slide 36 text
Analysis and Design of Algorithms
Output each object from the input sequence followed by
decreasing its count by 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 3 5 6 7
Slide 37
Slide 37 text
Analysis and Design of Algorithms
Output each object from the input sequence followed by
decreasing its count by 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 3 5 6 7
1 2 3 4 5 6 7
Slide 38
Slide 38 text
Analysis and Design of Algorithms
Output each object from the input sequence followed by
decreasing its count by 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 3 5 6 7
1 2 3 4 5 6 7
Slide 39
Slide 39 text
Analysis and Design of Algorithms
Output each object from the input sequence followed by
decreasing its count by 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 3 5 6 7
1 2 3 4 5 6 7
Slide 40
Slide 40 text
Analysis and Design of Algorithms
Output each object from the input sequence followed by
decreasing its count by 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 3 5 6 7
1 2 3 4 5 6 7
1
Slide 41
Slide 41 text
Analysis and Design of Algorithms
Output each object from the input sequence followed by
decreasing its count by 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 3 5 6 7
1 2 3 4 5 6 7
1
Slide 42
Slide 42 text
Analysis and Design of Algorithms
Output each object from the input sequence followed by
decreasing its count by 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 3 5 6 7
1 2 3 4 5 6 7
1
Slide 43
Slide 43 text
Analysis and Design of Algorithms
Output each object from the input sequence followed by
decreasing its count by 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 3 5 5 7
1 2 3 4 5 6 7
1 4
Slide 44
Slide 44 text
Analysis and Design of Algorithms
Output each object from the input sequence followed by
decreasing its count by 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 3 5 5 7
1 2 3 4 5 6 7
1 4
Slide 45
Slide 45 text
Analysis and Design of Algorithms
Output each object from the input sequence followed by
decreasing its count by 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 3 5 5 7
1 2 3 4 5 6 7
1 4
Slide 46
Slide 46 text
Analysis and Design of Algorithms
Output each object from the input sequence followed by
decreasing its count by 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 3 5 5 7
1 2 3 4 5 6 7
1 3 4
Slide 47
Slide 47 text
Analysis and Design of Algorithms
Output each object from the input sequence followed by
decreasing its count by 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 3 4 5 7
1 2 3 4 5 6 7
1 3 4
Slide 48
Slide 48 text
Analysis and Design of Algorithms
Output each object from the input sequence followed by
decreasing its count by 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 3 4 5 7
1 2 3 4 5 6 7
1 3 4
Slide 49
Slide 49 text
Analysis and Design of Algorithms
Output each object from the input sequence followed by
decreasing its count by 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 3 4 5 7
1 2 3 4 5 6 7
1 3 4
Slide 50
Slide 50 text
Analysis and Design of Algorithms
Output each object from the input sequence followed by
decreasing its count by 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 3 4 5 7
1 2 3 4 5 6 7
1 2 3 4
Slide 51
Slide 51 text
Analysis and Design of Algorithms
Output each object from the input sequence followed by
decreasing its count by 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 2 4 5 7
1 2 3 4 5 6 7
1 2 3 4
Slide 52
Slide 52 text
Analysis and Design of Algorithms
Output each object from the input sequence followed by
decreasing its count by 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 2 4 5 7
1 2 3 4 5 6 7
1 2 3 4
Slide 53
Slide 53 text
Analysis and Design of Algorithms
Output each object from the input sequence followed by
decreasing its count by 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 2 4 5 7
1 2 3 4 5 6 7
1 2 3 4
Slide 54
Slide 54 text
Analysis and Design of Algorithms
Output each object from the input sequence followed by
decreasing its count by 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 2 4 5 7
1 2 3 4 5 6 7
1 2 3 3 4
Slide 55
Slide 55 text
Analysis and Design of Algorithms
Output each object from the input sequence followed by
decreasing its count by 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 2 3 5 7
1 2 3 4 5 6 7
1 2 3 3 4
Slide 56
Slide 56 text
Analysis and Design of Algorithms
Output each object from the input sequence followed by
decreasing its count by 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 2 3 5 7
1 2 3 4 5 6 7
1 2 3 3 4
Slide 57
Slide 57 text
Analysis and Design of Algorithms
Output each object from the input sequence followed by
decreasing its count by 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 2 3 5 7
1 2 3 4 5 6 7
1 2 3 3 4
Slide 58
Slide 58 text
Analysis and Design of Algorithms
Output each object from the input sequence followed by
decreasing its count by 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 2 3 5 7
1 2 3 4 5 6 7
1 2 3 3 4 5
Slide 59
Slide 59 text
Analysis and Design of Algorithms
Output each object from the input sequence followed by
decreasing its count by 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 2 3 5 6
1 2 3 4 5 6 7
1 2 3 3 4 5
Slide 60
Slide 60 text
Analysis and Design of Algorithms
Output each object from the input sequence followed by
decreasing its count by 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 2 3 5 6
1 2 3 4 5 6 7
1 2 3 3 4 5
Slide 61
Slide 61 text
Analysis and Design of Algorithms
Output each object from the input sequence followed by
decreasing its count by 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 2 3 5 6
1 2 3 4 5 6 7
1 2 3 3 4 5
Slide 62
Slide 62 text
Analysis and Design of Algorithms
Output each object from the input sequence followed by
decreasing its count by 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 2 3 5 6
1 2 3 4 5 6 7
1 2 2 3 3 4 5
Slide 63
Slide 63 text
Analysis and Design of Algorithms
Output each object from the input sequence followed by
decreasing its count by 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 1 3 5 6
1 2 3 4 5 6 7
1 2 2 3 3 4 5
Slide 64
Slide 64 text
Analysis and Design of Algorithms
Output each object from the input sequence followed by
decreasing its count by 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 1 3 5 6
1 2 3 4 5 6 7
1 2 2 3 3 4 5
Slide 65
Slide 65 text
Analysis and Design of Algorithms
Array is now sorted
1 2 2 3 3 4 5
Analysis and Design of Algorithms
Time Complexity: O(n+k) where n is the number of elements in input
array, and k is the range of input.
Example of worst case
Range between 1 to 10K
10 5 10k 5k 200
Slide 71
Slide 71 text
Analysis and Design of Algorithms
Radix Sort
Slide 72
Slide 72 text
Analysis and Design of Algorithms
Radix sort is an algorithm that sorts numbers by processing digits
of each number either starting from the least significant digit (LSD)
or starting from the most significant digit (MSD).
The idea of Radix Sort is to do digit by digit sort starting from
least significant digit to most significant digit. Radix sort uses
counting sort as a subroutine to sort.
Slide 73
Slide 73 text
Analysis and Design of Algorithms
Algorithm:
Step1: Take the least significant digit of each element
Step2 : Sort the list of elements based on that digit
Step3 : Repeat the sort with each more significant digit
Slide 74
Slide 74 text
Analysis and Design of Algorithms
Assume the following Array:
170 45 75 90 802 24 2 66
Slide 75
Slide 75 text
Analysis and Design of Algorithms
The Sorted list will appear after three steps
170 45 75 90 802 24 2 66
170 90 802 2 24 45 75 66
802 2 24 45 66 170 75 90
2 24 45 66 75 90 170 802
Slide 76
Slide 76 text
Analysis and Design of Algorithms
Step1: Sorting by least significant digit (1s place)
170 45 75 90 802 24 2 66
170 90 802 2 24 45 75 66
Slide 77
Slide 77 text
Analysis and Design of Algorithms
Step2: Sorting by next digit (10s place)
170 90 802 2 24 45 75 66
802 2 24 45 66 170 75 90
Slide 78
Slide 78 text
Analysis and Design of Algorithms
Step3: Sorting by most significant digit (100s place)
802 2 24 45 66 170 75 90
2 24 45 66 75 90 170 802
Slide 79
Slide 79 text
Analysis and Design of Algorithms
Array is now sorted
2 24 45 66 75 90 170 802
Analysis and Design of Algorithms
Time Complexity: O(n+k/d) where n is the number
of elements in input array, k is the range of input,
and d is number of digits.
Slide 85
Slide 85 text
Analysis and Design of Algorithms
Merge Sort
Slide 86
Slide 86 text
Analysis and Design of Algorithms
Merge Sort is a Divide and Conquer algorithm. It
divides input array in two halves, calls itself for the
two halves and then merges the two sorted halves.
Slide 87
Slide 87 text
Analysis and Design of Algorithms
Algorithm:
Step1: Divide the list recursively into two halves until it can
no more be divided
Step2 : Merge (Conquer) the smaller lists into new list in
sorted order
Slide 88
Slide 88 text
Analysis and Design of Algorithms
Assume the following Array:
85 24 63 45 17 31 96 50