INSERTION SORTING
Insertion Sorting: code
#include<iostream>
using namespace std;
int main(){
int i,j,n,temp;
int arr[100];
cout<<"Enter size of the array:"<<endl;
cin>>n;
cout<<"Enter array that to be sorted by bubble sort"<<endl;
for(i=0;i<n;i++){
cin>>arr[i];
}
for(i=1;i<n;i++){
temp=arr[i];
j=i-1;
while(j>=0 && arr[j]>temp){
arr[j+1]=arr[j];
j--;
}
arr[j+1]=temp;
}
cout<<"Sorted Array is:"<<endl;
for(i=0;i<n;i++){
cout<<arr[i]<<" ";
}
return 0;
}
Comments
Post a Comment