-2

What is considered an optimal data structure for pushing elements in sorted order. Here i am looking for some idea or our own customize data structure using that i can achieve insertion of each element in O(1) time and it should be sorted. I do not want to use binary search or tree or linkedlist to make it done.

Values range would be till 50,000 and it can be insert in any random order. After each insert my test case will check data structure is sorted or not. So i have to sort after each insert.

Please share your suggestions and views on this. How can i achieve this inserting sorting order with O(1).

Thanks

sam_k
  • 5,983
  • 14
  • 76
  • 110

4 Answers4

3

If you could do insertion in O(1) time, then you could solve for sorting a list of n elements in O(n) time. But that problem has been proven to be O(n log n), so the original assumption, that insertion can be done in O(1), is wrong.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
1

If you are dealing with integers, the closest you can get to your requirements is by using a Van Emde Boas tree.

You can't get pure O(1). Either you have to do a binary search, or move elements around, or find the right place in a tree.

Hash tables will not keep your elements sorted in any way, at least with VEB trees you have the FindNext methods.

IVlad
  • 43,099
  • 13
  • 111
  • 179
  • 1
    I the elements are integers from 0 to 50,000, why not just have an array (or bitset if only one of each element) representing each element? Wouldn't that be O(1) insertion, and sorting would never be needed? – גלעד ברקן Feb 27 '15 at 17:09
  • @גלעדברקן Can you please give me more details what you said above. as per you i should create array of 50000 elements and then fill it one by one as per its coming order to desire index? – sam_k Feb 27 '15 at 17:17
  • @גלעדברקן - it depends on the necessary supported operations. Doing that would not allow operations that would be efficient on an actual sorted array or a search tree. – IVlad Feb 27 '15 at 17:25
  • @IVlad I see - could you give an example of an operation that would be more efficient on a sorted array or search-tree with 50,000 elements? – גלעד ברקן Feb 27 '15 at 17:27
  • @גלעדברקן - finding the first element larger than or equal to a given value, for example. With your method, the complexity of this depends on the actual input: you might have to iterate over a few elements or over a lot of elements. – IVlad Feb 27 '15 at 17:31
  • @גלעדברקן There are not that much operations. After every insert it will check its sorted order and insertion may contains duplicate values as well. Actually i want to use for priority sorted queue. – sam_k Feb 27 '15 at 18:03
0

The only "sorting" you can do in O(1) is to use your sort keys as direct indexes into an array, which becomes impractical or plain impossible as soon as your keys can vary in too broad a range.

kuroi neko
  • 8,479
  • 1
  • 19
  • 43
-1

Maybe "Bucket sort" will fulfill your requirement of O(1) insertion in sorted list, limited value range & insert with random order.

For example, you can split 1~50,000 number to 10,000 buckets, then when you get a number N, you can push it in bucket n/5. after that, you just need to rerank the number in bucket n/5.

this is "nearly" O(1).

  • 3
    That sounds like `O(n / 5)` to me. – IVlad Feb 27 '15 at 16:26
  • That is not o(n / 5). First, you choose a bucket, which index is n/5, and insert you data in this bucket. this is o(1). Then, you resort the elements in budcket n/5. this is 5log(5). So this is not o(n/5). – GlacierKba6 Mar 02 '15 at 04:05