Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 快速排序的两种实现在时间上有很大差异_Java_Algorithm_Quicksort - Fatal编程技术网

Java 快速排序的两种实现在时间上有很大差异

Java 快速排序的两种实现在时间上有很大差异,java,algorithm,quicksort,Java,Algorithm,Quicksort,我有两个快速排序的实现,第一个使用中间值(fist,middle,last)作为轴心,第二个使用中间元素作为轴心 第一次实施: public class quickMedian { public void sort(int array[]) // pre: array is full, all elements are non-null integers // post: the array is sorted in ascending order { quickSort(a

我有两个快速排序的实现,第一个使用中间值(fist,middle,last)作为轴心,第二个使用中间元素作为轴心 第一次实施:

public class quickMedian {
     public void sort(int array[]) 
// pre: array is full, all elements are non-null integers
// post: the array is sorted in ascending order
{
    quickSort(array, 0, array.length - 1);              // quicksort all the elements in the array
} 
public void quickSort(int array[], int start, int end)
{
        int i = start;                          // index of left-to-right scan
        int k = end;                            // index of right-to-left scan

        if (end - start >= 1)                   // check that there are at least two elements to sort
        {
                if (array[start+(end-start)/2]>array[end]){
                    swap(array,start+(end-start)/2, end);
                }
                if (array[start]>array[end]){
                    swap(array,start, end);
                }
                if (array[start+(end-start)/2]>array[start]){
                    swap(array, start+(end-start)/2, start);
                }

                int pivot = array[start];       // set the pivot as the first element in the partition

                while (k > i)                   // while the scan indices from left and right have not met,
                {
                        while (array[i] <= pivot && i <= end && k > i)  // from the left, look for the first
                                i++;                                    // element greater than the pivot
                        while (array[k] > pivot && k >= start && k >= i) // from the right, look for the first
                            k--;                                        // element not greater than the pivot
                        if (k > i)                                       // if the left seekindex is still smaller than
                                swap(array, i, k);                      // the right index, swap the corresponding elements
                }
                swap(array, start, k);          // after the indices have crossed, swap the last element in                                               // the left partition with the pivot 
                quickSort(array, start, k - 1); // quicksort the left partition
                quickSort(array, k + 1, end);   // quicksort the right partition
        }
        else    // if there is only one element in the partition, do not do any sorting
        {
                return;                     // the array is sorted, so exit
        }
}

public void swap(int array[], int index1, int index2) 
// pre: array is full and index1, index2 < array.length
// post: the values at indices 1 and 2 have been swapped
{
    int temp = array[index1];           // store the first value in a temp
    array[index1] = array[index2];      // copy the value of the second into the first
    array[index2] = temp;               // copy the value of the temp into the second
}


}
public class quickSort {
      private int array[];
    private int length;

    public void sort(int[] inputArr) {

        if (inputArr == null || inputArr.length == 0) {
            return;
        }
        this.array = inputArr;
        length = inputArr.length;
        quickSorter(0, length - 1);
    }

    private void quickSorter(int lowerIndex, int higherIndex) {

        int i = lowerIndex;
        int j = higherIndex;

        // calculate pivot number, I am taking pivot as middle index number
        int pivot = array[lowerIndex+(higherIndex-lowerIndex)/2];
        // Divide into two arrays
        while (i <= j) {
            /**
             * In each iteration, we will identify a number from left side which
             * is greater then the pivot value, and also we will identify a number
             * from right side which is less then the pivot value. Once the search
             * is done, then we exchange both numbers.
             */
            while (array[i] < pivot) {
                i++;
            }
            while (array[j] > pivot) {
                j--;
            }
            if (i <= j) {
                exchangeNumbers(i, j);
                //move index to next position on both sides
                i++;
                j--;
            }
        }
        // call quickSort() method recursively
        if (lowerIndex < j)
            quickSorter(lowerIndex, j);
        if (i < higherIndex)
            quickSorter(i, higherIndex);
    }

    private void exchangeNumbers(int i, int j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }

}

只需复制第二个解决方案并插入函数对3个元素进行排序:sort3(int array[],int idx1,int idx2,int idx3)到开头…你是说选择pivot?第一个算法对我来说很奇怪(但第二个很好)。只需编写sort3()函数并修改第二个解决方案,如下所示:int i=lowerIndex;int j=高指数;sort3(数组,lowerIndex,lowerIndex+(higherIndex-lowerIndex)/2,higherIndex);int pivot=数组[lowerIndex+(higherIndex-lowerIndex)/2];
 public static void main(String[] args) {
    File number = new File ("f.txt");
    final int size = 10000000;

    try{
   //  quickSort s = new quickSort();
     quickMedian s = new quickMedian();
    writeTofile(number, size);
  int [] arr1 =readFromFile(number, size);
    long a=System.currentTimeMillis();
    s.sort(arr1);
    long b=System.currentTimeMillis();
    System.out.println("quickSort: "+(double)(b-a)/1000);
    }catch (Exception ex){ex.printStackTrace();}


 }