C 练习实例37 - 排序

C 语言教程 · 2019-02-21 21:13:02

题目:对10个数进行排序。

程序分析:可以利用选择法,即从后9个比较过程中,选择一个最小的与第一个元素交换, 下次类推,即用第二个元素与后8个进行比较,并进行交换。

实例

// Created by www.codercto.com on 15/11/9. // Copyright © 2015年 码农教程. All rights reserved. // #include<stdio.h> #define N 10 int main() { int i,j,a[N],temp; printf("请输入 10 个数字:\n"); for(i=0;i<N;i++) scanf("%d",&a[i]); for(i=0;i<N-1;i++) { int min=i; for(j=i+1;j<N;j++) if(a[min]>a[j]) min=j; if(min!=i) { temp=a[min]; a[min]=a[i]; a[i]=temp; } } printf("排序结果是:\n"); for(i=0;i<N;i++) printf("%d ",a[i]); printf("\n"); return 0; }

以上实例输出结果为:

请输入 10 个数字:
23 2 27 98 234 1 4 90 88 34
排序结果是:
1 2 4 23 27 34 88 90 98 234 

点击查看所有 C 语言教程 文章: https://codercto.com/courses/l/17.html

查看所有标签

Effective Java

Effective Java

Joshua Bloch / Addison-Wesley Professional / 2018-1-6 / USD 54.99

The Definitive Guide to Java Platform Best Practices—Updated for Java 9 Java has changed dramatically since the previous edition of Effective Java was published shortly after the release of Jav......一起来看看 《Effective Java》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

html转js在线工具
html转js在线工具

html转js在线工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具