Java快速排序优化

时间:2011-12-04 05:36:36

标签: java

我有三节课。每个都创建一个包含1000个int值的数组。

  • a类:使用QuickSort
  • class b:使用QuickSort,直到每个分区的大小都小于10,然后执行InsertSort以对较小的分区进行排序。
  • class c :(这是我遇到的问题):与b类相同,只是在整个几乎排序的数组上执行InsertSort。

似乎c类只是b类代码的一个细微变化(基本上增加了类a)。我只是不知道如何实现这一目标......帮助!谢谢你提前......

A类:

import java.util.Arrays;
import java.util.Random;

public class QuickSort {

private static final Random random = new Random();
private static final int RANDOM_INT_RANGE = 9999;

private static int[] randomArray(int size) {

    // Randomize data (array)
    final int[] arr = new int[size];
    for (int i = 0; i < arr.length; i++) {
        arr[i] = random.nextInt(RANDOM_INT_RANGE);
    }
    return arr;
}

// Sort
private static void sort(int[] arr) {
    if (arr.length > 0)
        sortInPlace(arr, 0, arr.length - 1);
}

private static void sortInPlace(int[] arr, int left, int right) {

    if (left >= right)
        return; // sorted

    final int range = right - left + 1;
    int pivot = random.nextInt(range) + left;

    int newPivot = partition(arr, left, right, pivot);

    sortInPlace(arr, left, newPivot - 1);
    sortInPlace(arr, newPivot + 1, right);

}

private static int partition(int[] arr, int left, int right, int pivot) {

    int pivotVal = arr[pivot];
    swapArrayVals(arr, pivot, right);

    int storeIndex = left;
    for (int i = left; i <= (right - 1); i++) {
        if (arr[i] < pivotVal) {
            swapArrayVals(arr, i, storeIndex);
            storeIndex++;
        }
    }

    swapArrayVals(arr, storeIndex, right);

    return storeIndex;
}

private static void swapArrayVals(int[] arr, int from, int to) {
    int fromVal = arr[from];
    int toVal = arr[to];
    arr[from] = toVal;
    arr[to] = fromVal;
}

public static void main(String[] args) {

    long StartTime = System.nanoTime();

    // Array size
    int[] arr = randomArray(1000);
    int[] copy = Arrays.copyOf(arr, arr.length);

    // Print original data (array)
    System.out.println("The starting/unsorted array: \n"
            + Arrays.toString(arr));

    sort(arr);

    // check the result
    Arrays.sort(copy);
    if (Arrays.equals(arr, copy)) {
        System.out.println("The ending/sorted array: \n"
                + Arrays.toString(arr));

        // print time
        long TotalTime = System.nanoTime() - StartTime;
        System.out.println("Total elapsed time (milliseconds) " + "is: "
                + TotalTime);
    }

}
}

班级b:

import java.util.Arrays;
import java.util.Random;

public class OptQSort1 {
private static final Random random = new Random();
private static final int RANDOM_INT_RANGE = 9999;

private static int[] randomArray(int size) {

    // Randomize data (array)
    final int[] arr = new int[size];
    for (int i = 0; i < arr.length; i++) {
        arr[i] = random.nextInt(RANDOM_INT_RANGE);
    }
    return arr;
}

// Sort
private static void sort(int[] arr) {
    if (arr.length > 0)
        sortInPlace(arr, 0, arr.length - 1);
}

private static void sortInPlace(int[] arr, int left, int right) {

    boolean insertionSortCalled = false;

    // OptQSort1:
    int size = right - left + 1;
    if (size < 10 && !insertionSortCalled) {
        insertionSortCalled = true;
        insertionSort(arr, 0, arr.length - 1);
    }

    if (left >= right)
        return; // sorted

    final int range = right - left + 1;
    int pivot = random.nextInt(range) + left;

    int newPivot = partition(arr, left, right, pivot);

    sortInPlace(arr, left, newPivot - 1);
    sortInPlace(arr, newPivot + 1, right);

}

private static int partition(int[] arr, int left, int right, int pivot) {

    int pivotVal = arr[pivot];
    swapArrayVals(arr, pivot, right);

    int storeIndex = left;
    for (int i = left; i <= (right - 1); i++) {
        if (arr[i] < pivotVal) {
            swapArrayVals(arr, i, storeIndex);
            storeIndex++;
        }
    }

    swapArrayVals(arr, storeIndex, right);

    return storeIndex;
}

private static void swapArrayVals(int[] arr, int from, int to) {
    int fromVal = arr[from];
    int toVal = arr[to];
    arr[from] = toVal;
    arr[to] = fromVal;
}

public static void insertionSort(int[] arr, int left, int right) {
    int in, out;

    for (out = left + 1; out <= right; out++) {
        int temp = arr[out];
        in = out;

        while (in > left && arr[in - 1] >= temp) {
            arr[in] = arr[in - 1];
            --in;
        }
        arr[in] = temp;
    }

}

public static void main(String[] args) {

    long StartTime = System.nanoTime();

    // Array size
    int[] arr = randomArray(1000);
    int[] copy = Arrays.copyOf(arr, arr.length);

    // Print original data (array)
    System.out.println("The starting/unsorted array: \n"
            + Arrays.toString(arr));

    sort(arr);

    // check the result
    Arrays.sort(copy);
    if (Arrays.equals(arr, copy)) {
        System.out.println("The ending/sorted array: \n"
                + Arrays.toString(arr));

        // print time
        long TotalTime = System.nanoTime() - StartTime;
        System.out.println("Total elapsed time (milliseconds) " + "is: "
                + TotalTime);
    }
}

}

C类:

import java.util.Arrays;
import java.util.Random;

public class OptQSort2 {

private static final Random random = new Random();
private static final int RANDOM_INT_RANGE = 9999;

private static int[] randomArray(int size) {

    // Randomize data (array)
    final int[] arr = new int[size];
    for (int i = 0; i < arr.length; i++) {
        arr[i] = random.nextInt(RANDOM_INT_RANGE);
    }
    return arr;
}

// Sort
private static void sort(int[] arr) {
    if (arr.length > 0)
        sortInPlace(arr, 0, arr.length - 1);
    insertionSort(arr, 0, arr.length - 1);
}

private static void sortInPlace(int[] arr, int left, int right) {

    // OptQSort2:
    int size = right - left + 1;

    if (size < 10)
        return;

    if (left >= right)
        return; // sorted

    final int range = right - left + 1;
    int pivot = random.nextInt(range) + left;

    int newPivot = partition(arr, left, right, pivot);

    sortInPlace(arr, left, newPivot - 1);
    sortInPlace(arr, newPivot + 1, right);

}

private static int partition(int[] arr, int left, int right, int pivot) {

    int pivotVal = arr[pivot];
    swapArrayVals(arr, pivot, right);

    int storeIndex = left;
    for (int i = left; i <= (right - 1); i++) {
        if (arr[i] < pivotVal) {
            swapArrayVals(arr, i, storeIndex);
            storeIndex++;
        }
    }

    swapArrayVals(arr, storeIndex, right);

    return storeIndex;
}

private static void swapArrayVals(int[] arr, int from, int to) {
    int fromVal = arr[from];
    int toVal = arr[to];
    arr[from] = toVal;
    arr[to] = fromVal;
}

public static void insertionSort(int[] arr, int left, int right) {
    int in, out;

    for (out = left + 1; out <= right; out++) {
        int temp = arr[out];
        in = out;

        while (in > left && arr[in - 1] >= temp) {
            arr[in] = arr[in - 1];
            --in;
        }
        arr[in] = temp;
    }

}

public static void main(String[] args) {

    // Start the clock
    long StartTime = System.nanoTime();

    // Array size
    int[] arr = randomArray(1000);
    int[] copy = Arrays.copyOf(arr, arr.length);

    // Print original data (array)
    System.out.println("The starting/unsorted array: \n"
            + Arrays.toString(arr));

    sort(arr);

    // check the result
    Arrays.sort(copy);
    if (Arrays.equals(arr, copy)) {
        System.out.println("The ending/sorted array: \n"
                + Arrays.toString(arr));

        // print time
        long TotalTime = System.nanoTime() - StartTime;
        System.out.println("Total elapsed time (milliseconds) " + "is: "
                + TotalTime);
    }
}

}

2 个答案:

答案 0 :(得分:2)

你说c类只是实现了insert-sort,没有快速排序,对吗?

然后原则上,c类可以只是b类,用这一行:

    sort(arr);

改为:

    insertionSort(arr, 0, arr.length);

(然后你想要开始剥离很多代码 - 删除从未调用的方法,修改insertionSort方法以假设left0和{ {1}}为right而不是要求指定它们,将arr.length方法重命名为insertionSort等。

顺便说一句,c类实际上比你已经设法创建的类容易得多。你可能只需要睡一觉。你早上没问题。 : - )

答案 1 :(得分:1)

创建class c copy class b然后按以下方式更改它: 添加实例变量insertionSortCalled,这样就不会在不同的递归调用中多次调用插入排序

 boolean insertionSortCalled= false;

并将此部分更改为以下内容:insertionSort(arr,0,arr.length-1);

private static void sortInPlace(int[] arr, int left, int right) {

// OptQSort1:
int size = right - left + 1;
**if (size < 10 && !insertionSortCalled){**
      **insertionSortCalled=true;**
      **insertionSort(arr, 0, arr.length-1);**
  }

if (left >= right)
    return; // sorted

final int range = right - left + 1;
int pivot = random.nextInt(range) + left;

int newPivot = partition(arr, left, right, pivot);

sortInPlace(arr, left, newPivot - 1);
sortInPlace(arr, newPivot + 1, right);

}