Skip to content

Instantly share code, notes, and snippets.

@richzw
Created April 1, 2013 02:57
Show Gist options
  • Select an option

  • Save richzw/5282999 to your computer and use it in GitHub Desktop.

Select an option

Save richzw/5282999 to your computer and use it in GitHub Desktop.
loop version and recursive version
34 void insertSort(int a[], int len){
35 for (int j = 1; j < len; ++j){
36 int key = a[j];
37 int i = j - 1;
38 while(i >= 0 && a[i] > key){
39 a[i+1] = a[i];
40 i--;
41 }
42 a[i+1] = key;
43 }
44 }
45
46 void insertSort_recursive(int a[], int len){
47 if (len <= 1) return;
48 insertSort_recursive(a, len-1);
49 int key = a[len-1];
50 int i = len - 2;
51 while(i >= 0 && a[i] > key){
52 a[i+1] = a[i];
53 --i;
54 }
55 a[i+1] = key;
56 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment