deepak_k_0001
deepak_k_0001

Reputation: 57

Sorting Algorithms with time complexity Log(n)

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

Answers (3)

Surt
Surt

Reputation: 16129

Using a PLA it might be possible to implement counting sort for a few elements with a low range of values.

  • count each amount in parallel and sum using lg2(N) steps
  • find the offset of each element in lg2(N) steps
  • write the array in O(1)

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

Ashkanxy
Ashkanxy

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 :

  1. Counting sort with time complexity of O(n+k), while k is the maximum number in given list. Assuming n>>k, you can consider its time complexity as O(n)
  2. Radix sort with time complexity of O(d*(n+k)), where k is maximum number of input list and d is maximum number of digits you may have in input list. Similar to counting sort assuming n>>k && n>>d => time complexity will be O(n)
  3. Bucket sort with time complexity of O(n)

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

dominicm00
dominicm00

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

Related Questions