Skip to content

Instantly share code, notes, and snippets.

@sohana08
Created January 16, 2017 17:56
Show Gist options
  • Save sohana08/1cfc24376a7e21c56cac89225ec3c646 to your computer and use it in GitHub Desktop.
Save sohana08/1cfc24376a7e21c56cac89225ec3c646 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
void insertionSort(int n, int * arr)
{
int temp,i,j,k;
for(i=1;i<n;i++)
{
j=i;
while(j>0 && arr[j]<arr[j-1])
{
temp=arr[j];
arr[j]=arr[j-1];
arr[j-1]=temp;
j--;
for(k=0;k<n;k++)
cout<<arr[k]<<" ";
cout<<endl;
}
}
}
int main()
{
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
insertionSort(n,arr);
return 0;
}
@sohana08
Copy link
Author

this is my code for insertion sort.
sample input:
1 5 3 6 8 4
output:
1 5 3 6 8 4
1 3 5 6 8 4
1 3 4 5 6 8

i am new to programming and in a learning stage. i want to print the above output but its not giving the first line output.
what is missing in my code??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment