插入算法
- #include <stdlib.h>
- #include <stdio.h>
- void simple_insertSort(int array[], int n)
- {
- int i, j;
- int temp;
- for(i = 1; i < n; ++i)
- {
- temp = array[i];
- j = i - 1;
- while(j >= 0 && temp < array[j])
- {
- array[j+1] = array[j];
- --j;
- }
- array[j+1] = temp;
- }
- }
- int main()
- {
- int array[] = {5, 15, 3, 20, 11};
- simple_insertSort(array, sizeof(array)/sizeof(int));
- for(int i = 0; i < 5; ++i)
- printf("%d ", array[i]);
- printf("\n");
- }