insertSort
1 static void insertSort2(int* arr, int n) { 2 int j, temp; 3 for(int i = 1; i < n; i++) { 4 temp = arr[i]; 5 for(j = i - 1; j >= 0; j--) { 6 if(temp < arr[j]) 7 arr[j + 1] = arr[j]; 8 } 9 arr[j + 1] = temp; 10 } 11 }
1 #include <stdio.h> 2 3 static void insertSort2(int *arr, int n) { 4 int i, k, j, temp; 5 for( i = 1; i < n; i++) { 6 temp = arr[i]; 7 for(j = i - 1; j >= 0 && temp < arr[j]; j--) { 8 arr[j + 1] = arr[j]; 9 } 10 arr[j + 1] = temp; 11 } 12 for (k=0; k<n; k++){ 13 printf("%d ", arr[k]); 14 } 15 16 } 17 int main(){ 18 int arr[]={1,1,2,1,3,2,4,5,6,8,9,10}; 19 insertSort2(arr, 12); 20 return 0; 21 }