Insertion, Selection, Bubble Sort Algorithms
Insertion, Selection, Bubble Sort Algorithms
Algorithms
Course Code: CSC2211 Course Title: Algorithms
1. Sorting Algorithms
Insertion Sort
Selection Sort
Bubble Sort
Merge Sort
Quick Sort
Counting Sort
Sorting
Simple sorting methods use roughly n * n
comparisons
Insertion sort
Selection sort
Bubble sort
arr[currentIndex + 1] = elementOnHand;
}
}
Insertion Sort [Simulation]
j j j j j j j j j
i i i i i i i i i i
44
88
55
91
11
77
99
22
66
Key 22
11
33 66
33
22 66
33
22 77
66
55
44
99 77
99
66
55 91
99
77
66
11 99
91
88
77 55
99
91
88 88
99
91 44
99
0 1 2 3 4 5 6 7 8 9
Insertion Sort
Complexity Analysis
for(j=1;j<n;j++){
key=a[j];
i=j-1;
while(i>=0 && a[i]< key){
a[i+1] = a[i];
i--;
}
a[i+1]=key;
}
For loop (n)
for(j=1;j<n;j++){ j<n
key=a[j]; Key=a[j]
i=j-1
i=j-1; while loop (n-1)
i>=0 && a[i]< key
while(i>=0 && a[i]< key){ a[i+1] = a[i]
i--
a[i+1] = a[i]; a[i+1]=key
j++
i--;
Extra : j=1
}
a[i+1]=key;
For loop (n)
for(j=1;j<n;j++){ j<n
key=a[j]; Key=a[j]
i=j-1
i=j-1; while loop (n-1)
i>=0 && a[i]< key
while(i>=0 && a[i]< key){ a[i+1] = a[i]
i--
a[i+1] = a[i]; a[i+1]=key
j++
i--;
Extra : j=1
}
a[i+1]=key;
c1
For loop (n)
for(j=1;j<n;j++){ j<n
key=a[j]; Key=a[j]
i=j-1
i=j-1; while loop (n-1)
i>=0 && a[i]< key
while(i>=0 && a[i]< key){ a[i+1] = a[i]
i--
a[i+1] = a[i]; a[i+1]=key
j++
i--;
Extra : j=1
}
a[i+1]=key;
c1 c2
For loop (n)
for(j=1;j<n;j++){ j<n
key=a[j]; Key=a[j]
i=j-1
i=j-1; while loop (n-1)
i>=0 && a[i]< key
while(i>=0 && a[i]< key){ a[i+1] = a[i]
i--
a[i+1] = a[i]; a[i+1]=key
j++
i--;
Extra : j=1
}
a[i+1]=key;
c1 c2 c3
For loop (n)
for(j=1;j<n;j++){ j<n
key=a[j]; Key=a[j]
i=j-1
i=j-1; while loop (n-1) worst case
i>=0 && a[i]< key
while(i>=0 && a[i]< key){ a[i+1] = a[i]
i--
a[i+1] = a[i]; a[i+1]=key
j++
i--;
Extra : j=1
}
a[i+1]=key;
=
Number of machine instructions: n*{ c1 + *c2 }+ c3
=(
Selection Sort [Pseudocode]
if(minIdx!= startIdx){
swap(&arr[minIdx], &arr[startIdx]);
}
}
}
Selection Sort [Simulation]
i = StartingIndex
j = CurrentIndex
k = MinimumIndex
i i i i i i i i i
j j j j j j j j j
11
33 22
66 22
33
66 44
99 55
77 33
66
11 77
91 77
88
91
55 91
88 99
44
k k k k k k k k k k
Selection Sort
Complexity Analysis
for (i = 0; i < n-1; i++){
minIdx = i;
for (curr = i+1; curr < n; curr++)
if (arr[curr] < arr[minIdx])
minIdx = curr;
if(minIdx!=i){
swap(&arr[minIdx], &arr[i]);
}
}
for (i = 0; i < n-1; i++){
minIdx = i;
for (curr = i+1; curr < n; curr++)
if (arr[curr] < arr[minIdx])
minIdx = curr;
if(minIdx!=i){
swap(&arr[minIdx], &arr[i]);
}
}
c1
for (i = 0; i < n-1; i++){
minIdx = i;
for (curr = i+1; curr < n; curr++)
if (arr[curr] < arr[minIdx])
minIdx = curr;
if(minIdx!=i){
swap(&arr[minIdx], &arr[i]);
}
c1 c2
for (i = 0; i < n-1; i++){
minIdx = i;
for (curr = i+1; curr < n; curr++)
if (arr[curr] < arr[minIdx])
minIdx = curr;
if(minIdx!=i){
Extra i=0
swap(&arr[minIdx], &arr[i]);
}
c1 c2 c3
for (i = 0; i < n-1; i++){
minIdx = i;
for (curr = i+1; curr < n; curr++)
if (arr[curr] < arr[minIdx])
minIdx = curr;
if(minIdx!=i){
Extra i=0
swap(&arr[minIdx], &arr[i]);
}
Class Task
Find out the Best & average Case Complexity
using the above equation
Bubble Sorting
Concept:
Compare 1st two elements
If out of order, exchange them to put in order
Move down one element, compare 2nd and 3rd elements, exchange if
necessary. Continue until end of array.
Pass through array again, exchanging as necessary
Repeat until pass made with no exchanges.
Bubble Sort [Algorithm]
An Animated Example
Pass=1
Swap
98 23 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
Pass=1
Swap
23 98 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
Pass=1
Swap
23 98 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
Pass=1
Swap
23 45 98 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
Pass=1
Swap
23 45 98 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
Pass=1
Swap
23 45 14 98 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
Pass=1
Swap
23 45 14 98 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
Pass=1
Swap
23 45 14 6 98 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
Pass=1
Swap
23 45 14 6 98 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
Pass=1
Swap
23 45 14 6 67 98 33 42
1 2 3 4 5 6 7 8
An Animated Example
Pass=1
Swap
23 45 14 6 67 98 33 42
1 2 3 4 5 6 7 8
An Animated Example
Pass=1
Swap
23 45 14 6 67 33 98 42
1 2 3 4 5 6 7 8
An Animated Example
Pass=1
Swap
23 45 14 6 67 33 98 42
1 2 3 4 5 6 7 8
An Animated Example
Pass=1
Swap
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
After First Pass of Outer Loop
Pass=1
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
Pass=2
No Swap
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
Pass=2
Swap
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
Pass=2
Swap
23 14 45 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
Swap
23 14 45 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
Pass=2
Swap
23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
Pass=2
No Swap
23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
Pass=2
Swap
23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
Pass=2
Swap
23 14 6 45 33 67 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
Pass=2
Swap
23 14 6 45 33 67 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
Pass=2
Swap
23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
After Second Pass of Outer Loop
Pass=2
Finished second “Bubble Up”
23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
Pass=3
Swap
23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
Pass=3
Swap
14 23 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
Pass=3
Swap
14 23 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
Pass=3
Swap
14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
Pass=3
No Swap
14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
Pass=3
Swap
14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
Pass=3
Swap
14 6 23 33 45 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
Pass=3
Swap
14 6 23 33 45 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
Swap
14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
After Third Pass of Outer Loop
Pass=3
Finished third “Bubble Up”
14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
Finished
Completed
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
Class Task
Find out the Worst, Best & average Case
Complexity for Bubble sort
Books
https://www.google.com/search?q=bubble+sort+step+by+step&sxsrf=AL
eKk01uxzgfT3Oy6k1Q3WxVnSpiIN8_4g:1587999728942&tbm=isch&sourc
e=iu&ictx=1&fir=vRwFsGwVfJ6pJM%253A%252CSzhhze6MPQr4cM%252C
_&vet=1&usg=AI4_-kSrEEXqwRL-PkHhVUtn7jNfF9dB6g&sa=X&ved=2ahU
KEwje0Pz974jpAhXRAnIKHWhMD2UQ_h0wAXoECAcQBg#imgrc=EN4Sdu7
veOWVoM&imgdii=eOqvCu85p9-eBM
https://www.interviewcake.com/concept/java/counting-sort
https://www.geeksforgeeks.org/counting-sort/
https://www.hackerearth.com/practice/algorithms/sorting/quick-sort/tut
orial/