5.1 键索引计数法
来源:互联网 发布:大数据时代财务管理 编辑:程序博客网 时间:2024/06/16 10:00
一、思想
适用于小整数键的简单排序方法;假设数组a[]中的每个元素都保存了一个名字和一个组号,其中组号在0到R-1之间,以组号为键进行分组排序;
二、步骤
频率统计:使用int数组计算每个键出现的频率;
将频率转换成索引:使用count[]来计算每个键在排序结果中的起始位置;
数据分类:将count[]数组转换成一张索引表后,将所有元素移动到一个辅助数组aux[]中以进行排序;
回写:将排序数组aux[]结果复制回原数组;
命题:键索引计数法排序 N 个键为 0 到 R - 1 之间的整数的元素需要访问数组 11N + 4R + 1 次。
三、代码
/** * 建索引计数法 * * @author pengcx * */ public class KeyIndex { /** * 使用键索引计数法对学生数组a,以键key进行分组排序 * * @param a * 学生数组a * @param R * 分组个数R */ public static void sort(Student[] a, int R) { int N = a.length; Student[] aux = new Student[N]; int[] count = new int[R + 1]; // 第一步,计算出现的频率 for (int i = 0; i < N; i++) { count[a[i].key() + 1]++; } //第二步,将频率转换成索引 for (int r = 0; r < R; r++) { count[r + 1] += count[r]; } //第三步, 将元素分类 for (int i = 0; i < N; i++) { aux[count[a[i].key()]++] = a[i]; } // 第四步,回写 for (int i = 0; i < N; i++) { a[i] = aux[i]; } } public static void main(String[] args) { Student stu1 = new Student("cac", 2); Student stu2 = new Student("fnc", 2); Student stu3 = new Student("edb", 4); Student stu4 = new Student("oel", 0); Student stu5 = new Student("akf", 2); Student stu6 = new Student("erk", 1); Student[] stus = { stu1, stu2, stu3, stu4, stu5, stu6 }; sort(stus, 5); for (int i = 0; i < stus.length; i++) { System.out.println(stus[i].toString()); } } } class Student { private String name; private int key; public Student(String name, int key) { super(); this.name = name; this.key = key; } public int key() { return key; } @Override public String toString() { return name + ":" + key; } }
参考:
http://lib.csdn.net/article/datastructure/30328
阅读全文
0 0
- 5.1 键索引计数法
- 键索引计数法
- 键索引计数法
- 键索引计数法
- 算法:键索引计数法
- 字符串排序先导 键索引计数法 (c++ )
- 算法系列—低位优先的字符串排序(基于键索引计数法)
- 索引节点、硬连接和连接计数(转载)
- Linux中的索引节点、硬连接和连接计数
- 科学计数法 E
- 罗马计数法
- 科学计数法
- 2502火星计数法
- JAVA 科学计数法
- JAM计数法(DP)
- PAT1024 科学计数法
- JAVA科学计数法
- PAT1024科学计数法
- Web框架spring boot/Flask几个概念
- 面试常考设计模式总结
- 树的构建
- hibernate持久化对象的状态
- 原码反码补码详解与十进制转换为二进制的各种实现
- 5.1 键索引计数法
- Ubuntu软件更新时出错问题解决
- 统计学习绪论
- leetcode 47. Permutations II 全排列问题(去掉重复元素)+递归
- 欢迎使用CSDN-markdown编辑器
- PHP错误代码解决方法
- vue-cli简易脚手架搭建
- 51Nod 1305Pairwise Sum and Divide
- 【C#】委托和事件