/**
* Package: sort
*
* File: JavaSorts.java
*
*
*/
public class JavaSorts {
/**
* 希尔排序
* @param array
*/
public static void ShellSort(int[] array){
int d = array.length;
do {
d /= 2; //设置增量,通过设置增量来进行分组,分组后,每一组内采用直接插入排序
OneShell(array, d);//一个增量对应一趟希尔排序
} while (d > 1);
}
/**
* 一趟希尔排序
* @param array
* @param d
*/
public static void OneShell(int[] array,int d){
for (int i = 0; i < array.length - d; i++) {
int temp = array[i+d]; //array[i+d]的拷贝,每一次插入操作所以插入的值
if (array > array[i + d]) {
int j = i;
//此时分组为:j,j+d,j+2d,j+3d····,组内采用直接插入排序
while(j >= 0 && array[j] > temp){
array[j + d] = array[j]; //使用while循环寻找temp能够插入的位置,从右往左寻找,大于temp的往后移动
j -= d;
}
array[j + d] = temp; //插入数据
}
}
}
/**
* 基数排序
* @param array
*/
public static void RadixSort(int[] array){
int bits = FindMaxBit(array); //得到数组中最大的那个数的位数
for (int i = 0; i < bits; i++) {
OneBitSort(array, i+1); //一位基数的排序
}
}
/**
* 一位基数的排序
* @param array
* @param bit
*/
public static void OneBitSort(int[] array,int bit){
int[] tempArray = new int[array.length];
int index = 0;
int tempIndex = 0;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < array.length; j++) {
if (FindBitNum(array[j], bit) == i) {
tempArray[index++] = array[j];
}
}
}
while(tempIndex < array.length){
array[tempIndex] = tempArray[tempIndex++]; //复制数组
}
}
/**
* 得到某个数某一位的数(例如:num=1234,n=2,FindBitNum(1234,2)=3)
* @param num
* @param n
* @return
*/
public static int FindBitNum(int num,int n){
int key1 = (int)Math.pow(10, n);
int key2 = (int)Math.pow(10, n-1);
num %= key1;
num /= key2;
return num;
}
/**
* 得到一个数组中最大数的位数
* @param array
* @return
*/
public static int FindMaxBit(int[] array){
if (array == null || array.length ==0) {
return 0;
}
int max = array[0];
for (int i = 1; i < array.length; i++) {
if (array > max) {
max = array;
}
}
int bit = 0;
while(max / 10 != 0 || max % 10 != 0){
max /= 10;
bit++;
}
return bit;
/**
* Package: sort
*
* File: SortRunnable.java
*
*
*/
public class ProduceRandomNum{
public synchronized static int[] RandomArray(int num,int n){
Random random = new Random();
int[] array = new int[num];
for (int i = 0; i < array.length; i++) {
array = random.nextInt((int)Math.pow(10, n));
}
return array;
}
private static final int arrayNum = 500000;
private static final int bit = 4;
public static long getBubbleSortTime(){
long oldTime = System.currentTimeMillis();
JavaSorts.BubbleSort(ProduceRandomNum.RandomArray(arrayNum, bit));
long newTime = System.currentTimeMillis();
return newTime - oldTime;
}
public static long getQuickSortTime(){
int[] array = ProduceRandomNum.RandomArray(arrayNum, bit);
long oldTime = System.currentTimeMillis();
JavaSorts.QuickSort(array, 0, array.length - 1);
long newTime = System.currentTimeMillis();
return newTime - oldTime;
}
public static long getDirSelectSortTime(){
long oldTime = System.currentTimeMillis();
JavaSorts.DirectSelectSort(ProduceRandomNum.RandomArray(arrayNum, bit));
long newTime = System.currentTimeMillis();
return newTime - oldTime;
}
public static long getDirInsertSortTime(){
long oldTime = System.currentTimeMillis();
JavaSorts.DirectInsertSort(ProduceRandomNum.RandomArray(arrayNum, bit));
long newTime = System.currentTimeMillis();
return newTime - oldTime;
}
public static long getBinaryInsertSortTime(){
long oldTime = System.currentTimeMillis();
JavaSorts.BinaryInsertSort(ProduceRandomNum.RandomArray(arrayNum, bit));
long newTime = System.currentTimeMillis();
return newTime - oldTime;
}
public static long getShellSortTime(){
long oldTime = System.currentTimeMillis();
JavaSorts.ShellSort(ProduceRandomNum.RandomArray(arrayNum, bit));
long newTime = System.currentTimeMillis();
return newTime - oldTime;
}
public static long getMergeSortTime(){
long oldTime = System.currentTimeMillis();
JavaSorts.MergeSort(ProduceRandomNum.RandomArray(arrayNum, bit));
long newTime = System.currentTimeMillis();
return newTime - oldTime;
}
public static long getRadixSortTime(){
long oldTime = System.currentTimeMillis();
JavaSorts.RadixSort(ProduceRandomNum.RandomArray(arrayNum, bit));
long newTime = System.currentTimeMillis();
return newTime - oldTime;
}
public static long getHeapSortTime(){
long oldTime = System.currentTimeMillis();
JavaSorts.HeapSort(ProduceRandomNum.RandomArray(arrayNum, bit));
long newTime = System.currentTimeMillis();
return newTime - oldTime;
}
} 作者: gcww 时间: 2018-5-20 04:41
谢谢楼主分享