Name: Sanket Rahangdale REG No.: 19010855 ROLL No: B-171: Practical-2
Name: Sanket Rahangdale REG No.: 19010855 ROLL No: B-171: Practical-2
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.
Step-01: For i = 1
Step-02: For i = 2
Step-03: For i = 3
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-
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
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-
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.
Code:-
#include <bits/stdc++.h>
using namespace std;
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
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 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.
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, 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.