For the following data set {25, 74, 45, 65, 17, 31, 64, 50}, Perform insertion sort and write output of each iteration
#include <iostream>
using namespace std;
void insertion_Sort(int array[], int arr_size)
{
int n, item, k;
for (n = 1; n <arr_size; n++)
{
item = array[n];
k = n - 1;
while (k >= 0 && array[k] > item)
{
array[k + 1] = array[k];
k = k - 1;
}
array[k + 1] = item;
}
}
void display(int array[], int arr_size)
{
int x;
for (x = 0; x < arr_size; x++)
cout << array[x] << " ";
cout << endl;
}
int main()
{
int array[] = {25, 74, 45, 65, 17, 31, 64, 50};
int array_size= sizeof(array) / sizeof(array[0]);
insertion_Sort(array, array_size);
display(array, array_size);
return 0;
}
Comments
Leave a comment