Created
October 9, 2017 20:11
-
-
Save rahuladream/00b3169900a78876ad25c9b3949b0138 to your computer and use it in GitHub Desktop.
Insertion Sort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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