1104. Unit Interval Sort

Easy1.5 points estimatedArrayBucket SortSorting3 hints

A lab measures the fraction of light that passes through a filter, once per sample. Every reading in values is a real number with 0 <= value < 1, and the readings are spread more or less evenly over that range. The lab needs them sorted from the smallest to the largest before computing percentiles.

Return a new array containing the same readings in non-decreasing order. Because the readings are spread evenly over the unit interval, you can do better than a general comparison sort: put each reading into a bucket chosen from its value, order the buckets, and fix the order inside each bucket. Any correct approach is accepted; answers are compared with a small tolerance.

Example 1

Input:
values = [0.42,0.07,0.99,0.5,0.07]
Output:
[0.07,0.07,0.42,0.5,0.99]
Explanation:

The readings in increasing order are 0.07, 0.07, 0.42, 0.5 and 0.99; duplicates stay.

Example 2

Input:
values = [0.9,0.1]
Output:
[0.1,0.9]
Explanation:

With two readings the smaller one, 0.1, goes first.

Constraints

1 ≤ values.length ≤ 105

0 ≤ values[i] < 1

How this problem is judged

Answers
Numbers are accepted within a tolerance of 1.0E-6: |answer - expected| <= 1.0E-6 x max(1, |expected|).
Tolerance
0.000001

Expected complexity

Time
O(n) expected
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…