直接插入排序练习:POJ 2388
来源:互联网 发布:积分商城数据分析 编辑:程序博客网 时间:2024/06/07 07:15
关于直接插入排序请参看:http://128kj.iteye.com/blog/1662280
POJ2388题意:
【输入】第一行为n,接下来n行分别为一个数;
【输出】这n个数排序后的中位数
样例:
Sample Input
5
2
4
1
3
5
Sample Output
3
分析:好象用多种排序法都可以AC,前面用了堆排序,这里再用直接插入排序,主要是复习一下代码。比起堆排序,代码短多了。
排一次序后输出中位数,但效率太低了。这里先不管了。
POJ2388题意:
【输入】第一行为n,接下来n行分别为一个数;
【输出】这n个数排序后的中位数
样例:
Sample Input
5
2
4
1
3
5
Sample Output
3
分析:好象用多种排序法都可以AC,前面用了堆排序,这里再用直接插入排序,主要是复习一下代码。比起堆排序,代码短多了。
排一次序后输出中位数,但效率太低了。这里先不管了。
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner in=new Scanner(System.in);
- int n=in.nextInt();
- int[] array =new int[n];
- for(int i=0;i<n;i++)
- array[i]=in.nextInt();
- sort(array);
- System.out.println(array[n / 2 ]);
- //for(int el : array) {
- // System.out.print(el + " ");
- //}
- }
- static void sort(int[] array) {
- int temp;
- int i,j;
- int length = array.length;
- for(i = 1; i < length; i++) {
- temp = array[i];
- for(j = i-1; j >=0; j--) {
- if(temp < array[j]) {
- array[j+1] = array[j];
- } else {
- break;
- }
- }
- array[j+1] = temp;
- }
- }
- }
import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); int n=in.nextInt(); int[] array =new int[n]; for(int i=0;i<n;i++) array[i]=in.nextInt(); sort(array); System.out.println(array[n / 2 ]); //for(int el : array) { // System.out.print(el + " "); //} } static void sort(int[] array) { int temp; int i,j; int length = array.length; for(i = 1; i < length; i++) { temp = array[i]; for(j = i-1; j >=0; j--) { if(temp < array[j]) { array[j+1] = array[j]; } else { break; } } array[j+1] = temp; } } }
- 直接插入排序练习:POJ 2388
- 直接插入排序 练习
- Scala练习-直接插入排序
- 插入排序--直接插入
- 插入排序------直接插入
- 插入排序---直接插入
- 排序:直接插入排序
- 排序---直接插入排序
- 排序 -- 直接插入排序
- 排序---直接插入排序
- 排序--直接插入排序
- 排序-直接插入排序
- 【排序】直接插入排序
- 排序--直接插入排序
- 排序:直接插入排序
- 【排序】直接插入排序
- 排序--直接插入排序
- 排序--直接插入排序
- Objective-C对象之类对象和元类对象(一)
- Java @Override报错
- 经纬度,将度分秒转换为度
- 《易道客》源码剖析之二:模块的定义和使用
- java中的String... args
- 直接插入排序练习:POJ 2388
- java执行Linux命令 .
- iOS开发中@property的属性介绍
- jquerymobile-18 Dialog
- 堆排序练习:POJ 2388
- 2012年黑龙江省测绘地理信息质检人员暨国家2000大地坐标系培训合格人员名单
- 利用Java Reflect机制编写万能toString()方法
- URL去重
- 连接测试用例与待测系统