ITeye博客

InsertionSrot

js_hum · 2012-03-20 16:13 · 蛋疼系列之排序算法 · 非技术 · Markdown

package com.lizhen.sort;

 

public class InsertionSort {

public static int length = 1000;

 

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

Integer[] num2Sort = new Integer[length];

// Integer[] after2Sort = null;

System.out.println("开始时间:" + System.currentTimeMillis());

double startTime = System.currentTimeMillis();

for (int i = 0; i < length; i++) {

num2Sort[i] = (int) (Math.random() * 10000);

System.out.print(num2Sort[i] + ",");

}

System.out.println();

 

for (int i = 1; i < length; i++) {

int temp = num2Sort[i];

int j;

for (j = i; j > 0; j--) {

if (temp < num2Sort[j - 1]) {

num2Sort[j] = num2Sort[j - 1];

} else

break;

}

num2Sort[j] = temp;

/*

* System.out.print("第" + i + "次排序:"); for (int k = 0; k <

* num2Sort.length; k++) { System.out.print(num2Sort[k] + ","); }

* System.out.println();

*/

}

for (int i = 0; i < num2Sort.length; i++) {

System.out.print(num2Sort[i] + ",");

}

System.out.println();

System.out.println("结束时间:" + System.currentTimeMillis());

double timeConsuming = System.currentTimeMillis() - startTime;

System.out.println("共用时间:" + timeConsuming + "毫秒");

}

}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。