1222:排序
来源:互联网 发布:淘宝新开店铺图标 编辑:程序博客网 时间:2024/05/20 11:24
1222:排序
Description
排序算法是程序设计中使用地非常频繁的一类算法,下面,就请你写一排序算法,对一整型数组进行升序或降序排序。
Input
多组输入
输入数据由三行组成,第一行只有一个数字,代表了这个数组的长度;第二行也只有一个数字,取值有两种:0或1,若为0,则表示使用升序排序,若为1则表示使用降序排序;第三行由若干数字组成,表示待排序的数据,每个数据用回车分隔.
Output
排好序的数据,每个数据单独占一行。
Sample Output
5
0
5 4 3 2 1
Sample Output
1
2
3
4
5
HINT
(注:程序中待排序的数据都为 int
类型,数据的数量在(0,32767]之间,每个数据的取值范围是[0,32767];每两个数据都不会相同。)
#include<iostream>#include<stdio.h>using namespace std;int main(){ int n,a,i,j; while(1==scanf("%d",&n)) { cin>>a; int ch[100]; for(i=0; i<n; i++) { cin>>ch[i]; } int temp,index; for(i=0; i<n-1; i++) { index=i; for(j=i+1;j<n;j++) { if(ch[j]<ch[index]) index=j; if(index!=i) { temp=ch[i];ch[i]=ch[index];ch[index]=temp; } } } if(a==0) { for(i=0;i<n;i++) cout<<ch[i]<<endl; } else { for(i=n-1;i>=0;i--) cout<<ch[i]<<endl; } } return 0; }
阅读全文
0 0
- 1222: 排序
- 1222:排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- GBDT(Gradient Boosting Decision Tree 梯度提升/迭代树)算法
- 更快更低耗!Spread表格控件V11 CTP发布,性能巨大提升
- Softmax 回归模型
- MacOS 上的 NSCell(比较 NSButton 和 NSButtonCell )
- 前台、后台ui的选择
- 1222:排序
- 权限系统--敢不敢从建工程开始
- H5 牛牛源码 带后台控制的
- 模板template
- 【LeetCode】669 Trim a Binary Search Tree
- 使用Toolbar + DrawerLayout实现侧滑和改变toolbar左边按钮颜色
- Android -Logcat:you must supply a layout_width attribute
- uva 340 Master-Mind Hints 模拟
- 带缩略图的淘宝风轮播图