2 Select Sort
阿新 • • 發佈:2018-12-11
#include <stdio.h> void PrintArray(int *a, int len); int SelectSort(int *a, int len); int HeapSort(int *a, int len, int d); #define LENGTH 10 int array[LENGTH] = {4, 1, 5, 7, 9, 8, 6, 0, 3, 2}; //int array[LENGTH] = {2, 3, 4, 1, 9, 8, 6, 0, 3, 2}; void main() { PrintArray(array, LENGTH); SelectSort(array, LENGTH);PrintArray(array, LENGTH); } void PrintArray(int *a, int len) { printf("\n"); int i; for(i = 0; i < len; i++) printf("%d ", a[i]); //printf("a[%d] = %d\n", i, a[i]); printf("\n"); } int SelectSort(int *a, int len) { int i = 0, j = 0, k = 0; int temp; for(i = 0; i < len; i++) { temp = a[i]; for(j = i; j < len; j++) { if(a[j] < temp) { temp = a[j]; k = j; } } printf("k=%d temp=%d\n", k, temp); if(temp != a[i]) { k--; while(k >= i) { a[k+1] = a[k]; k--; } a[k+1] = temp; } // PrintArray(a, LENGTH); } }