不是元素不能比较 而是不借助compareTo进行实现
计数排序
统计每个整数在序列中出现的次数进而推导出每个整数在有序列中的索引
public class CountSort {public void sortColors(int[] nums) {//处理元素取值范围[0,R) --> R=最大值+1int R = 3;//数组中数的范围 0,1,2int[] cnt = new int[R];//cnt数组存储的就是索引位置上有多少个值 索引就是值for (int num : nums) {cnt[num]++;}//记录i值的范围 [index[i],index[i+1])]范围都是iint[] index = new int[R + 1];//index记录的是每个值的区间index[0] = 0;for (int i = 0; i < R; i++) {//前一个值+当前位置值的个数就是这个值所在的区间index[i + 1] = index[i] + cnt[i];}for (int i = 0; i + 1 < index.length; i++) {//遍历值为i 所对应的区间 index[i],index[i+1])] i+1不能越界 所以i + 1 < index.lengthfor (int j = index[i]; j < index[i + 1]; j++) {nums[j] = i;}}}public static void main(String[] args) {int [] nums = {1,2,3,1,2,3,1,2,1,2,1,3,3};new CountSort().sortColors(nums);for (int i : nums){System.out.println(i);}}}
对对象进行排序
public class CountSort {public void sortObj(StudentSore[] studentSores) {//定义分数的范围 R=最大值+1int R = 101; //[0,101);int[] count = new int[R];//保存每个分数下的个数//记录每个分数(索引)的个数for (StudentSore s : studentSores) {count[s.getScore()]++;}//[index,index+1)int[] index = new int[R + 1]; //记录每个分数的区间index[0] = 0;//把每个数的区间保存下来for (int i = 0; i < R; i++) {index[i + 1] = index[i] + count[i];//这算的是当前值结束的位置(不包含)和下一个值开始的位置}//通过这个临时对象保存排好的序StudentSore[] temp = new StudentSore[studentSores.length];/*** 原数组 2 1 0 1 2 0* A B C D E F* -----------------------------------* 0 1 2* count 2 2 2* index 0 2 4 6* 0->[0,2) 1->[2,4) 2->[4,6)*/for (StudentSore s : studentSores) {temp[index[s.getScore()]] = s;index[s.getScore()]++;}//将排好序的对象放入到原始数组中for (int i = 0; i < temp.length; i++) {studentSores [i] = temp[i];}}public static void main(String[] args) {StudentSore[] studentSores = new StudentSore[26];int k = 0;for (char i = 'a'; i <= 'z'; i++) {studentSores[k] = new StudentSore(i+"", new Random().nextInt(101));k++;}new CountSort().sortObj(studentSores);for (StudentSore s : studentSores) {System.out.println(s);}}}class StudentSore {String name;int score;public StudentSore(String name, int score) {this.name = name;this.score = score;}public int getScore() {return score;}@Overridepublic String toString() {return "StudentSore{" +"name='" + name + '\'' +", score=" + score +'}';}}
基数排序
依次对个位数,十位数,百位数,千位数,万位数。。。。进行排序
只能对整数进行排序
public class BaseSort {
public void baseSort(int[] nums) {
//找到最大值
int max = nums[0];
for (int i = 0; i < nums.length; i++) {
if (nums[i] > max) max = nums[i];
}
//循环对每一位进行判断
int base = 1;//记录当前位数
int[] temp = new int[nums.length];//保存临时排序好的数组
//当base位数<=max的位数时进行循环
while (max / base > 0) {
//将当前位数下0-9出现的次数保存在里面
//后面将次数替换成在数组中最大长度(因为是索引存入数组中是-1)
int[] bucket = new int[10];//不管哪一位上都是0-9 10个数字
for (int i = 0; i < nums.length; i++) {
//统计当前位数下0-9出现的次数
bucket[nums[i] / base % 10]++;
}
//将次数替换成在数组中最大长度 --> 当前位上的值加上前面的长度=当前值在数组中的最大长度(使用时-1)
for (int i = 1; i < bucket.length; i++) {
//计算当前值在数组中的最大长度
bucket[i] = bucket[i] + bucket[i - 1];
}
//从后往前的方式遍历数组
//bucket[nums[i] / base % 10]当做值在数组的长度 - 1数组中的索引位置
for (int i = nums.length - 1; i >= 0; i--) {
temp[bucket[nums[i] / base % 10] - 1] = nums[i];
bucket[nums[i] / base % 10]--;//有多个为数值相同 下个长度后移
}
//重新赋值到nums中去
for (int i = 0; i < nums.length; i++) {
nums[i] = temp[i];
}
//每次扩大10倍--->当位数比max大的时候max/base=0(1234/10000=0)
base = base * 10;
}
}
public static void main(String[] args) {
int[] nums = {111, 2, 23, 1, 52, 3, 1, 2, 1, 2, 1, 3, 3};
new BaseSort().baseSort(nums);
for (int n : nums) {
System.out.println(n);
}
}
桶排序
按一定规则将数据放入不同的桶中,对不同的桶进行单独排序在进行输出
public class BucketSort {
public void buckerSort(int[] nums) {
//找到最大值
int max = nums[0];
for (int i : nums) {
if (i > max) {
max = i;
}
}
//计算最大值的长度 当做桶的长度
int length = new String(Integer.toString(max)).length();
List[] buckets = new ArrayList[length];
//初始化桶
for (int i = 0; i < buckets.length; i++)//初始化
{
buckets[i] = new ArrayList<Integer>();
}
//将数据按照某种规则放入桶中
for (int i = 0; i < nums.length; i++)//将待排序序列放入对应桶中
{
//根据数组的位数放入桶中1位对应的是0号索引
int index = new String(Integer.toString(nums[i])).length();
buckets[index - 1].add(nums[i]);
}
for (int i = 0; i < buckets.length; i++) {
buckets[i].sort(null);//使用其他排序方式对桶里的元素进行排序
for (int j = 0; j < buckets[i].size(); j++)//顺便打印输出
{
System.out.print(buckets[i].get(j) + " ");
}
}
}
public static void main(String[] args) {
new BucketSort().buckerSort(new int[]{11,22,44,1,56,11,111,1,6,7,9});
}
}
