选择排序

Tags
排序
每次选出最小的元素,排到最前面
#include <stdio.h> int main(int argc, char *argv[]) { int arr[] = {5, 2, 9, 3, 7, 1, 4, 8, 6, 0}; int len = sizeof(arr) / sizeof(arr[0]); int i, j, min_index, temp; for (i = 0; i < len; i++) { min_index = i; for (j = i + 1; j < len; j++) { if (arr[j] < arr[min_index]) { min_index = j; } } if (min_index != i) { temp = arr[i]; arr[i] = arr[min_index]; arr[min_index] = temp; } } for (i = 0; i < len; i++) { printf("%d ", arr[i]); } printf("\n"); return 0; }