4Selection Sort
4Selection Sort
Sorting
• Sorting is a process in which records are arranged in
ascending or descending order
1 2 3 4 5 6
77 42 35 12 101 5
1 2 3 4 5 6
5 12 35 42 77 101
- Picks the smallest element in the array and brings it in the 1st
position; then picks the next smallest and puts it in the 2nd
position and so on
• Takes more time on the list sorted in descending order (worst possible case)
• Takes less time on the list sorted in ascending order (best possible case)
• Average time & worst-case time is quadratic Ο(n2)
Algorithm Selection Sort
How Selection Sort Work
Example: Selection Sort
• 26 33 43 100 46 88 52 17 53 77
• 17 | 33 43 100 46 88 52 26 53 77
• 17 26 | 43 100 46 88 52 33 53 77
• 17 26 33 | 100 46 88 52 43 53 77
• 17 26 33 43 | 46 88 52 100 53 77
• 17 26 33 43 46 | 88 52 100 53 77
• 17 26 33 43 46 52 | 88 100 53 77
• 17 26 33 43 46 52 53 | 100 88 77
• 17 26 33 43 46 52 53 77 | 88 100
• 17 26 33 43 46 52 53 77 88 | 100
H4=4 7 19 24 13 31 8 82 18 44 63 5 29
1 2 3 4 5 6 7 8 9 10 11 12
H3=3 7 8 24 13 31 19 5 18 44 63 82 29
1 2 3 4 5 6 7 8 9 10 11 12
H2=2 7 8 19 5 18 24 13 31 29 63 82 44
1 2 3 4 5 6 7 8 9 10 11 12
H1=1
7 5 18 8 13 24 19 31 29 44 82 63
1 2 3 4 5 6 7 8 9 10 11 12
Sorted list 5 7 8 13 18 19 24 29 31 44 63 82
1 2 3 4 5 6 7 8 9 10 11 12
For More Example
• https://www.youtube.com/watch?v=CmPA7zE8mx0
https://youtu.be/ddeLSDsYVp8
Exercise
Solve this array using Shell Sort
23 4 7 34 45 2 43 1 5
Radix Sort
Radix Sort
• It is used to sort numbers using 10 buckets for decimal numbers
ranging 0 - 9.
• The given numbers are first sorted according to the unit digit and
each number is placed in the concerned bucket, and then the buckets
are combined in order before distributing according to the tens digit.
• This sorting process continues to the last digit (i.e. the most
significant bit) in d-passes for d-digit numbers.
• For a 5- digit numbers, radix sort places numbers in the bucket in 5
passes and combines the buckets five times.
• Radix sort is sometimes used to sort records of information that are
keyed by multiple fields (i.e. to sort dates by year, month and day).
• Number of comparisons (Cn) = d*s*n
Where d = Digits in a number (d=10 for decimal digit)
s = Number of digits in a number (s = 4 for 972, 8345 & 89
numbers)
n = Number of items (given numbers to be sorted)
The worst - case = C(n) = O(n2) as s = n
The best - case = C(n) = O(n logn) as s=log10n
The average - case = C(n) = O(s*n)
Activity
Number MSB(Most Significant Bit) LSB(Least Significant Bit)
234567 2 7
58356 5 6
3621 3 1
7824 7 4
Example
Input
4 3 2
5 6 4
1 2 1
0 4 5
0 1
7 8 8
2 3
Example
List of numbers:
48081, 90342, 90287, 90583, 53202, 65215, 78397, 48001, 972, 65315,
41983, 90283, 81664, 38107
Unsorted list
bkt 1st Pass bkt 2nd Pass bkt 3rd Pass bkt 4th Pass bkt 5th Pass Sorted list
• https://
www.youtube.com/watch?time_continue=53&v=nu4gDuFabIM&feat
ure=emb_logo
Activity
List of the numbers sort it using Radix Sort
[234,3478,102,2003,450,2819,611,2976,90283]
Merge Sort
Mergesort (A , mid+1 , L)
Merge ( A, F, mid, L)
Merge Sort Algorithm
Procedure Merge( A[F…..L), F, mid, L)
←
first1 F
←
last1 mid
←
first2 mid+1
←
last2 L
Index ← first1
for first1 to last1 and first2 to last2 do
if A[first1] < A[first2]
then temparray[index] ← A[first1]
←
A[first1] temparray[index]
else temparray[index] ← A[first2]
←
A[first2] t emparray[index]
for first1 to last1
temparray[index] ←
A[first1]
for first2temparray[index]
to last2 ←
← F←to temparray[index]
A[first2]
A[index]
for index L
Merge Sort Analysis
• Let f(n) be of number comparisons needed
to sort an n element array. Algorithm
requires at most n passes and each pass
merges n elements. Also each pass will
require n comparisons.
so for both the worst case and average case
running time will be O(n log 2 n).
Merge Sort
21 7 42 88 3 9 23 67 12 6
Bubble Sort
• In Bubble sort N-1 (N is Number of items to
be sorted) passes are made through array.
• During 1st pass each element is compared
with next one in the array and then their
position is exchanged if required according to
criteria (whether ascending or descending)
Bubble Sort
• At the end of 1st pass, the largest element gets
to its proper position (in case of ascending
order) i.e; the last location of array. So we do
not compare it during 2nd pass. The 2nd pass is
made like 1st one and at the end of 2 nd pass,
the 2nd largest element goes to its proper
position. So we do not compare it during 3 rd
pass and so on it continues.
Bubble Sort
• In this way N-1 passes are made through
array and during each pass comparisons
made are : N- number of passes
Bubble Sort Algorithm
Procedure BubbleSort( arr[1..n])
{ Bubble sort algorithm to arrange elements in
ascending order}
for j ← n to 2 do
for i ← 1 to (j-1)do
if arr[ i ]> arr [i+1] then
hold ← arr[ i ]
arr[ i ] ← arr[ i +1]
arr[i +1] ← hold
Bubble Sort Examples
• Example
• Original Array:
32 51 27 85 66 23 13 57 60 41 30 29
1 2 3 4 5 6 7 8 9 10 11 12
Total number of passes = N-1 = 12-1=11
Comparisons during each pass = N - number of passes
Bubble Sort Analysis
• There are N-1 passes
• Pass one requires N-1 comparisons and at most N-1
exchanges
• Pass two requires N-2 comparisons and at most N-2
exchanges
• So in worst case bubble requires
(N-1)+(N-2)+(N-3)+--------+1=N*(N-1)/2
comparisons
and also
(N-1)+(N-2)+(N-3)+--------+1=N*(N-1)/2 exchanges
Bubble Sort Analysis
• Now operations are:
N*(N-1)/2+ N*(N-1)/2 =N*(N-1)
• Each exchange requires 3 data moves hence there
are :
3* (N*(N-1))=3 * N²-3*N operations in
the
worst
Hencecase
Bubble Sort is O(N²) in the worst case
Also in average case Bubble Sort is O(N²)
Example
Bubble Sort
• https://www.youtube.com/watch?v=xli_FI7CuzA
• https://www.youtube.com/watch?v=18OO361--1E
Activity
4 7 3 0 8 7 9 6 1 2
Bucket Sort
• Bucket sorts work well for data sets where the
possible key values are known and relatively small
and there are on average just a few elements per
bucket.
• Time Complexity:
Best Case : O(N)
Average Case : O(N)
Worst Case : O(N*N) (i.e. Insertion Sort)
• Bucket-Sort (A)
1 n = length [A]
2 for i = 1 to n
3 do insert A [i] into list B [n A [i]]
4 for i = 0 to n - 1
5 do sort list B [i] with insertion sort
6 concatenate the lists B[0], B[1] ... B[n-1] together in order
1 n = 10
A B
2 for i = 1
1 .78 0
3 do insert A[1] into list B[10 A [1]]
2 .17 1
3 .39 2
4 .26 3
5 .72 4
6 .94 5
7 .21 6
8 .12 7 .78
9 .23 8
10 .68 9
1 n = 10
A B
2 for i = 2
1 .78 0
3 do insert A[2] into list B[10 A [2]]
2 .17 1 .17
3 .39 2
4 .26 3
5 .72 4
6 .94 5
7 .21 6
8 .12 7 .78
9 .23 8
10 .68 9
1 n = 10
A B
2 for i = 3
1 .78 0
3 do insert A[3] into list B[10 A [3]]
2 .17 1 .17
3 .39 2
4 .26 3 .39
5 .72 4
6 .94 5
7 .21 6
8 .12 7 .78
9 .23 8
10 .68 9
1 n = 10
A B
2 for i = 4
1 .78 0
3 do insert A[4] into list B[10 A [4]]
2 .17 1 .17
3 .39 2 .26
4 .26 3 .39
5 .72 4
6 .94 5
7 .21 6
8 .12 7 .78
9 .23 8
10 .68 9
1 n = 10
A B
2 for i = 5
1 .78 0
3 do insert A[5] into list B[10 A [5]]
2 .17 1 .17
3 .39 2 .26
4 .26 3 .39
5 .72 4
6 .94 5
7 .21 6
8 .12 7 .78 .72
9 .23 8
10 .68 9
1 n = 10
A B
2 for i = 6
1 .78 0
3 do insert A[6] into list B[10 A [6]]
2 .17 1 .17
3 .39 2 .26
4 .26 3 .39
5 .72 4
6 .94 5
7 .21 6
8 .12 7 .78 .72
9 .23 8
10 .68 9 .94
1 n = 10
A B
2 for i = 7
1 .78 0
3 do insert A[7] into list B[10 A [7]]
2 .17 1 .17
3 .39 2 .26 .21
4 .26 3 .39
5 .72 4
6 .94 5
7 .21 6
8 .12 7 .78 .72
9 .23 8
10 .68 9 .94
1 n = 10
A B
2 for i = 8
1 .78 0
3 do insert A[8] into list B[10 A [8]]
2 .17 1 .17 .12
3 .39 2 .26 .21
4 .26 3 .39
5 .72 4
6 .94 5
7 .21 6
8 .12 7 .78 .72
9 .23 8
10 .68 9 .94
1 n = 10
A B
2 for i = 9
1 .78 0
3 do insert A[9] into list B[10 A [9]]
2 .17 1 .17 .12
3 .39 2 .26 .21 .23
4 .26 3 .39
5 .72 4
6 .94 5
7 .21 6
8 .12 7 .78 .72
9 .23 8
10 .68 9 .94
1 n = 10
A B
2 for i = 10
1 .78 0
3 do insert A[10] into list B[10 A [10]]
2 .17 1 .17 .12
3 .39 2 .26 .21 .23
4 .26 3 .39
5 .72 4
6 .94 5
7 .21 6 .68
• https://www.youtube.com/watch?v=geVyIsFpxUs
Note: Must watch this video to solve this Activity
Insertion Sort
Example
Example
Example
Example
Con’t
Example