快速计算比较和交换

时间:2018-10-06 19:00:38

标签: java sorting quicksort

我正在对从文件中读取的2000个整数进行快速排序,并对比较和交换进行计数,但是由于数字似乎不正确,我不确定我的计数器是否在正确的位置,或者排序有问题?

public int partition(int array[], int low, int high) 
    { 
        int pivot = array[low];

        while(low < high)
        {
            while(pivot < array[high] && low < high)
            {
                high = high - 1;
                compCounter++;
            }
            if(high != low)
            {
                array[low] = array[high];
                SwapCounter++;
                low++;
            }
            while(array[low] < pivot && low < high)
            {
                low = low +1;
                compCounter++;
            }
            if(high != low)
            {
                array[high] = array[low];
                SwapCounter++;
                high--;
            }
        }

        SwapCounter++;
        int temp = array[high];
        array[high] = pivot;
        return high;


    } 

    public void quickSort(int array[], int low, int high) 
    { 
        if (low < high) 
        { 
            int pivotPoint = partition(array, low, high); 
            quickSort(array, low, pivotPoint-1); 
            quickSort(array, pivotPoint+1, high); 
        } 
    }  

1 个答案:

答案 0 :(得分:0)

我检查了您的代码,但我认为您还要做更多的工作(在某些特殊情况下可能会产生无效的结果)。我以某种方式更改了代码的正确性,然后可以通过在正确的位置放置一些搜索和放置计数器来找到它。

    private static int CompCounter, SwapCounter;

    public static void main(String[] args) {
        int[] a = {3, 2, 1, 5, 6, 7 , 4, -1};
        quickSort(a, 0, a.length - 1);
        System.out.println(Arrays.toString(a));
    }

    public static int partition(int array[], int low, int high) {
        int pivot = array[high];

        int lowBound = low;
        for (int i = low; i < high; i++)
        {
            CompCounter++;
            if (array[i] < pivot) {
                int temp = array[lowBound];
                array[lowBound] = array[i];
                array[i] = temp;
                lowBound++;
                SwapCounter++;
            }
        }

        SwapCounter++;
        array[high] = array[lowBound];
        array[lowBound] = pivot;
        return lowBound;
    }

    public static void quickSort(int array[], int low, int high) {
        if (low < high)
        {
            int pivotPoint = partition(array, low, high);
            quickSort(array, low, pivotPoint - 1);
            quickSort(array, pivotPoint + 1, high);
        }
    }

如果需要,可以使用另一种方式进行排序分区(另一种算法):

    public static int partitionSecondWay(int array[], int low, int high) {
    int pivot = array[low];
    int i = low - 1;
    int j = high + 1;
    while (true)
    {
        do
        {
            i++;
            CompCounter++;
        } while (array[i] < pivot);
        do
        {
            j--;
            CompCounter++;
        } while (array[j] > pivot);
        if (i >= j)
        {
            CompCounter++;
            return j; // notice if you use this way, then in quicksort you
            // must use quicksort(array, low, partition) and 
            // quicksort(array, partition + 1, high)
        }

        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
        SwapCounter++;
    }
}

希望这些对您有所帮助。如果有问题,请对其进行评论。