Output |
#include <iostream>
using namespace std;
int insertionSort(int a[],int n);
int main()
{
int n,data,a[100];
cout<<"Enter no. of values: ";
cin>>n;
cout<<"Enter "<<n<<" values: ";
for(int x=0;x<n;x++) {
cin>>a[x];
}
insertionSort(a,n);
cout<<"Array after Sorting using Insertion Sort: \n";
for(int x=0;x<n;x++) {
cout<<a[x]<<"\n";
}
return(0);
}
int insertionSort(int a[],int n)
{
int pivot,temp;
for(int startPos=1;startPos<n;startPos++) {
pivot=startPos;
while(pivot >= 1 && a[pivot] < a[pivot-1]) {
temp=a[pivot];
a[pivot]=a[pivot-1];
a[pivot-1]=temp;
pivot--;
}
}
}
0 comments :
Post a Comment