# 算法练习(5)-计数排序法及优化

    static void sort0() {
int[] arr = new int[]{5, 4, 4, 1, 2, 3};
int[] indexCountArr = new int[6];

//下标计数排序
for (int i = 0; i < arr.length; i++) {
indexCountArr[arr[i]] += 1;
}
//排完后，indexCountArr的值:[0, 1, 1, 1, 2, 1]
System.out.println("indexCountArr=>" + Arrays.toString(indexCountArr));

//排序结果输出
for (int i = 0; i < indexCountArr.length; i++) {
for (int j = 0; j < indexCountArr[i]; j++) {
System.out.print(i + "\t");
}
}
System.out.println("\n");
}


indexCountArr=>[0, 1, 1, 1, 2, 1]
1 2 3 4 4 5

    static class EmpScore {
public String empNo;
public int score;

public EmpScore(String empNo, int score) {
this.empNo = empNo;
this.score = score;
}

@Override
public String toString() {
return empNo + ":" + score;
}
}

public static void main(String[] args) {
EmpScore[] arr = new EmpScore[]{
new EmpScore("S01", 5),
new EmpScore("S02", 4),
new EmpScore("S03", 4),
new EmpScore("S04", 1),
new EmpScore("S05", 2),
new EmpScore("S06", 3)
};

//todo 待排序
}


    static void sort1(EmpScore[] arr) {
//排序过程(下标计数排序)
Map<Integer, List<EmpScore>> scoreMap = new HashMap<>(arr.length);
int[] indexCountArr = new int[6];
for (int i = 0; i < arr.length; i++) {
indexCountArr[arr[i].score] += 1;
List<EmpScore> list;
if (scoreMap.containsKey(arr[i].score)) {
list = scoreMap.get(arr[i].score);
} else {
list = new ArrayList<>();
}
scoreMap.put(arr[i].score, list);
}
//排完后，indexCountArr的值:[0, 1, 1, 1, 2, 1]
System.out.println("indexCountArr=>" + Arrays.toString(indexCountArr));

//输出结果
List<EmpScore> sortedList = new ArrayList<>(arr.length);
for (int i = 1; i < indexCountArr.length; i++) {
}
System.out.println(sortedList.toString() + "\n");
}


indexCountArr=>[0, 1, 1, 1, 2, 1]
[S04:1, S05:2, S06:3, S02:4, S03:4, S01:5]

static void sort2(EmpScore[] arr) {
int[] indexCountArr = new int[6];
for (int i = 0; i < arr.length; i++) {
indexCountArr[arr[i].score] += 1;
}
//关键点1
for (int i = 1; i < arr.length; i++) {
indexCountArr[i] += indexCountArr[i - 1];
}

//排完后，indexCountArr的值:[0, 1, 2, 3, 5, 6]
System.out.println("indexCountArr=>" + Arrays.toString(indexCountArr));

EmpScore[] sorted = new EmpScore[arr.length];
//关键点2
for (int i = arr.length - 1; i >= 0; i--) {
sorted[indexCountArr[arr[i].score] - 1] = arr[i];
indexCountArr[arr[i].score] -= 1;
}

System.out.println(Arrays.toString(sorted));
}


posted @ 2021-03-25 20:04  菩提树下的杨过  阅读(25)  评论(0编辑  收藏