高级排序-快速排序-使用插入排序来处理小于10个数据项的子数组,使快速排序性能发挥到极致。

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Nuan_Feng/article/details/56669090

package g_advancedSort.E_quickSort3;


/**
 * 使用插入排序来处理小于10个数据项的子数组,使快速排序性能发挥到极致。
 *  一般用9作为切割点,但是最终还是要取决计算机,操作系统等。
 * 
 * @author Administrator
 * 
 */
public class QuickSortApp {
public static void main(String[] args) {
int maxSize = 16;
ArrayIns arr = new ArrayIns(maxSize);
for (int i = 0; i < maxSize; i++) {
long n = (int) (Math.random() * 199);
arr.insert(n);
}
arr.displays();
arr.quickSort();
arr.displays();
}
}


package g_advancedSort.E_quickSort3;



public class ArrayIns {
private long arr[];
private int nElems;


public ArrayIns(int max) {
arr = new long[max];
nElems = 0;
}


public void insert(long value) {
arr[nElems++] = value;
}


public void displays() {
System.out.print("A = ");
for (int i = 0; i < nElems; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}


public void quickSort() {
recQuickSort(0, nElems - 1);
}


/*
* 快速排序
*/
private void recQuickSort(int left, int right) {
int size = right - left + 1;
if (size < 10)
insertionSort(left, right);// 利用插入排序使效率最大化
else {
long median = medianOf(left, right);// 3数据项取中,并返回中间数字值,中间数字值对应的索引为right-1
int partition = partitionIt(left, right, median);// 返回中间索引
recQuickSort(left, partition);
recQuickSort(partition + 1, right);
}
}


/*
* 插入排序
*/
private void insertionSort(int left, int right) {
int in, out;
for (out = left + 1; out <= right; out++) {
long temp = arr[out];
in = out;
while (in > left && arr[in - 1] >= temp) {
arr[in] = arr[in - 1];
--in;
}
arr[in] = temp;


}


}


/*
* 返回中间索引
*/
private int partitionIt(int left, int right, long pivot) {
int leftPtr = left;//
int rightPtr = right - 1;// pivot
while (true) {
while (arr[++leftPtr] < pivot)
;
while (arr[--rightPtr] > pivot)
;
if (leftPtr >= rightPtr)
break;
else
swap(leftPtr, rightPtr);
}
swap(leftPtr, right - 1);// pivot和leftPtr交换
return leftPtr;
}


/*
* 3数据项取中,并返回中间数字值
*/
private long medianOf(int left, int right) {
int center = (left + right) / 2;
if (arr[left] > arr[center])
swap(left, center);
if (arr[left] > arr[right])
swap(left, right);
if (arr[center] > arr[right])
swap(center, right);


swap(center, right - 1);
return arr[right - 1];
}


/*
* 交换
*/
private void swap(int dex1, int dex2) {
long temp = arr[dex1];
arr[dex1] = arr[dex2];
arr[dex2] = temp;


}
}
展开阅读全文

没有更多推荐了,返回首页