|
|
|
@ -54,7 +54,42 @@ public class SortUtil { |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
public static int[] insertSort(int[] arr){ |
|
|
|
|
int length = arr.length; |
|
|
|
|
for (int i=1;i<length;i++){ |
|
|
|
|
for (int j=i;j>0;j--){ |
|
|
|
|
//
|
|
|
|
|
if (arr[j]<arr[j-1]) { |
|
|
|
|
int temp = arr[j - 1]; |
|
|
|
|
arr[j-1] = arr[j]; |
|
|
|
|
arr[j] = temp; |
|
|
|
|
}else { |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return arr; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 希尔排序(插入排序变种版) |
|
|
|
|
* @param arr |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
public static int[] shellSort(int[] arr){ |
|
|
|
|
int length = arr.length; |
|
|
|
|
for (int i=length/2;i>0;i/=2){ |
|
|
|
|
for (int j=i;j<length;j++){ |
|
|
|
|
for (int k=j;k>0&&k-i>=0;k-=i){ |
|
|
|
|
if (arr[k]<arr[k-i]){ |
|
|
|
|
int temp = arr[k-i]; |
|
|
|
|
arr[k-i] = arr[k]; |
|
|
|
|
arr[k] = temp; |
|
|
|
|
}else { |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return arr; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|