I am trying to sort and find the median of a string of integers that only contains 3 to 4 different integers.
The amount of numbers I am dealing with is of magnitudes of about 20 to 25 million and I am supposed to sort the vector and find the median each time a new integer is added into the vector and add the median into a separate "Total" variable which sums up all the medians each time a median is generated.
1 Median: 1 Total: 1
1 , 2 Median: (1+2)/2 = 1 Total: 1 + 1 = 2
1 , 2 , 3 Median: 2 Total: 2 + 2 = 4
1 , 1 , 2 , 3 Median: (1+2)/2 = 1 Total: 4 + 1 = 5
1 , 1 , 1 , 2 , 3 Median: 1 Total: 5 + 1 = 6
I am trying to find a way to optimize my code further because it is just not efficient enough. (Got to process under 2s or so) Does anyone have any idea how to further speed up my code logic?
I am currently using 2 heaps, or priority queues in C++. One functioning as a max-heap and the other functioning as a min-heap.
Gotten the idea from Data structure to find median
You can use 2 heaps, that we will call Left and Right.
Left is a Max-Heap.
Right is a Min-Heap.
Insertion is done like this:
If the new element x is smaller than the root of Left then we insert x to
Left.
Else we insert x to Right.
If after insertion Left has count of elements that is greater than 1 from
the count of elements of Right, then we call Extract-Max on Left and insert
it to Right.
Else if after insertion Right has count of elements that is greater than the
count of elements of Left, then we call Extract-Min on Right and insert it
to Left.
The median is always the root of Left.
So insertion is done in O(lg n) time and getting the median is done in O(1)
time.
but it is just not fast enough...