0

If an array contains duplicated elements, what data structure is better for sorting?

Could B tree work?

5
  • 3
    Um, what? Every sorting algorithm correctly handles duplicates, and a lot of them even maintain the relative order of items which compare as equal (this is called a stable sort). An array is also fine, because it can contain duplicate elements. Please clarify. Commented Oct 7, 2012 at 22:38
  • B-trees are always sorted (as are a number of other ADTs). If it is better to keep an always-sorted structure or to sort-on-demand really depends upon other requirements/usage .. but the question seems to be asking something orthogonal to this. While the comparison sorting O(n log n) time limits apply, there the different approaches amortize the complexity differently, etc. Commented Oct 7, 2012 at 22:46
  • I just want to find out a better sorting algorithm that has smaller Big O. And everytime I insert a new element, I want to keep the order. Commented Oct 7, 2012 at 22:53
  • @FreyaRen If the sorted order must always be maintained, then use the applicable tree, heap, trie, etc (Wikipedia is a good starting place to read up on the fundamental differences/rules). If the data just needs to be sorted at some point, consider a simple array/list and sort-on-demand. The list can consume less memory at any one point, and can be sorted in O(n log n), but the sort is not amortized as with trees and other such structures. Also there are "eventual sort" algorithms, etc. It depends on the use-case, the real requirements, and real limitations. Commented Oct 7, 2012 at 22:54
  • So, my point is: don't invent a problem which doesn't exist; just use the structure is easiest to work with unless there are other concerns/limitations :-) SO is best when asking "practical, answerable" questions - which generally starts with some code. Commented Oct 7, 2012 at 22:59

1 Answer 1

1

For a fixed and small range of element values you can use counting sort algorithm, as described here. Its complexity is O(n + k), where n is the size of your array, and k is, basically, the amount of different possible elements.

The point is to calculate the number of same elements, and then insert them in the right order.

Sign up to request clarification or add additional context in comments.

7 Comments

This depends if the data in the array to be sorted has a range of values.
Of course it does. It has the O(TotalNumberOfElements) complexity.
That's Big Oh notation for time complexity, but you will need to create an array of X elements to use as buckets. If you're going to sort a 10 element in array with data varying between 1 an 100000, this will be a bad approach.
I just thought the OP has specific data to sort, so standard O(n log n) is bad for him.
@Luiggi Mendoza. Yes, if the elements are not in a range of values, counting sort will not work. Is comparison sort(O(n*logn)) the only way I could use?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.