Design & Analysis of Algorithms - Topic 4 - Introduction to the Design of Sorting Algorithms
Design & Analysis of Algorithms - Topic 4 - Introduction to the Design of Sorting Algorithms
ALGORITHMS
Topic 4
Introduction to the Design of
Sorting Algorithms
INSTRUCTOR: Sulaman Ahmad Naz
Sorting
■ In this lesson and in this series of lessons we are
going to talk about sorting algorithms.
2
Sorting
■ Sometimes to improve the readability of that data,
so that others might be able to search or extract
some information quickly out of that data.
■ For example:
We keep currency notes sorted.
3
Sorting
■ Now, have a look at this, when we go to an online
marketplace such as OLX.
■ The site gives us options of sorting the items.
4
Sorting
■ So, sorting is a really helpful feature here and there
are so many places where we like to keep our data
sorted.
■ Be it the language dictionary where we want to keep
the words sorted so that searching a word in the
dictionary is easy.
5
Sorting
■ If we want to define sorting formally, then sorting is
arranging the elements in a list or collection in
increasing or decreasing order of some property.
12 21 6 8 5
5 6 8 12 21
7
Sorting
■ For example, if we have the following list:
12 21 6 8 5
21 12 8 6 5
8
Sorting
■ For example, if we have the following list:
12 21 6 8 5
9
Sorting
■ For example, if we have the following list:
12 21 6 8
5
1,2, 1,3, 1,2, 1,2,
1,5
3,4, 7,21 3,6 4,8
6,12
10
Sorting
■ A sorted list is the permutation of the original list,
When we sort a list, we just rearrange the elements.
11
Sorting
12
Sorting
13
Sorting
■ And we may have a list of complex data type as well.
19
Sorting
■ We have so many algorithms for sorting that have been
designed over a period of time.
20
Sorting w.r.t Time Complexi
Sorting Algorithms
Polynomial Time
n.Logrithmic Time Linear Time
21
Sorting
■ The second parameter that we use for classification is space
complexity or memory usage.
Sorting Algorithms
In-Place Out-Place
Sorting Algorithms
Stable Unstable
25
Sorting
■ The next parameter of classification is whether a sort is
internal sort or external sort.
Sorting Algorithms
Internal External
27
Sorting
■ The next parameter upon which we may classify sorting
algorithms is whether algorithm is recursive or non
recursive in nature.
28
Sorting w.r.t Design
Sorting Algorithms
Iterative Recursive
29
Sorting
■ There is one more parameter upon which we may classify
sorting algorithms, and it is whether algorithm compares
the values or does sorting in some other way.
Sorting Algorithms
31
The Basic Comparison Block
3
■ Before starting to learn
sorting algorithms, lets start
with a very basic comparison 4 4
block.
32
The Basic Comparison Block
num1
If num1 <= num2
Pop_Down num1
Pop_Right num2 num2 larger
Else
Pop_Down num2
Pop_Right num1 smaller
Now, we will combine these blocks in a triangular way and try to sort a list.
33
5
Constructing Our
4 5 1st Sorting Algorithm
4
After each step, we
3 4 5 get/select the
minimum element of
3 4 the (remaining) list.
1 3 4 5
1 3 4
2 2 3 4
1 2 3 4 5 34
Observations
The minimum value is “SELECTED” after each iteration.
For i = L to U-1
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
36
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
12 21 6 8 5
For i = L to U-1 i
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
37
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
12 21 6 8 5
For i = L to U-1 i
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
38
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
12 21 6 8 5
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
39
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
12 21 6 8 5
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
40
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
12 21 6 8 5
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
41
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
12 21 6 8 5
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
42
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
12 21 6 8 5
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
43
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
12 21 6 8 5
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
44
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
12 21 6 8 5
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
45
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
12 21 6 8 5
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
46
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
12 21 6 8 5
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
47
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
12 21 6 8 5
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
48
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
12 21 6 8 5
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
49
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
5 21 6 8 12
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
50
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
5 21 6 8 12
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
51
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
5 21 6 8 12
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
52
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
5 21 6 8 12
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
53
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
5 21 6 8 12
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
54
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
5 21 6 8 12
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
55
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
5 21 6 8 12
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
56
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
5 21 6 8 12
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
57
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
5 21 6 8 12
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
58
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
5 21 6 8 12
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
59
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
5 21 6 8 12
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
60
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
5 6 21 8 12
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
61
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
5 6 21 8 12
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
62
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
5 6 21 8 12
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
63
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
5 6 21 8 12
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
64
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
5 6 21 8 12
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
65
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
5 6 21 8 12
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
66
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
5 6 21 8 12
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
67
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
5 6 21 8 12
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
68
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
5 6 21 8 12
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
69
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
5 6 8 21 12
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
70
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
5 6 8 21 12
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
71
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
5 6 8 21 12
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
72
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
5 6 8 21 12
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
73
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
5 6 8 21 12
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
74
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
5 6 8 21 12
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
75
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
5 6 8 21 12
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
76
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
5 6 8 12 21
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
77
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
5 6 8 12 21
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
78
1st Sorting Algorithm
L U
Algorithm ColumnWiseSorting(List[L…U])
5 6 8 12 21
For i = L to U-1 i j
Min
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
79
Recall: The Basic Comparison
Block
num1
If num1 <= num2
Pop_Down num1
Pop_Right num2 num2 larger
Else
Pop_Down num2
Pop_Right num1 smaller
Again, we will combine these blocks in a triangular way and try to sort a list.
80
5
5
Constructing Our
4 2nd Sorting Algorithm
4 5
After each step, we
3 4 insert the next
element in its correct
3 4 5 position in the sorted
sub-list.
1 3 4
1 3 4 5
2 2 3 4
1 2 3 4 5 81
Observations
The minimum value is “SELECTED” after each iteration.
The maximum value is “SELECTED” after each iteration.
Input size is reduced after each iteration. (Decrease & Conquer)
The mechanism of the algorithm reflects “Iterative Improvement” design paradigm.
It belongs to “Divide & Conquer” class of algorithms.
The comparison block does not apply when only 1 one number is left.
The problem is recursive-only in nature.
The problem is iterative in nature.
After each iteration, the considered element is “INSERTED” in its final sorted location.
After each iteration, the considered element is “INSERTED” in its current sorted location.
82
2nd Sorting Algorithm
L U
Algorithm RowWiseSorting(List[L…U])
12 21 6 8 5
For i = L+1 to U
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break
List[ j+1 ] = List[ j ]
List[ j+1 ] = value
83
2nd Sorting Algorithm
L U
Algorithm RowWiseSorting(List[L…U])
12 21 6 8 5
For i = L+1 to U i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break
List[ j+1 ] = List[ j ]
List[ j+1 ] = value
84
2nd Sorting Algorithm
L U
Algorithm RowWiseSorting(List[L…U])
12 21 6 8 5
For i = L+1 to U i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 21
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 21
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 21
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 21
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 21
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 21
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 6
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 6
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 6
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 6
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 6
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 6
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 6
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 6
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 6
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 6
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 8
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 8
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 8
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 8
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 8
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 8
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 8
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 8
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 8
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 8
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 8
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 8
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 5
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 5
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 5
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 5
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 5
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 5
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 5
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 5
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 5
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 5
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 5
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 5
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 5
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 5
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 5
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 5
For i = L+1 to U j i
value=List[i]
For j = i-1 to L
If List[ j ] < value value
break 5
ColumnWiseSorting(List[L…U]) RowWiseSorting(List[L…U])
For i = L to U-1 For i = L+1 to U
Min=i value=List[i]
For j = i+1 to U For j = i-1 to L
If List[ j ] < If List[ j ] < value
List[Min] break
Min=j List[ j+1 ] = List[ j ]
Swap List[i]↔List[Min] List[ j+1 ] = value
131
Another Basic Sorting Block
132
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
12 21 6 8 5
133
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
12 21 6 8 5
i 134
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
12 21 6 8 5
j
i 135
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
12 21 6 8 5
j
i 136
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
12 21 6 8 5
j
i 137
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
12 21 6 8 5
j
i 138
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
12 6 21 8 5
j
i 139
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
12 6 21 8 5
j
i 140
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
12 6 21 8 5
j
i 141
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
12 6 8 21 5
j
i 142
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
12 6 8 21 5
j
i 143
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
12 6 8 21 5
j
i 144
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
12 6 8 5 21
j
i 145
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
12 6 8 5 21
j
i 146
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
12 6 8 5 21
j
i 147
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
12 6 8 5 21
j
i 148
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
12 6 8 5 21
j
i 149
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
6 12 8 5 21
j
i 150
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
6 12 8 5 21
j
i 151
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
6 12 8 5 21
j
i 152
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
6 8 12 5 21
j
i 153
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
6 8 12 5 21
j
i 154
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
6 8 12 5 21
j
i 155
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
6 8 5 12 21
j
i 156
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
6 8 5 12 21
j
i 157
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
6 8 5 12 21
j
i 158
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
6 8 5 12 21
j
i 159
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
6 8 5 12 21
j
i 160
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
6 8 5 12 21
j
i 161
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
6 8 5 12 21
j
i 162
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
6 5 8 12 21
j
i 163
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
6 5 8 12 21
j
i 164
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
6 5 8 12 21
j
i 165
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
6 5 8 12 21
j
i 166
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
6 5 8 12 21
j
i 167
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
5 6 8 12 21
j
i 168
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
5 6 8 12 21
j
i 169
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
5 6 8 12 21
j
i 170
3rd Sorting Algorithm
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
L U
5 6 8 12 21
j
i 171
Observations
The minimum value is placed after each iteration.
The maximum value is placed after each iteration.
Input size is reduced after each iteration. (Decrease & Conquer)
The mechanism of the algorithm reflects “Iterative Improvement” design paradigm.
It belongs to “Divide & Conquer” class of algorithms.
The comparison block does not apply when only 1 one number is left.
The problem is recursive-only in nature.
The problem is iterative in nature.
After each iteration, the considered element is placed in its final sorted location.
After each iteration, the considered element is placed in its current sorted location.
172
Class Activity (1.5 hrs)
173
SELECTION SORT
ColumnWiseSorting(List[L…U])
For i = L to U-1
Min=i
For j = i+1 to U
If List[ j ] < List[Min]
Min=j
Swap List[i]↔List[Min]
174
BUBBLE SORT
Algorithm BubbleSort(List[L…U])
For i = U-1 to L
For j = L to i
If List[ j ] > List[ j+1 ]
Swap List[ j ] =
List[ j+1 ]
175
INSERTION SORT
RowWiseSorting(List[L…U])
For i = L+1 to U
value=List[i]
For j = i-1 to L
If List[ j ] < value
break
List[ j+1 ] = List[ j ] //shift
List[ j+1 ] = value
176
End of Lecture
THANK YOU
177