Created
April 1, 2013 02:57
-
-
Save richzw/5282999 to your computer and use it in GitHub Desktop.
loop version and recursive version
This file contains hidden or 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
| 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