Skip to content

Instantly share code, notes, and snippets.

@kokousin
Created February 27, 2011 16:17
Show Gist options
  • Save kokousin/846292 to your computer and use it in GitHub Desktop.
Save kokousin/846292 to your computer and use it in GitHub Desktop.
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