Skip to content

Instantly share code, notes, and snippets.

@rahuladream
Created October 9, 2017 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rahuladream/00b3169900a78876ad25c9b3949b0138 to your computer and use it in GitHub Desktop.
Save rahuladream/00b3169900a78876ad25c9b3949b0138 to your computer and use it in GitHub Desktop.
Insertion Sort
#include <bits/stdc++.h>
using namespace std;
void insertionSort(int arr[], int length);
void printArray(int array[],int size);
int main()
{
int array[5] = {5,4,3,2,1};
insertionSort(array,5);
return 0;
}
void insertionSort(int arr[], int length)
{
int i,j,tmp;
for(i=1; i<length; i++)
{
j = i;
while(j>0 && arr[j-1] > arr[j])
{
tmp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = tmp;
j--;
}
printArray(arr,5);
}
}
void printArray(int array[], int size)
{
cout<<"Sorting the array using insertion";
int j;
for(j=0; j<size; j++)
for(j=0; j<size; j++)
cout<<" " <<array[j];
cout<<endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment