选择排序解析 + 代码实现(C语言)
选择排序思路:若是正序(从小到大)排列,从第一个元素开始,用每一个元素和当前元素后面的元素进行比较,若当前元素值大于后面的元素,则交换位置,否则继续往后比较。
C语言代码实现
#include <stdio.h>int main(int argc, const char * argv[]) {int arr[10] = {1,2,6,5,4,3,9,8,7,0}; //定义一个装10个整型元素的数组,并初始化for (int i = 0; i < 9; i++) { //从第一个元素开始,每一个元素(下标为i)和后面的元素进行比较for (int j = i + 1; j < 10; j++) { //i后面的元素if (arr[i] > arr[j]) {int temp = arr[i];arr[i] = arr[j];arr[j] = temp;}}// 每一趟比较过程打印for (int i = 0; i < 10; i++) {printf(" =>%d",arr[i]);}printf("\n");}return 0;}// 打印结果=>0 =>2 =>6 =>5 =>4 =>3 =>9 =>8 =>7 =>1=>0 =>1 =>6 =>5 =>4 =>3 =>9 =>8 =>7 =>2=>0 =>1 =>2 =>6 =>5 =>4 =>9 =>8 =>7 =>3=>0 =>1 =>2 =>3 =>6 =>5 =>9 =>8 =>7 =>4=>0 =>1 =>2 =>3 =>4 =>6 =>9 =>8 =>7 =>5=>0 =>1 =>2 =>3 =>4 =>5 =>9 =>8 =>7 =>6=>0 =>1 =>2 =>3 =>4 =>5 =>6 =>9 =>8 =>7=>0 =>1 =>2 =>3 =>4 =>5 =>6 =>7 =>9 =>8=>0 =>1 =>2 =>3 =>4 =>5 =>6 =>7 =>8 =>9