简体   繁体   English

分类算法的复杂性

[英]Complexity of sorting algorithm

Here is a sorting algorithm, not a clever one. 这是一种排序算法,而不是聪明的算法。 In this version, it works well when elements are non-negative and occur at most once. 在此版本中,当元素为非负数且最多出现一次时,它将很好地工作。 I'm confused about its time complexity. 我对其时间的复杂性感到困惑。 Is it O(n)? 是O(n)吗? So is it better than quick sort in terms of that notation? 因此,就该符号而言,它比快速排序更好吗? Thanks. 谢谢。 Here is the code: 这是代码:

public int[] stupidSort( int[] array ){

// Variables
int max = array[0];
int index = 0;
int[] lastArray = new int[array.length];

// Find max element in input array
for( int i = 0; i < array.length; i++ ){
  if ( array[i] > max )
    max = array[i];
}

// Create a new array. In this array, element n will represent number of n's in input array
int[] newArray = new int[max + 1];

for ( int j = 0; j < array.length; j++ )
  newArray[array[j]]++;

// If element is bigger than 0, it means that number occured in input. So put it in output array
for( int k = 0; k < newArray.length; k++ ){
  if( newArray[k] > 0 )
    lastArray[index++] = k;
}
return lastArray;
}

What you wrote is the counting sort , and it has O(n) complexity indeed. 您写的是计数排序 ,它确实具有O(n)复杂度。 However, it cannot be compared to QuickSort because QuickSort is an algorithm based on comparisons. 但是,它不能与QuickSort进行比较,因为QuickSort是一种基于比较的算法。 These 2 algorithms belong to different categories (yours is a non-comparison, quicksort is a comparison algorithm). 这两种算法属于不同的类别(您是非比较类,quicksort是比较算法)。 Your algorithm (counting sort) makes the assumption that the range of numbers in the array is known and that all numbers are integer, whereas QuickSort works for every number. 您的算法(计数排序)假设数组中数字的范围是已知的,并且所有数字都是整数,而QuickSort则适用于每个数字。

You can learn more for sorting algorithms here . 您可以在此处了解更多有关排序算法的信息 In that link you can see the complexity for sorting algorithms divided in the 2 categories: comparison and non-comparison. 在该链接中,您可以看到排序算法的复杂性分为两类:比较和非比较。

EDIT 编辑

As Paul Hankin pointed out the complexity isn't always O(n). 正如保罗·汉金(Paul Hankin)所指出的那样,复杂度并不总是O(n)。 It is O(n+k) where k is the max of the input array. 它是O(n + k) ,其中k是输入数组的最大值。 Quoted below is the time complexity as explained in the wikipedia article for the counting sort : 下面引用的是时间复杂度,如Wikipedia文章所述,用于计数排序

Because the algorithm uses only simple for loops, without recursion or subroutine calls, it is straightforward to analyze. 由于该算法仅使用简单的for循环,而没有递归或子例程调用,因此分析起来很简单。 The initialization of the count array, and the second for loop which performs a prefix sum on the count array, each iterate at most k + 1 times and therefore take O(k) time. 计数数组的初始化以及第二个for循环在计数数组上执行前缀求和,每个循环最多重复k + 1次,因此需要O(k)时间。 The other two for loops, and the initialization of the output array, each take O(n) time. 其他两个for循环以及输出数组的初始化各自花费O(n)时间。 Therefore, the time for the whole algorithm is the sum of the times for these steps, O(n + k). 因此,整个算法的时间就是这些步骤的时间之和,即O(n + k)。

The given algorithm is very much similar to Count sort . 给定的算法与Count sort非常相似。 Whereas QuickSort is a comparison model based sorting algorithm. QuickSort是一种基于比较模型的排序算法。 Only in the worst case QuickSort gives O(n^2) time complexity, otherwise it is O(nlogn). 只有在最坏的情况下, QuickSort才会给出O(n ^ 2)的时间复杂度,否则为O(nlogn)。 Also the QuickSort is used with it's randomized version that is pivot is selected randomly and hence the worst-case is avoided most often this way. QuickSort也与它的随机版本一起使用,它是枢轴随机选择的,因此,这种情况最经常避免的是最坏的情况。

Edit: Correction as pointed by paulhankin in comment complexity=O(n+k): 编辑:更正,如paulhankin在评论复杂度= O(n + k)中指出的那样:

The code you have put forth is using counting based sort, that is count sort and your code's time complexity is O(n+k). 您提出的代码使用的是基于计数的排序,即计数排序,您的代码的时间复杂度为O(n + k)。 But what you must realize is that this algorithm is dependent on the range of the input and that range could be anything. 但是您必须意识到,该算法取决于输入范围,并且该范围可以是任意值。 Furthermore this algorithm is not InPlace but Stable . 此外,该算法不是InPlace而是Stable In many cases the data you want to sort is not only integer rather the data can be anything with a key that is required to be sorted with the help of the key. 在许多情况下,您要排序的数据不仅是整数,而且数据可以是任何带有键的数据,而这些键需要借助键进行排序。 If Stable algorithm is not used than in such a case sorting can be problematic. 如果不使用稳定算法,则在这种情况下排序可能会出现问题。

Just in case if someone does not know: 万一有人不知道,以防万一:

InPlace Algorithm : Is the one in which additional space required is not dependent on the given input. 就地算法 :是一种其中所需的额外空间不依赖于给定输入的算法。

Stable Algorithm : Is the one in which for example if there were two 5's in the data set before sorting, the 5 that came first before sorting comes first than the second even after the sorting. 稳定算法 :例如,如果在排序之前数据集中有两个5,则排序之前排在第一的5排在第二,即使排序之后排在第二。

EDIT: (Regarding aladinsane7's comment): Yes the formal version of countSort does handle this aspect also. 编辑:(关于aladinsane7的评论):是的, countSort的正式版也可以处理此方面。 It would be good if you have a look at CountSort . 如果您看看CountSort,那将是很好的。 And its time complexity is O(n+k) . 其时间复杂度为O(n + k) Where K accounts for the range of data and n is complexity for remaining algorithm. 其中K代表数据范围,n是剩余算法的复杂度。

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

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