Skip to content

Commit f120847

Browse files
author
robert
committed
add count sort
1 parent 7dd3680 commit f120847

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# simplealgorithm##com.algorithm TestAsynTreadXunlei 开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推。<br>PrimeNumber 素数打表,快速判断一个数是不是素数<br>UnionFind 并查集算法<br>##com.algorithm.beanPerson 实现Comparable接口的一个类<br>##com.algorithm.cacheIcache 缓存接口<br>BaseCache 缓存基类<br>FIFOCache FIFO缓存算法实现<br>LRUCache LRU缓存算法实现<br>LFUCache LFU缓存算法实现<br>CacheTest 缓存测试类<br>##com.algorithm.digestDigestUtil 快速计算文件MD5信息,通用获取MD 5文件信息的封装<br>DigestMain MD5测试类<br>##com.algorithm.list MyArrayList 数组线性表的实现<br>MyLinkedList 链表的实现<br>Permutation 简单的组合算法<br>##com.algorithm.treeBinarySearchTree 二分查找的递归实现和非递归实现方式<br>##com.algorithm.sortJavaSort Java Collections.sort的两种排序方式<br>Bubblesort 冒泡排序<br>SelectionSort 选择排序<br>InsertionSort 插入排序<br>MergeSort 归并排序<br>QuickSort 快速排序<br>
1+
# simplealgorithm##com.algorithm TestAsynTreadXunlei 开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推。<br>PrimeNumber 素数打表,快速判断一个数是不是素数<br>UnionFind 并查集算法<br>##com.algorithm.beanPerson 实现Comparable接口的一个类<br>##com.algorithm.cacheIcache 缓存接口<br>BaseCache 缓存基类<br>FIFOCache FIFO缓存算法实现<br>LRUCache LRU缓存算法实现<br>LFUCache LFU缓存算法实现<br>CacheTest 缓存测试类<br>##com.algorithm.digestDigestUtil 快速计算文件MD5信息,通用获取MD 5文件信息的封装<br>DigestMain MD5测试类<br>##com.algorithm.list MyArrayList 数组线性表的实现<br>MyLinkedList 链表的实现<br>Permutation 简单的组合算法<br>##com.algorithm.treeBinarySearchTree 二分查找的递归实现和非递归实现方式<br>##com.algorithm.sortJavaSort Java Collections.sort的两种排序方式<br>Bubblesort 冒泡排序<br>SelectionSort 选择排序<br>InsertionSort 插入排序<br>MergeSort 归并排序<br>QuickSort 快速排序<br>CountSort 计数排序<br>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.algorithm.sort;
2+
3+
/**
4+
* 计数排序
5+
*
6+
* @author chao
7+
*
8+
*/
9+
public class CountSort {
10+
11+
public static void main(String[] args) {
12+
int[] num = { 1, 1 };
13+
sort(num);
14+
for (int i = 0; i < num.length; i++)
15+
System.out.print(num[i] + " ");
16+
}
17+
18+
/**
19+
* 计数排序
20+
*
21+
* @param num
22+
*/
23+
public static void sort(int[] num) {
24+
int len = num.length;
25+
int[] orign = new int[len];
26+
int max = 0;// 我们只对正整数排序
27+
for (int i = 0; i < len; i++) {
28+
orign[i] = num[i];
29+
if (num[i] > max) {
30+
max = num[i];
31+
}
32+
}
33+
max = max + 1;
34+
int[] count = new int[max];
35+
for (int i = 0; i < max; i++) {
36+
count[i] = 0;
37+
}
38+
for (int i = 0; i < len; i++) {
39+
count[num[i]]++;
40+
}
41+
int t1, t2;
42+
t1 = count[1];
43+
count[0] = count[1] = 0;
44+
for (int i = 2; i < max; i++) {
45+
t2 = count[i];
46+
count[i] = t1 + count[i - 1];
47+
t1 = t2;
48+
}
49+
int key, index;
50+
for (int i = 0; i < len; i++) {
51+
key = orign[i];
52+
index = count[key];
53+
num[index] = orign[i];
54+
count[key]++;
55+
}
56+
}
57+
}

src/com/algorithm/sort/QuickSort.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ public class QuickSort {
1111
* 快速排序
1212
*
1313
* @param num
14-
* @param start
15-
* @param end
14+
* @param left
15+
* @param right
1616
*/
1717
public static void sort(int[] num, int left, int right) {
1818
if (left < right) {
@@ -23,7 +23,7 @@ public static void sort(int[] num, int left, int right) {
2323
}
2424

2525
/**
26-
* 数据分组
26+
* 数据分组
2727
*
2828
* @param num
2929
* @param left

0 commit comments

Comments
 (0)