选择排序解析 + 代码实现(C语言)
Posted 锋少的技术博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了选择排序解析 + 代码实现(C语言)相关的知识,希望对你有一定的参考价值。
选择排序解析 + 代码实现(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
以上是关于选择排序解析 + 代码实现(C语言)的主要内容,如果未能解决你的问题,请参考以下文章
8种面试经典!排序详解--选择,插入,希尔,冒泡,堆排,3种快排,快排非递归,归并,归并非递归,计数(图+C语言代码+时间复杂度)
8种面试经典排序详解--选择,插入,希尔,冒泡,堆排,3种快排及非递归,归并及非递归,计数(图+C语言代码+时间复杂度)
8种面试经典排序详解--选择,插入,希尔,冒泡,堆排,3种快排及非递归,归并及非递归,计数(图+C语言代码+时间复杂度)