Sorting the patient's files with three types: Low, Med, High

Problem

  An efficient way to sort patient files in an array of just 3 types, "high-importance', 'med-importance', 'low-importance', which are in an arbitrary order(unsorted)

  The output preference should start with the highest.

  example: [high, low, low, med, high, low]


Counting sort approach


This question is the same question as "Counting Sort" problem in disguise.
The question already limit the range of the input into 3 types: Low, Med, High.
We can assign the number to that types as below:

High = 0, Med = 1, Low =2

We just turned the generic problem into the candidate problem for the counting sort.

The algorithm of the counting sort can be found in "Counting Sort".

The running time of the solution is the same as that of counting sort, $$O(N)$$, where N is the number of files given as input.

Here is the complete code in C++.



Practice statistics:

17:54 : to think up the algorithm and write up the code (had one logical flaw)
05:50: to verify the algorithm (still did not catch the flaw)
03:10: to debug the answer through execution and found the flaw

Comments

Popular posts from this blog

Planting flowers with no adjacent flower plots

Find the maximum number of bomb that can be detonated