快速排序算法无法正常工作

时间:2014-02-19 15:22:31

标签: java algorithm sorting data-structures

我已经创建了一个用于快速排序的代码,唯一的问题是有时它会发出排序的数组,有时它却没有。知道为什么会这样做吗?总是显而易见的第一部分..它显示数组要被排序的部分,但下一部分偶尔出现在我运行它很多时间之后。

这是我的代码的一部分:

public static final int max = 10;

public static void main(String[] args) {
    int[] toSortArray = new int[max];

    for (int i = 0; i < max; i++) {
        toSortArray[i] = (int) (Math.random() * 100);
    }

    System.out.println("The array to be sorted is:");

    for (int i = 0; i < max; i++) {
        System.out.print(" | " + toSortArray[i]);
    }
    System.out.println(" | ");

    // Beginning of the algorithm
    quicksortHelper(toSortArray, 0, max - 1);
    // End of the algorithm

    System.out.println("The sorted array is: ");
    for (int i = 0; i < max; i++) {
        System.out.print(" | " + toSortArray[i]);
    }
    System.out.println(" | ");
}

private static void quicksortHelper(int[] toSortArray, int first, int last) {
    if (first < last) {
        int splitpoint = partition(toSortArray, first, last);
        quicksortHelper(toSortArray, first, splitpoint - 1);
        quicksortHelper(toSortArray, splitpoint + 1, last);
    }
}

private static int partition(int[] toSortArray, int first, int last) {
    int pivot = toSortArray[first];
    int leftmark = first + 1;
    int rightmark = last;
    boolean done = true;

    while (done) {
        while (leftmark <= rightmark && toSortArray[leftmark] < pivot) {
            leftmark++;
        }

        while (leftmark <= rightmark && toSortArray[rightmark] > pivot) {
            rightmark--;
        }

        if (leftmark > rightmark) {
            done = false;
        } else {
            int temp = toSortArray[leftmark];
            toSortArray[leftmark] = toSortArray[rightmark];
            toSortArray[rightmark] = temp;
        }
    }

    int temp = toSortArray[rightmark];
    toSortArray[rightmark] = toSortArray[first];
    toSortArray[first] = temp;
    return rightmark;
}

2 个答案:

答案 0 :(得分:0)

尝试更改

    while (leftmark <= rightmark && toSortArray[leftmark] < pivot) {
        leftmark++;
    }

    while (leftmark <= rightmark && toSortArray[leftmark] <= pivot) {
        leftmark++;
    }

,或者

    while (leftmark <= rightmark && toSortArray[rightmark] > pivot) {
        rightmark--;
    }

    while (leftmark <= rightmark && toSortArray[rightmark] >= pivot) {
        rightmark--;
    }

(也就是说,将<更改为<= >更改为>=。)

这将确保在涉及非唯一值时不会退出。

答案 1 :(得分:0)

这是我从你的代码编写的一个程序,它运行良好

class QuickSort{
  public static final int max = 10;

  public static void main(String[] args) {
    int[] toSortArray = new int[max];

    for (int i = 0; i < max; i++) {
      toSortArray[i] = (int) (Math.random() * 100);
    }

    System.out.println("The array to be sorted is:");

    for (int i = 0; i < max; i++) {
      System.out.print(" | " + toSortArray[i]);
    }
    System.out.println(" | ");

    // Beginning of the algorithm
    quicksortHelper(toSortArray, 0, max - 1);
    // End of the algorithm

    System.out.println("The sorted array is: ");
    for (int i = 0; i < max; i++) {
      System.out.print(" | " + toSortArray[i]);
    }
    System.out.println(" | ");
  }

  private static void quicksortHelper(int[] toSortArray, int first, int last) {
    if (first < last) {
      int splitpoint = partition(toSortArray, first, last);
      quicksortHelper(toSortArray, first, splitpoint - 1);
      quicksortHelper(toSortArray, splitpoint + 1, last);
    }
  }

  private static int partition(int[] array, int first, int last) {
    int temp;
    // Always assumes pivot index is first
    int pivot = array[first];

    // Swap Pivot and Last
    array[first] = array[last];
    array[last] = pivot;

    int mark = first;

    for(int i=first;i<last;i++){
      if(array[i] <= pivot){
        temp = array[mark];
        array[mark] = array[i];
        array[i] = temp;
        mark++;
      }
    }

    temp = array[last];
    array[mark] = array[last];
    array[last] = temp;

    return mark;
  }
}