锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
public class Sort {
/**
* 鎻掑叆鎺掑簭鐨勫熀鏈濇兂涓猴細(xì)棣栧厛瀵繪壘涓涓湁搴忔暟鍒楋紝鐒跺悗灝嗘暟緇勪腑鐨勬瘡涓厓绱犳彃鍏ュ埌璇ユ湁搴忓簭鍒椾腑錛?br />
* 鍒欒鏁扮粍搴忓垪鍗沖彲鍙樹負(fù)鏈夊簭鏁板垪銆傚叿浣撳疄鏂藉姙娉曚負(fù)錛岄閫夊皢絎竴涓厓绱犵湅浣滄槸涓涓湁搴忓簭鍒楋紝鐒跺悗
* 浠庣浜屼釜鍏冪礌寮濮嬮亶鍘嗘暟緇勶紝灝嗘瘡涓厓绱犳彃鍏ュ埌浠庣涓涓厓绱犲埌鍓嶄竴涓厓绱犵殑鏈夊簭搴忓垪涓紝鍗沖彲瀹?br />
* 鎴愭帓搴忋?br />
* @param temp
*/
/*
public static void insertSort(int[] temp)
{
int length = temp.length;
for (int i = 1; i < length; i++) // 鎶婄涓涓厓绱犵湅浣滀竴涓湁搴忓簭鍒楋紝浠庣浜屼釜鍏冪礌寮濮嬮亶鍘?br />
{
int tempNo = temp[i];
for (int j = 0; j < i; j++)
{
if (tempNo < temp[j])
{
for (int k = i; k > j; k--) // 灝嗗叾閬嶅巻鏁板拰姣旇緝鏁頒箣闂寸殑鏁頒緷嬈″悜鍚庣Щ鍔ㄤ竴浣?br />
temp[k] = temp[k-1];
temp[j] = tempNo;
}
}
}
}
*/
/**
* javaeye涓婄湅鍒扮殑鍙﹀涓縐嶅啓娉曪紝涓嶅悓涔嬪鏄叾涓庡墠杈規(guī)暟瀛椾竴涓竴涓瘮杈冿紝鐒跺悗涓嬈′竴嬈¢愭笎縐誨姩銆?br />
*/
/*
public static void insertSort(int[] a)
{
for(int i = 1; i < a.length; i++)
{
int temp = a[i];
int j = i - 1;
while (j >= 0 && temp < a[j])
{
a[j+1] = a[j];
j--;
}
a[j+1] = temp;
}
}
*/
/**
* 鏁版嵁緇撴瀯涔︿笂鍘熺増綆楁硶鐨刯ava浠g爜瀹炵幇
*/
public static void insertSort(int[] temp)
{
int j = 0;
int length = temp.length;
int[] a = new int[length+1];
System.arraycopy(temp, 0, a, 1, length);
for(int i = 2; i < a.length; i++)
{
if(a[i] < a[i-1])
{
a[0] = a[i];
a[i] = a[i-1];
for (j = i - 2; a[i] < a[i-1]; j--)
{
a[j+1] = a[j];
}
a[j+1] = a[0];
}
}
for (int i = 1; i < a.length; i++)
{
System.out.println(a[i]);
}
}
/**
* 鎶樺崐鎻掑叆鎺掑簭綆楁硶鐨刯ava瀹炵幇
* @param temp
*/
public static void bInsertSort(int[] temp)
{
int length = temp.length;
for (int i = 1; i < length; i++)
{
int tempVal = temp[i];
int low = 0;
int high = i - 1;
while (low <= high)
{
int middle = (low + high) / 2;
if (tempVal < temp[middle])
high = middle - 1;
else
low = middle + 1;
}
for (int j = i; j > high + 1; j--)
temp[j] = temp[j-1];
temp[high+1] = tempVal;
}
}
public static void buddleSort(int[] temp)
{
for (int i = 0; i < temp.length - 1; i++)
{
for (int j = i + 1; j < temp.length; j++)
{
if (temp[i] > temp[j])
{
int tempVal = temp[i];
temp[i] = temp[j];
temp[j] = tempVal;
}
}
}
}
public static void main(String[] args)
{
int a[] = {113, 3, 24, 24, 78, 96};
Sort.bInsertSort(a);
//Sort.insertSort(a);
//Sort.buddleSort(a);
for (int i = 0; i < a.length; i++)
{
System.out.println(a[i]);
}
}
}