Skip to content

Instantly share code, notes, and snippets.

@nanchenzi
Created April 3, 2013 17:43
Show Gist options
  • Save nanchenzi/5303460 to your computer and use it in GitHub Desktop.
Save nanchenzi/5303460 to your computer and use it in GitHub Desktop.
插入排序
int[] InsertionSort(int[] array){
for(int i = 1; i<array.length; i++){
int insertValue = array[i];
int cursor = i-1;
while(cursor >= 0 && array[cursor]>insertValue){
array[cursor+1] = array[cursor];
cursor--;
}
array[cursor+1] = insertValue;
}
return array;
}
/**在最优的情况下,它的算法时间复杂度为o(n),在最坏的情况下算法时间复杂度为o(n^2) **/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment