Another Algorithm Job-Interview

后端 未结 5 1516
孤城傲影
孤城傲影 2021-02-04 15:02

So here is the question:

Suppose you have 100 thousand integers which ranges from 1 to 1 million. Please sort out the integers. The time complexity should

5条回答
  •  日久生厌
    2021-02-04 15:53

    Sounds like a straightforward counting sort.

    1. Reserve memory for an array a of size 1 million
    2. Initialize all array values to 0
    3. Loop through the integers. For each integer i increment a[i] by one.
    4. Output sorted sequence by walking through the array and printing each number i for a[i] times.

    Space is constant. Runtime is O(n).

提交回复
热议问题