Reputation: 57
Is there any sorting algorithm with an average time complexity log(n)??
example [8,2,7,5,0,1] sort given array with time complexity log(n)
Upvotes: 3
Views: 23054
Reputation: 16129
Using a PLA it might be possible to implement counting sort for a few elements with a low range of values.
Only massive parallel computation would be able to do this, general purpose CPU's would not do here unless they implement it in silicon as part of their SIMD.
Upvotes: 1
Reputation: 3951
As @dominicm00 also mentioned the answer is no. In general when you see an algorithm with time complexity of Log N with base 2 that means that, you are dividing the input list into 2 sets, and getting rid of one of them repeatedly. In sorting algorithm we need to put all the elements in their appropriate place, if we get rid of half of the list in each iteration, that does not correlate with sorting functionality.
The most efficient sorting algorithms have the time complexity of O(n), but with some limitations. Three most famous algorithm with complexity of O(n) are :
But in general due to limitation of each of these algorithms most implementation relies on O(n* log n) algorithms, such as merge sort, quick sort, and heap sort.
Also there are some sorting algorithms with time complexity of O(n^2) which are recommended for list with smaller sizes such as insertion sort, selection sort, and bubble sort.
Upvotes: 2
Reputation: 1610
No; this is, in fact, impossible for an arbitrary list! We can prove this fairly simply: the absolute minimum thing we must do for a sort is look at each element in the list at least once. After all, an element may belong anywhere in the sorted list; if we don't even look at an element, it's impossible for us to sort the array. This means that any sorting algorithm has a lower bound of n
, and since n > log(n)
, a log(n)
sort is impossible.
Although n
is the lower bound, most sorts (like merge sort, quick sort) are n*log(n)
time. In fact, while we can sort purely numerical lists in n
time in some cases with radix sort, we actually have no way to, say, sort arbitrary objects like strings in less than n*log(n)
.
That said, there may be times when the list is not arbitrary; ex. we have a list that is entirely sorted except for one element, and we need to put that element in the list. In that case, methods like binary search tree can let you insert in log(n)
, but this is only possible because we are operating on a single element. Building up a tree (ie. performing n
inserts) is n*log(n)
time.
Upvotes: 13