---
title: "InsertionSrot"
author: "js_hum"
date: "2012-03-20T16:13:30Z"
tags: []
categories: ["非技术"]
slug: "insertionsrot"
draft: false
lastmod: "2012-04-06T13:31:18Z"
id: 1458749
url: "https://js-hum.iteye.com/blog/1458749"
---

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 &lt; length; i++) {

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

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

}

System.out.println();

 

for (int i = 1; i &lt; length; i++) {

int temp = num2Sort\[i];

int j;

for (j = i; j &gt; 0; j--) {

if (temp &lt; num2Sort\[j - 1]) {

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

} else

break;

}

num2Sort\[j] = temp;

/*

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

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

\* System.out.println();

\*/

}

for (int i = 0; i &lt; 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 + "毫秒");

}

}