选择排序是一种攻击性算法,用于从数组中找到最小的数字,然后将其放置到第一个位置。下一个要遍历的数组将从索引开始,靠近放置最小数字的位置。
选择元素列表中第一个最小的元素并将其放置在第一个位置。
对列表中的其余元素重复相同的操作,直到所有元素都获得已排序。
考虑以下列表 -

立即学习“C语言免费学习笔记(深入)”;
Sm = a[0] = 30 Sm
a[1]
a[2]
a[3]
Shell本身是一个用C语言编写的程序,它是用户使用Linux的桥梁。Shell既是一种命令语言,又是一种程序设计语言。作为命令语言,它交互式地解释和执行用户输入的命令;作为程序设计语言,它定义了各种变量和参数,并提供了许多在高级语言中才具有的控制结构,包括循环和分支。它虽然不是Linux系统核心的一部分,但它调用了系统核心的大部分功能来执行程序、建立文件并以并行的方式协调各个程序的运行。因此,对于用户来说,shell是最重要的实用程序,深入了解和熟练掌握shell的特性极其使用方法,是用好Linux系统
24
a[4]
10 50 40 30 20

Sm = a[1] = 50 sm
a[2]
a[3]
a[4]
10 20 40 30 50

Sm = a[2] = 40 Sm
a[3] < sm $\square$ $\square$ 30 <40 (T) $\square$ $\square$ 40
a[4] < sm $\square$ $\square$ 50<40 (F) $\square$ $\square$ 30 exchange a[2] with sm value
的中文翻译为:a[3] < sm $\square$ $\square$ 30 <40 (T) $\square$ $\square$ 40
a[4] < sm $\square$ $\square$ 50<40 (F) $\square$ $\square$ 30 用sm值交换a[2]
10 20 30 40 50

Sm = a[3] = 40 Sm
a[4] < $\square$ $\square$ sm 50 <40 (F) $\square$ 40 $\square$ exchange a[3] with sm value
请参考下面的步骤进行选择排序。
for (i=0; i<n-1; i++){
sm=i;
for (j=i+1; j<n; j++){
if (a[j] < a[sm])
sm=j;
}
t=a[i];
a[i] = a[sm];
a[sm] = t;
}
}以下是选择排序技术的C程序−
#include<stdio.h>
int main(){
int a[50], i,j,n,t,sm;
printf("enter the No: of elements in the list:</p><p>");
scanf("%d", &n);
printf("enter the elements:</p><p>");
for(i=0; i<n; i++){
scanf ("%d", &a[i]);
}
for (i=0; i<n-1; i++){
sm=i;
for (j=i+1; j<n; j++){
if (a[j] < a[sm]){
sm=j;
}
}
t=a[i];
a[i]=a[sm];
a[sm]=t;
}
printf ("after selection sorting the elements are:</p><p>");
for (i=0; i<n; i++)
printf("%d\t", a[i]);
return 0;
}当执行上述程序时,会产生以下结果 -
enter the No: of elements in the list: 4 enter the elements: 45 12 37 68 after selection sorting the elements are: 12 37 45 68
以上就是解释C语言中选择排序的过程的详细内容,更多请关注php中文网其它相关文章!
C语言怎么学习?C语言怎么入门?C语言在哪学?C语言怎么学才快?不用担心,这里为大家提供了C语言速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号