Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.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_Sorting_Quicksort - Fatal编程技术网

Java 对于这种特定的快速排序实现,最佳、平均和最差的情况是什么?

Java 对于这种特定的快速排序实现,最佳、平均和最差的情况是什么?,java,algorithm,sorting,quicksort,Java,Algorithm,Sorting,Quicksort,我知道最佳和平均时间是O(n log(n)),最差时间是O(n^2)。有人能告诉我这些情况在这个特定的实现中实际发生的时间吗?其他实现之间的差异如何 private void quickSort(int low, int high) { int i = low, j = high; // Get the pivot element from the middle of the list int pivot = array[low + (hi

我知道最佳和平均时间是O(n log(n)),最差时间是O(n^2)。有人能告诉我这些情况在这个特定的实现中实际发生的时间吗?其他实现之间的差异如何

    private void quickSort(int low, int high) {
        int i = low, j = high;
        // Get the pivot element from the middle of the list
        int pivot = array[low + (high - low) / 2];

        // Divide into two lists
        while (i <= j) {
            // If the current value from the left list is smaller then the pivot
            // element then get the next element from the left list
            while (array[i] < pivot) {
                i++;
            }
            // If the current value from the right list is larger then the pivot
            // element then get the next element from the right list
            while (array[j] > pivot) {
                j--;
            }

            // If we have found a values in the left list which is larger then
            // the pivot element and if we have found a value in the right list
            // which is smaller then the pivot element then we exchange the
            // values.
            // As we are done we can increase i and j
            if (i <= j) {
                swap(array, i, j);
                i++;
                j--;
            }
        }
        // Recursion
        if (low < j)
            quickSort(low, j);
        if (i < high)
            quickSort(i, high);
    }
private void快速排序(int-low,int-high){
int i=低,j=高;
//从列表中间获取pivot元素
int pivot=array[low+(high-low)/2];
//分为两个列表
而(我){
j--;
}
//如果我们在左边的列表中找到一个大于
//pivot元素,以及我们是否在右侧列表中找到了值
//它比枢轴元素小,然后我们交换
//价值观。
//完成后,我们可以增加i和j

如果(i最坏的情况发生在您选择一个轴时,即在“划分为两个列表”部分之后,在数组的一个极端结束(最左边或最右边)。原因是:两个递归调用中的一个几乎不做任何工作,而另一个将再次做几乎所有的工作

例如:你有一个从1到100的排列,你在中间选择一个支点。假设支点是1。你运行“分为两个列表”部分,现在你必须对左边的0个元素和右边的99个元素排序。



最好的情况是当数据轴拆分为两个列表的一半时(在置换示例中,当您选择一个大约为50的数据轴时)。

仔细查看数据轴选择。如果所有数字都具有相同的值,会发生什么情况?