简体   繁体   English

排序算法的时间复杂度

[英]Time complexity for a sorting algorithm

I came across the following question while I was doing some exercise: 我在做运动时遇到了以下问题:

A sorting algorithm starts from start of the list, scan until two succeeding items that are in the wrong order are found. 排序算法从列表的开始开始扫描,直到找到错误顺序的两个后续项目。 Swap those items and go back to the beginning. 交换这些项目并回到开头。 The algorithm ends when the end of the list is reached. 当到达列表的末尾时,算法结束。

What is the worst-case running time for a list of size n? 大小为n的列表的最坏情况运行时间是多少?

I feel that it is similar with bubble sort, but probably worse that that because it doesn't finish the whole pass of scanning the list. 我觉得它与冒泡排序类似,但可能更糟糕,因为它没有完成扫描列表的整个过程。 But I can't figure out how to calculate its time complexity. 但我无法弄清楚如何计算其时间复杂度。 I am not sure if the code I came up below for this algorithm is correct. 我不确定这个算法下面的代码是否正确。 Many thanks for your help! 非常感谢您的帮助!

for (int i=0, i<n , i++){//n is the size of the array
     if (array[i]>array[i+1]){
          swap (array[i], array[i+1]);
          i=0;
     }
}

The number of comparisons for the worst case (I wont prove the worst case is n, n-1, ... 1 but I assume it is) is a tetrahedral number . 最坏情况下的比较次数(我不会证明最坏的情况是n,n-1,... 1但我认为是)是四面体数

Why? 为什么?

imagine a sequence 想象一个序列

n, n-1, .... 1

so for 1, will be swap when everything before is already on place. 因此,对于1,将在以前的所有内容都已到位时进行交换。 So the number of comparisons when is in the last place will be n-1 before is swapped to the second last place. 因此,在最后一个位置的比较次数将是n-1,然后交换到倒数第二位。 From the second last place it will have to do n - 2 comparisons. 从倒数第二位开始,它将进行n - 2次比较。 Following this logic, and extending to the previous numbers we have for different n: 遵循这个逻辑,并扩展到以前的数字,我们有不同的n:

In other words, for position ni will need to move one number down (1), from position n - 1 I will need to move 2, (1 & 2), from n-2 (1, 2 & 3). 换句话说,对于位置ni,需要向下移动一个数字(1),从位置n-1移动,我将需要从n-2(1,2和3)移动2,(1和2)。 Assuming 1,2,3... n is the valid order and they start as n,...3, 2,1. 假设1,2,3 ... n是有效订单,它们从n开始,...... 3,2,1。

n = 3 -> 2 + 1 * 2 = 4 
n = 4 -> 3 + 2 * 2 + 3= 10 
n = 5 -> 4 + 3 * 2 + 2 * 3 + 4 = 20 
n = 6 -> 5 + 4 * 2 + 3 * 3 * 2 * 4 + 5 = 35
n = 7 -> 6 + 5 * 2 + 4 * 3 + 3 * 4 + 2 * 5 + 6 = 56 
n = 8 -> 7 + 6 * 2 + 5 * 3 + 4 * 4 + 3 * 5 + 2 * 6 + 7= 84

And this sequence is the tetrahedral numbers. 这个序列是四面体数。

第n个四面体数的公式由n的第3个上升因子除以3的阶乘表示

And as a binomial coeficient: 并且作为二项式系数:

二项式系数

So, it seems that it is O(n^3) 所以,它似乎是O(n ^ 3)

This is a bad form of insertion sort. 这是一种不好的插入排序形式。 In insertion sort, the complexity of insertion is linear in the length of the array, here it is quadratic. 在插入排序中,插入的复杂性在数组的长度上是线性的,这里它是二次的。 Hence the worst case time complexity of this algorithm is IMO O(n^3). 因此,该算法的最坏情况时间复杂度是IMO O(n ^ 3)。

If the initial array was 4, 3, 2, 1, then the steps of the algorithm would be: 如果初始数组是4,3,2,1,那么算法的步骤将是:

4 3 2 1
3 4 2 1
3 2 4 1
2 3 4 1
2 3 1 4
2 1 3 4
1 2 3 4

Note that each step in this sequence has a linear time complexity because to get the position of the earliest 'inversion' you scan the entire array from the beginning. 请注意,此序列中的每个步骤都具有线性时间复杂度,因为要获取最早“反转”的位置,您将从头开始扫描整个数组。

An array may contain O(n^2) inversions. 数组可能包含O(n ^ 2)个反转。 Every run corrects only one inversion and executes O(n) steps (comparisons). 每次运行只校正一次反演并执行O(n)步骤(比较)。 So overall time complexity is O(n^3). 因此总体时间复杂度为O(n ^ 3)。
The worst case - backward-sorted array with N*(N-1)/2 inversions, exact runtime analysis has already been done by Raul Guiu. 最坏的情况 - 具有N *(N-1)/ 2反转的反向排序数组,Raul Guiu已经完成了精确的运行时分析。

您所描述的是一种冒泡类型: http//en.wikipedia.org/wiki/Bubble_sort冒泡排序具有最坏情况和平均复杂度О(n2)

First, this algorithm looks like bubble-sort but slower than that. 首先,这个算法看起来像冒泡,但比这慢。

Second, this algorithm is doing some non-sense operations, ie Swap those items and go back to the beginning. 其次,这个算法正在做一些无意义的操作,即Swap those items and go back to the beginning. For example your initial array is as follows, 例如,您的初始数组如下,

10 11 12 13 14 15 16 17 18 19 9

You will iterate till the end, when you see 9, swap it with 18 then go to the beginning of the array and start comparing if(arr[0] < arr[1]) but you already know that it is smaller. 你将迭代到最后,当你看到9,用18交换然后转到数组的开头并开始比较if(arr[0] < arr[1])但你已经知道它更小了。

The worst case running time of this is O(n^3). 最糟糕的情况是运行时间为O(n ^ 3)。 The pseudo code will be something like this: 伪代码将是这样的:

for i = 0:n-1
    if(arr[i] > arr[i+1]){
        swap(arr[i], arr[i+1])
        i = 0;
    }

Now Compare both your Code and Bubble sort because it seems to be.. 现在比较你的代码和冒泡排序,因为它似乎是..

Your code It will give Max O(n^3) 你的代码它会给出Max O(n^3)

Bubble sort 冒泡排序

void bubbleSort(int arr[])
{
    int n = arr.length;
    for (int i = 0; i < n-1; i++)
        for (int j = 0; j < n-i-1; j++)
            if (arr[j] > arr[j+1])
            {
                // swap temp and arr[i]
                int temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
}

It will give in worst case O(n^2) and even if array is sorted. 在最坏的情况下,它将给出O(n ^ 2),即使数组已经排序。

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

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