繁体   English   中英

快速计算比较和交换

[英]Counting comparisons and swaps in a quick sort

我正在对从文件中读取的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); 
        } 
    }  

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

    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++;
    }
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM