Algorithm For Insertion Sort
//Insertion Sort
#include"stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int arr[10];
int tb;
cout<<"Enter array values"<<endl;
for(int i=0;i<10;i++)
{
cin>>arr[i];
}
for(int j, i = 0; i < 10; i++)
{ tb = arr[i];
j = i - 1;
while ((tb < arr[j]) && (j >= 0))
{ arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = tb;
}
cout<<"After sorting array is"<<endl;
for(int i=0;i<10;i++)
{
cout<<arr[i]<<endl;
}
return 0;
}
Comments
Post a Comment