Write a program for insertion sorting.
#include <iostream>
using namespace std;
void insertionSort(int *p,int size);
void insertionSort(int *p,int size)
{
int i,j,temp;
for(i=1;i<size;i++)
{
temp=p[i];
j=i-1;
while(j>=0
&& p[j]>temp)
{
p[j+1]=p[j];
j--;
}
p[j+1]=temp;
}
for(i=0;i<size;i++)
{
cout<<p[i]<<" ";
}
}
int main()
{
int n;
int *p;
cout<<"Enter number of element you want to store in an a
array:"<<endl;
cin>>n;
p=new int[n];
cout<<"Now enter the values"<<endl;
for(int
i=0;i<n;i++)
{
cin>>p[i];
}
insertionSort(p,n);
}
Checkout more java programs on Zamacodes.com
0 comments:
Post a Comment