package com.jxufe.argorithem;
import java.awt.Insets;
/**
* 插入排序
* @author jack_one_one
* @version 创建时间:2016年5月5日 下午1:00:21
*
*/
public class InsertSort {
/**
* 插入排序的具体实现方法
*@param array 排序的数组
*/
public static void sort(int[] array){
for (int i = 1; i < array.length; i++) {
int temp = array[i];//用一个变量来临时存储要插入的数
/*
* 寻找插入的位子
*/
int j;
for( j=i-1;j>=0 && array[j]>temp;j--){
array[j+1] = array[j];
}
/**
* 找到这个位置并把这数插入进去
*/
array[j+1] = temp;
}
}
public static void main(String[] args) {
int[] array={324,12,3,23434,1,343};
InsertSort.sort(array);
for(int i : array){
System.out.print(i+" ");
}
}
}
结果: