求排序一堆整数,数据都是有限范围的和有限个数的,对他们进行排序,要求O(n)的时间复杂度....
求排序一堆整數(shù),數(shù)據(jù)都是有限范圍的和有限個數(shù)的,任意數(shù)據(jù)都小于100000,個數(shù)也肯定小于100000,對他們進(jìn)行排序,要求O(n)的時間復(fù)雜度.
思路:
(1)比如有一組數(shù)據(jù)arr={1,200,44,232,12,33,200},然后定義一個數(shù)組int[] count = new int[100000],初始化每一個數(shù)據(jù)值為0;
(2)然后掃描這個數(shù)組,然后count[arr[1]]++,即:
遇到1,則count[1]++;
遇到200,則count[200]++;
遇到44,則count[44]++;
遇到232,則count[232]++;
遇到12,則count[12]++;
遇到33,則count[33]++;
遇到200,則count[200]++;
(3)然后遍歷這個count數(shù)組,如果count[i]>0的時候,表示數(shù)組中有多個相同的數(shù),則輸出多次i.
?
代碼實(shí)現(xiàn)(Java):
public class CoolSort {public static void main(String[] args) {int[] count = new int[100000];int[] arr = { 1, 200, 44, 232, 9999, 9999, 12, 33, 200 };for (int i = 0; i < arr.length; i++) {count[arr[i]]++;}for (int i = 0; i < count.length; i++) {if (count[i] > 0) {for (int j = 0; j < count[i]; j++) {System.out.println(i);}}}} }
轉(zhuǎn)載于:https://www.cnblogs.com/ChrisWang/articles/1597519.html
總結(jié)
以上是生活随笔為你收集整理的求排序一堆整数,数据都是有限范围的和有限个数的,对他们进行排序,要求O(n)的时间复杂度....的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 博客园在升级的路上,不妨更自信些,同时说
- 下一篇: [数论] 求逆元