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

查看所有标签

Advanced Web Metrics with Google Analytics

Advanced Web Metrics with Google Analytics

Brian Clifton / Sybex / 2008 / USD 39.99

Are you getting the most out of your website? Google insider and web metrics expert Brian Clifton reveals the information you need to get a true picture of your site's impact and stay competitive usin......一起来看看 《Advanced Web Metrics with Google Analytics》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

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

html转js在线工具