A blog to help students to learn programming in various languages including C, C++, Java, Python and many more.

Subscribe For Free Latest Updates!

We'll not spam mate! We promise.

Saturday 23 January 2016



Program Screenshot
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



Copyright © 2016 - ProgrmIt - All Rights Reserved
(Articles Cannot Be Reproduced Without Author Permission.)
Design By : | Powered By: Blogger