What is the fastest way to sort 1 million integers when integers are from the range [1,100]?

前端 未结 8 1637
慢半拍i
慢半拍i 2020-12-23 22:13

Notes: I\'ve thought about Radix sort, bucket sort, counting sort.

Is there anyway to achieve big O(n)?

8条回答
  •  情深已故
    2020-12-23 22:24

    Here is a counting sort in scala:

    val res = Array.fill (100)(0)
    val r = util.Random 
    // generate data to sort
    val nums = for (i <- 1 to 1000*1000) yield r.nextInt (100)
    for (i <- nums) res(i) += 1
    println (res.mkString (" ")) 
    

提交回复
热议问题