NAME: SANKET RAHANGDALE
REG No.: 19010855
ROLL No: B-171
PRACTICAL-2
AIM: Stimulate both the insertion sort and selection sort algorithm on the array
X = {1, 1, 1, 1, 1, 1}
How does this compare to sorting the arrays U and V in previous practical-1.
U = {1,2,3,4,5,6} V = {6,5,4,3,2,1}
THEORY:
Insertion sort
Insertion sort is an in-place sorting algorithm.
It uses no auxiliary data structures while sorting.
It is inspired from the way in which we sort playing cards.
How Insertion Sort Works?
Consider the following elements are to be sorted in ascending order- 6, 2, 11, 7, 5
Insertion sort works as-
Firstly,
It selects the second element (2).
It checks whether it is smaller than any of the elements before it.
Since 2 < 6, so it shifts 6 towards right and places 2 before it.
The resulting list is 2, 6, 11, 7, 5.
Secondly,
Itselects the third element (11).
It checks whether it is smaller than any of the elements before it.
Since 11 > (2, 6), so no shifting takes place.
The resulting list remains the same.
Thirdly,
Itselects the fourth element (7).
It checks whether it is smaller than any of the elements before it.
Since 7 < 11, so it shifts 11 towards right and places 7 before it.
The resulting list is 2, 6, 7, 11, 5.
Fourthly,
It selects the fifth element (5).
It checks whether it is smaller than any of the elements before it.
Since 5 < (6, 7, 11), so it shifts (6, 7, 11) towards right and places 5 before
them.
The resulting list is 2, 5, 6, 7, 11.
As a result, sorted elements in ascending order are- 2, 5, 6, 7, 11
Insertion Sort Example-
Consider the following elements are to be sorted in ascending order- 6, 2, 11, 7, 5
The above insertion sort algorithm works as illustrated below-
Step-01: For i = 1
Step-02: For i = 2
Step-03: For i = 3
2 5 11 7 6 For j = 2; 11 > 7 so A[3] = 11
For j = 1; 5 < 7 so loop stops and A[2] =
2 5 11 11 6
7
2 5 7 11 6 After inner loop ends
Working of inner loop when i = 3
Step-04: For i = 4
Loop gets terminated as ‘i’ becomes 5. The state of array after the loops are finished-
With each loop cycle,
One element is placed at the correct location in the sorted sub-array until array A is
completely sorted.
Important Notes-
Insertion sort is not a very efficient algorithm when data sets are large.
This is indicated by the average and worst case complexities.
Insertion sort is adaptive and number of comparisons are less if array is
partially sorted.
Selection Sort-
Selection sort is one of the easiest approaches to sorting.
It is inspired from the way in which we sort things out in day to day life.
It is an in-place sorting algorithm because it uses no auxiliary data structures
while sorting.
How Selection Sort Works?
Consider the following elements are to be sorted in ascending order using selection
sort-6, 2, 11, 7, 5
Selection sort works as-
It finds the first smallest element (2).
It swaps it with the first element of the unordered list.
It finds the second smallest element (5).
It swaps it with the second element of the unordered list.
Similarly, it continues to sort the given elements.
As a result, sorted elements in ascending order are-2, 5, 6, 7, 11
Selection Sort Example-
Consider the following elements are to be sorted in ascending order-6, 2, 11, 7, 5
The above selection sort algorithm works as illustrated below-
Step-01: For i = 0
Step-02: For i = 1
Step-03: For i = 2
Step-04: For i = 3
Step-05: For i = 4
Loop gets terminated as ‘i’ becomes 4.
The state of array after the loops are finished is as shown-
With each loop cycle,
The minimum element in unsorted sub-array is selected.
It is then placed at the correct location in the sorted sub-array until array A is
completely sorted.
Important Notes-
Selection sort is not a very efficient algorithm when data sets are large.
This is indicated by the average and worst case complexities.
Selection sort uses minimum number of swap operations O(n) among all the
sorting algorithms.
PROGRAM AND INPUT:
Code:-
#include <bits/stdc++.h>
using namespace std;
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
int selectionSort(int arr[], int n)
{
int i, j, min_idx, count = 0;
for (i = 0; i < n - 1; i++)
{
min_idx = i;
for (j = i + 1; j < n; j++)
{
if (arr[j] < arr[min_idx])
min_idx = j;
count++;
}
swap(&arr[min_idx], &arr[i]);
}
return count;
}
int insertionSort(int arr[], int n)
{
int i, key, j, count = 0;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
count++;
}
arr[j + 1] = key;
}
return count;
}
void printArray(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
cout << arr[i] << " ";
cout << " ";
}
int main()
{
int count2 = 0, count1 = 0;
int arr1[] = {1, 1, 1, 1, 1, 1};
int arr2[] = {1, 1, 1, 1, 1, 1};
int n1 = sizeof(arr1) / sizeof(arr1[0]);
int n2 = sizeof(arr2) / sizeof(arr2[0]);
cout << "array before sorting-" << endl;
cout << " W W" << endl;
cout << "array before sorting:- ";
printArray(arr1, n1);
printArray(arr2, n2);
cout << endl;
cout << "array after sorting:- ";
count1 = selectionSort(arr1, n1);
printArray(arr1, n1);
count2 = insertionSort(arr2, n2);
printArray(arr2, n2);
cout << endl
<< "count value:- ";
cout << count1 << " ";
cout << count2 << endl;
if (count1 > count2)
cout << "insertion sort is faster than selection sort";
else if (count1 == count2)
cout << "insertion sort and selection sort take equal time";
else
cout << "selection sort is faster than insertion sort";
return 0;
}
Output 1:-
Justification:-
To decide which algorithm (i.e. program) run faster we need to give same input to
both program(i.e. here).
As we can see that selection sort takes 15 iteration while insertion sort take 0 iteration.
So insertion sort run faster than selection sort on input W.
Output 2:-
Justification:- In this, insertion sort runs in same time for both arrays V and U as
both are sorted already.
Output 3:-
Justification:- In this, insertion sort runs faster on array U than array V as U is
sorted already.
Output 4:-
Justification:- In this, selection sort runs in same time for both arrays X and U as
selection sort takes same time irrespective whether the array is sorted or not.
Output 5:-
Justification:- In this, selection sort runs in same time for both arrays X and V as
selection sort takes same time irrespective whether the array is sorted or not.
CONCLUSION: In the above practical we have implemented selection sort and
compared time complexity on various input.