Sorting-Bubble, Insertion and Selection
Sorting-Bubble, Insertion and Selection
Insertion Sort
and
Selection Sort
77 42 35 12 101 5
1 2 3 4 5 6
5 12 35 42 77 101
1 2 3 4 5 6
77 42 35 12 101 5
1 2 3 4 5 6
42 Swap4277
77 35 12 101 5
1 2 3 4 5 6
42 7735 Swap35
77 12 101 5
1 2 3 4 5 6
42 35 12 Swap 12
77 77 101 5
1 2 3 4 5 6
42 35 12 77 101 5
No need to swap
1 2 3 4 5 6
1 2 3 4 5 6
42 35 12 77 5 101
1 2 3 4 5 6
42 35 12 77 5 101
1 2 3 4 5 6
35 12 42 5 77 101
1 2 3 4 5 6
N-1
12 35 5 42 77 101
1 2 3 4 5 6
12 5 35 42 77 101
1 2 3 4 5 6
5 12 35 42 77 101
1 2 3 4 5 6
35 12 42 5 77 101
1 2 3 4 5 6
12 35 5 42 77 101
1 2 3 4 5 6
12 5 35 42 77 101
For example:
This is the 4th “bubble up”
MAX is 6
Thus we have 2 comparisons to do
1 2 3 4 5 6
12 35 5 42 77 101
N 8 did_swap true
to_do 7
index
98 23 45 14 6 67 33 42
1 2 3 4 5 6 7 8
N 8 did_swap false
to_do 7
index 1
98 23 45 14 6 67 33 42
1 2 3 4 5 6 7 8
N 8 did_swap false
to_do 7
index 1
Swap
98 23 45 14 6 67 33 42
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 7
index 1
Swap
23 98 45 14 6 67 33 42
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 7
index 2
23 98 45 14 6 67 33 42
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 7
index 2
Swap
23 98 45 14 6 67 33 42
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 7
index 2
Swap
23 45 98 14 6 67 33 42
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 7
index 3
23 45 98 14 6 67 33 42
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 7
index 3
Swap
23 45 98 14 6 67 33 42
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 7
index 3
Swap
23 45 14 98 6 67 33 42
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 7
index 4
23 45 14 98 6 67 33 42
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 7
index 4
Swap
23 45 14 98 6 67 33 42
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 7
index 4
Swap
23 45 14 6 98 67 33 42
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 7
index 5
23 45 14 6 98 67 33 42
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 7
index 5
Swap
23 45 14 6 98 67 33 42
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 7
index 5
Swap
23 45 14 6 67 98 33 42
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 7
index 6
23 45 14 6 67 98 33 42
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 7
index 6
Swap
23 45 14 6 67 98 33 42
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 7
index 6
Swap
23 45 14 6 67 33 98 42
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 7
index 7
23 45 14 6 67 33 98 42
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 7
index 7
Swap
23 45 14 6 67 33 98 42
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 7
index 7
Swap
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 7
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
N 8 did_swap false
to_do 6
index 1
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
N 8 did_swap false
to_do 6
index 1
No Swap
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
N 8 did_swap false
to_do 6
index 2
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
N 8 did_swap false
to_do 6
index 2
Swap
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 6
index 2
Swap
23 14 45 6 67 33 42 98
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 6
index 3
23 14 45 6 67 33 42 98
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 6
index 3
Swap
23 14 45 6 67 33 42 98
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 6
index 3
Swap
23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 6
index 4
23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 6
index 4
No Swap
23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 6
index 5
23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 6
index 5
Swap
23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 6
index 5
Swap
23 14 6 45 33 67 42 98
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 6
index 6
23 14 6 45 33 67 42 98
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 6
index 6
Swap
23 14 6 45 33 67 42 98
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 6
index 6
Swap
23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 6
23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
N 8 did_swap false
to_do 5
index 1
23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
N 8 did_swap false
to_do 5
index 1
Swap
23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 5
index 1
Swap
14 23 6 45 33 42 67 98
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 5
index 2
14 23 6 45 33 42 67 98
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 5
index 2
Swap
14 23 6 45 33 42 67 98
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 5
index 2
Swap
14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 5
index 3
14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 5
index 3
No Swap
14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 5
index 4
14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 5
index 4
Swap
14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 5
index 4
Swap
14 6 23 33 45 42 67 98
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 5
index 5
14 6 23 33 45 42 67 98
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 5
index 5
Swap
14 6 23 33 45 42 67 98
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 5
index 5
Swap
14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 5
14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
N 8 did_swap false
to_do 4
index 1
14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
N 8 did_swap false
to_do 4
index 1
Swap
14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 4
index 1
Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 4
index 2
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 4
index 2
No Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 4
index 3
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 4
index 3
No Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 4
index 4
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 4
index 4
No Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
N 8 did_swap true
to_do 4
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
N 8 did_swap false
to_do 3
index 1
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
N 8 did_swap false
to_do 3
index 1
No Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
N 8 did_swap false
to_do 3
index 2
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
N 8 did_swap false
to_do 3
index 2
No Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
N 8 did_swap false
to_do 3
index 3
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
N 8 did_swap false
to_do 3
index 3
No Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
N 8 did_swap false
to_do 3
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
N 8 did_swap false
to_do 3
We didn’t do any swapping,
index 4 so all of the other elements
must be correctly placed.
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
Array after
Completion
of Each Pass
Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 100
Bubble Sort: Analysis
Number of comparisons (worst case):
n – 1 O(n)
Number of exchanges (worst case):
(n-1) + (n-2) + ... + 3 + 2 + 1 O(n²)
Number of exchanges (best case):
0 O(1)
Overall worst case: O(n²) + O(n²) = O(n²)
Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 101
Insertion Sort
Insertion sort is the simple sorting algorithm
which is commonly used in the daily lives
while ordering a deck of cards.
In this algorithm, we insert each element onto
its proper place in the sorted array.
This is less efficient than the other sort
algorithms like quick sort, merge sort, etc.
Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 103
Technique
Consider an array A whose elements are to be sorted. Initially,
A[0] is the only element on the sorted set. In pass 1, A[1] is
placed at its proper index in the array.
In pass 2, A[2] is placed at its proper index in the array.
Likewise, in pass n-1, A[n-1] is placed at its proper index into the
array.
To insert an element A[k] to its proper index, we must compare it
with all other elements i.e. A[k-1], A[k-2], and so on until we find
an element A[j] such that, A[j]<=A[k].
All the elements from A[k-1] to A[j] need to be shifted and A[k]
will be moved to A[j+1].
Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 104
Insertion Sort
int[] myList = {2, 9, 5, 4, 8, 1, 6}; //
The insertion sort Unsorted
Step 1: Initially, the sorted sublist contains the 2 9 5 4 8 1 6
algorithm sorts a list first element in the list. Insert 9 to the sublist.
of values by
Step2: The sorted sublist is {2, 9}. Insert 5 to the 2 9 5 4 8 1 6
repeatedly inserting sublist.
an unsorted element
into a sorted sublist Step 3: The sorted sublist is {2, 5, 9}. Insert 4 to 2 5 9 4 8 1 6
the sublist.
until the whole list
is sorted. Step 4: The sorted sublist is {2, 4, 5, 9}. Insert 8 2 4 5 9 8 1 6
to the sublist.
Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 105
animation
Insertion Sort
int[] myList = {2, 9, 5, 4, 8, 1, 6}; // Unsorted
2 9 5 4 8 1 6
2 9 5 4 8 1 6
2 5 9 4 8 1 6
2 4 5 9 8 1 6
2 4 5 8 9 1 6
1 2 4 5 8 9 6
1 2 4 5 6 8 9
Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 106
How to Insert?
The insertion sort [0] [1] [2] [3] [4] [5] [6]
algorithm sorts a list list 2 5 9 4 Step 1: Save 4 to a temporary variable currentElement
Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 107
Insertion Sort: Example
The Card Player‘s Method: Insertion Sort
Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 108
Insertion Sort: Example
Insertion Sort:
4 passes
Pass 3
Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 109
Insertion Sort: Analysis
Number of comparisons (worst case):
n –1 O(n)
Number of exchanges (worst case):
0 O(1)
Overall worst case: O(n²) + O(n²) = O(n²)
Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 110
void insertionSort(int[] arr)
{
int i, j, newValue;
for (i = 1; i < arr.length; i++)
{
newValue = arr[i];
j = i;
while (j > 0 && arr[j - 1] > newValue)
{
arr[j] = arr[j - 1];
j--;
}
arr[j] = newValue;
}
}
Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 111
#include<stdio.h>
void main ()
{
int i,j, k,temp;
int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
printf("\nprinting sorted elements...\n");
for(k=1; k<10; k++)
{
temp = a[k];
j= k-1;
while(j>=0 && temp <= a[j])
{
a[j+1] = a[j];
j = j-1;
}
a[j+1] = temp;
}
}
Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 112
Selection Sort
SELECTION SORT
Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 114
Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 115
Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 116
Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 117
Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 118
Example:
Selection
Sort
Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 119
PSEUDO CODE FOR SELECTION SORT
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
if(a[j]<a[min])
Min=j;
Swap(a[i},a[min}
}
}
Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 120
// Selection Sort
#include <stdlib.h>
#define N 6
int main()
{
int a[N]= { 23, 78, 45, 8, 32, 56};
int i,j;
// Sort the array using Selection Sort
int minIndex;
for(i=0; i < N-1; i++)
{
// find the minimum element in the unsorted part of the
array
minIndex=i;
for(j=i+1; j < N; j++)
if(a[j] < a[minIndex])
minIndex = j;
Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 122
Advantages
– Easy to write
– Can be done “in place”
– Can be done on linked lists too (keep a tail pointer)
Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 123
Disadvantages
It is about N2 even in the best case for
comparisons.
So the running time (over all inputs)
is approximately O(N2).
The primary disadvantage of selection sort is
its poor efficiency when dealing with a huge
list of items.
Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 124
Comparison of Quadratic Sorts
Comparisons Exchanges
Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 125
Result Quadratic Algorithms
Pro Contra