数据结构------选择排序
原理:参考趣学数据结构
代码:
#include<stdio.h>
#include<stdlib.h>
void simpleSelectSort(int a[], int length) {//选择排序int j;for (int i = 0; i < length - 1; i++) {int k = i;//每趟排序中找到最小的元素int tempMin = a[k];for (j = i + 1; j < length; j++) {if (a[j] < tempMin) {k = j;tempMin = a[j];}}if (k != i) {//在后面找到了第i+1小的元素int temp = a[k];a[k] = a[i];a[i] = temp;}}
}
void print6(int a[], int length) {for (int i = 0; i < length; i++) {printf("%d ", a[i]);}printf("\n");
}
int main() {int a[] = { 7,5,4,22,6,8,9,43,2,1 };int length = sizeof(a) / sizeof(a[0]);printf("排序前:\n");print6(a, length);simpleSelectSort(a, length);printf("排序后:\n");print6(a, length);system("pause");return 0;
}
测试截图:
