Created
February 27, 2011 16:17
-
-
Save kokousin/846292 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 insertSort(int num[] , int length) | |
{ | |
for(int i=2 ; i<length ; i++) // We need to start to sort from num[2], because num[0] is the guard. | |
{ // and num[1] is the first sorted value. | |
num[0] = num[i] ; // num[0] is the guard which is the value that will be inserted, num[i] ; | |
int j = i ; | |
while(num[0]<num[j-1]) // We needn't to worry about the ArrayIndex out of bounds, | |
{ // because the first number of array is num[0] that equals | |
num[j] = num[j-1] ; // of num[i]. | |
j-- ; | |
} | |
num[j] = num[0] ; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment