`
js_hum
  • 浏览: 17974 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
社区版块
存档分类
最新评论

InsertionSrot

 
阅读更多

 

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 + "毫秒");

}

}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics