Last active
January 5, 2016 15:21
-
-
Save sri-prasanna/daf1188c46dfe90f0642 to your computer and use it in GitHub Desktop.
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
void InsertionSort(int [] A, int n) | |
{ | |
int curr = A[n]; | |
if (n != 0) | |
InsertionSort(A, (n-1)); | |
PushIntoItsSpot(A, curr, (n-1)); | |
} | |
void PushIntoItsSpot(int [] A, int val, int rpos) | |
{ | |
while(rpos >= 0 && A[rpos] > val) | |
A[rpos+1] = A[rpos--]; | |
A[++rpos] = val; | |
} | |
void Main() | |
{ | |
var A = new int [] {4, 1, -1, 12, 5}; | |
Console.WriteLine(A); | |
InsertionSort(A, A.Length-1); | |
Console.WriteLine(A); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment