How to sort a specific kind of input

58

You could do this using a heap of N elements where N is the maximum overlap (N=300 in your case).

Start with an empty heap.

For each element in the sequence:

  1. Add element to the heap

  2. If the heap has more than N elements, output (and remove from the heap) the smallest element

Finally output the remaining elements in the heap.

This will have complexity O(nlogN)

Share:
58

Related videos on Youtube

BSRK Aditya
Author by

BSRK Aditya

Updated on November 23, 2022

Comments

  • BSRK Aditya
    BSRK Aditya over 1 year

    I have a sequence of elements, say a1,a2,...,an. (n ~ 4*10^7)

    The input will be mostly sorted. More specifically any element less than a_i will be either to the left, or very close to the right(~ 300 elements).

    How can I sort this sequence efficiently?

    • mmgp
      mmgp over 11 years
      Check natural mergesort.
  • Yrogirg
    Yrogirg about 12 years
    how do I configure it to disable showing running applications?
  • aroque
    aroque about 12 years
    I don't know. Try asking in the extension page.
  • Bogdan
    Bogdan over 11 years
    I would say to use a queue/list of size N and always keep it sorted by inserting each new element at its correct location. When the size reaches N start removing the smallest elements.