An array of length N can contain values 1,2,3 … N^2. Is it possible to sort in O(n) time?

前端 未结 3 1335
别跟我提以往
别跟我提以往 2020-12-09 21:55

Given an array of length N. It can contain values from ranging from 1 to N^2 (N squared) both inclusive, values are integral. Is it possible to sort this array in O(N) time?

3条回答
  •  遥遥无期
    2020-12-09 22:50

    Yes, you can, using radix sort with N buckets and two passes. Basically, you treat the numbers as having 2 digits in base N.

提交回复
热议问题