parent
11d7fb7c56
commit
a210e12948
@ -0,0 +1,78 @@
|
|||||||
|
package leo.class05_08;
|
||||||
|
|
||||||
|
import leo.util.ArrayUtil;
|
||||||
|
import sun.jvm.hotspot.oops.BranchData;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Leo
|
||||||
|
* @ClassName CountSort
|
||||||
|
* @DATE 2020/12/2 2:03 下午
|
||||||
|
* @Description 计数排序(桶排序)
|
||||||
|
*/
|
||||||
|
public class CountSort {
|
||||||
|
|
||||||
|
|
||||||
|
public static void countSort(int[] arr) {
|
||||||
|
if (arr.length < 2 || arr == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int max = Integer.MIN_VALUE;
|
||||||
|
for (int i = 0; i < arr.length; i++) {
|
||||||
|
max = Math.max(max, arr[i]);
|
||||||
|
}
|
||||||
|
int[] bucket = new int[max + 1];
|
||||||
|
for (int i = 0; i < arr.length; i++) {
|
||||||
|
bucket[arr[i]]++;
|
||||||
|
}
|
||||||
|
int i = 0;
|
||||||
|
for (int j = 0; j < bucket.length; j++) {
|
||||||
|
while (bucket[j]-- > 0) {
|
||||||
|
arr[i++] = j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args){
|
||||||
|
int maxSize = 100;
|
||||||
|
int range = 100;
|
||||||
|
int testTime = 10000;
|
||||||
|
System.out.println("start!");
|
||||||
|
|
||||||
|
for (int i = 0; i < testTime; i++) {
|
||||||
|
int[] arr = randomArray(maxSize, range);
|
||||||
|
int[] copyArray = copyArray(arr);
|
||||||
|
countSort(arr);
|
||||||
|
Arrays.sort(copyArray);
|
||||||
|
if (!ArrayUtil.isEqual(arr, copyArray)) {
|
||||||
|
ArrayUtil.printArr(arr, "arr");
|
||||||
|
ArrayUtil.printArr(copyArray, "copyArray");
|
||||||
|
System.out.println("fuck!");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println("end!");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int[] randomArray(int maxSize, int range) {
|
||||||
|
int[] arr = new int[(int) (maxSize * Math.random() + 1)];
|
||||||
|
for (int i = 0; i < arr.length; i++) {
|
||||||
|
arr[i] = (int) (range * Math.random()) + 1;
|
||||||
|
}
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int[] copyArray(int[] arr) {
|
||||||
|
int[] copyArr = new int[arr.length];
|
||||||
|
for (int i = 0; i < arr.length; i++) {
|
||||||
|
copyArr[i] = arr[i];
|
||||||
|
}
|
||||||
|
return copyArr;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,108 @@
|
|||||||
|
package leo.class05_08;
|
||||||
|
|
||||||
|
import leo.util.ArrayUtil;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Leo
|
||||||
|
* @ClassName RadixSort
|
||||||
|
* @DATE 2020/12/2 3:55 下午
|
||||||
|
* @Description 基数排序 要求必须是正整数
|
||||||
|
* O(n*log10^max)
|
||||||
|
* O(n)
|
||||||
|
*/
|
||||||
|
public class RadixSort {
|
||||||
|
|
||||||
|
public static void radixSort(int[] arr) {
|
||||||
|
if (arr.length < 2 || arr == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
radixSort(arr, 0, arr.length - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void radixSort(int[] arr, int l, int r) {
|
||||||
|
if (l >= r) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int i, j = 0;
|
||||||
|
int bits = maxBits(arr, l, r);
|
||||||
|
final int radix = 10;
|
||||||
|
int[] help = new int[r - l + 1];
|
||||||
|
for (int b = 1; b <= bits; b++) {
|
||||||
|
int[] count = new int[radix];
|
||||||
|
for (i = l; i <= r; i++) {
|
||||||
|
j = getRemainder(arr[i], b);
|
||||||
|
count[j]++;
|
||||||
|
}
|
||||||
|
for (i = 1; i < count.length; i++) {
|
||||||
|
count[i] = count[i] + count[i - 1];
|
||||||
|
}
|
||||||
|
for (i = r; i >= l; i--) {
|
||||||
|
j = getRemainder(arr[i], b);
|
||||||
|
count[j]--;
|
||||||
|
help[count[j]] = arr[i];
|
||||||
|
}
|
||||||
|
for (i = 0; i <= r; i++) {
|
||||||
|
arr[i + l] = help[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int maxBits(int[] arr, int l, int r) {
|
||||||
|
int res = 0;
|
||||||
|
int max = Integer.MIN_VALUE;
|
||||||
|
for (int i = l; i <= r; i++) {
|
||||||
|
max = Math.max(max, arr[i]);
|
||||||
|
}
|
||||||
|
while (max != 0) {
|
||||||
|
max /= 10;
|
||||||
|
res++;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int getRemainder(int num, int b) {
|
||||||
|
return ((num / (int) Math.pow(10, b - 1)) % 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class RadixSortMain {
|
||||||
|
|
||||||
|
public static void main(String[] args){
|
||||||
|
int testTime = 1000;
|
||||||
|
int range = 1000;
|
||||||
|
int maxSize = 40;
|
||||||
|
System.out.println("start");
|
||||||
|
|
||||||
|
for (int i = 0; i < testTime; i++) {
|
||||||
|
int[] arr = randomArray(maxSize, range);
|
||||||
|
int[] copyArray = copyArray(arr);
|
||||||
|
Arrays.sort(copyArray);
|
||||||
|
RadixSort.radixSort(arr);
|
||||||
|
if (!ArrayUtil.isEqual(arr, copyArray)) {
|
||||||
|
System.out.println("fuck!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println("end");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int[] randomArray(int maxSize, int range) {
|
||||||
|
int[] arr = new int[(int) (maxSize * Math.random()) + 1];
|
||||||
|
for (int i = 0; i < arr.length; i++) {
|
||||||
|
arr[i] = (int) (range * Math.random()) + 1;
|
||||||
|
}
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int[] copyArray(int[] arr) {
|
||||||
|
int[] copyArr = new int[arr.length];
|
||||||
|
for (int i = 0; i < copyArr.length; i++) {
|
||||||
|
copyArr[i] = arr[i];
|
||||||
|
}
|
||||||
|
return copyArr;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,222 @@
|
|||||||
|
package leo.class05_08;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Leo
|
||||||
|
* @ClassName TrieTree_Map
|
||||||
|
* @DATE 2020/12/2 10:33 上午
|
||||||
|
* @Description next 用map实现
|
||||||
|
*/
|
||||||
|
public class TrieTree_Map {
|
||||||
|
|
||||||
|
private class Node {
|
||||||
|
private int pass;
|
||||||
|
private int end;
|
||||||
|
private HashMap<Integer,Node> next;
|
||||||
|
|
||||||
|
public Node() {
|
||||||
|
this.pass = 0;
|
||||||
|
this.end = 0;
|
||||||
|
this.next = new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private Node root;
|
||||||
|
|
||||||
|
public TrieTree_Map() {
|
||||||
|
this.root = new Node();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void insert(String word) {
|
||||||
|
if (word == null || "".equals(word.trim())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Node node = this.root;
|
||||||
|
root.pass++;
|
||||||
|
int path;
|
||||||
|
char[] chars = word.toCharArray();
|
||||||
|
|
||||||
|
for (int i = 0; i < chars.length; i++) {
|
||||||
|
path = chars[i];
|
||||||
|
if (node.next.get(path) == null) {
|
||||||
|
node.next.put(path, new Node());
|
||||||
|
}
|
||||||
|
node = node.next.get(path);
|
||||||
|
node.pass++;
|
||||||
|
}
|
||||||
|
node.end++;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void delete(String word) {
|
||||||
|
if (search(word) == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Node node = this.root;
|
||||||
|
node.pass--;
|
||||||
|
int path;
|
||||||
|
char[] chars = word.toCharArray();
|
||||||
|
for (int i = 0; i < chars.length; i++) {
|
||||||
|
path = chars[i];
|
||||||
|
if (--node.next.get(path).pass == 0) {
|
||||||
|
node.next.remove(path);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
node = node.next.get(path);
|
||||||
|
}
|
||||||
|
node.end--;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public int search(String word) {
|
||||||
|
if (word == null || "".equals(word.trim())) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
Node node = this.root;
|
||||||
|
int path;
|
||||||
|
char[] chars = word.toCharArray();
|
||||||
|
for (int i = 0; i < chars.length; i++) {
|
||||||
|
path = chars[i];
|
||||||
|
if (node.next.get(path) == null) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
node = node.next.get(path);
|
||||||
|
}
|
||||||
|
return node.end;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int prefixNumber(String word) {
|
||||||
|
if (word == null || "".equals(word.trim())) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
Node node = this.root;
|
||||||
|
char[] chars = word.toCharArray();
|
||||||
|
int path;
|
||||||
|
for (int i = 0; i < chars.length; i++) {
|
||||||
|
path = chars[i];
|
||||||
|
if (node.next.get(path) == null) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
node = node.next.get(path);
|
||||||
|
}
|
||||||
|
return node.pass;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
class TrieTree_Map_Test {
|
||||||
|
private HashMap<String,Integer> map;
|
||||||
|
public TrieTree_Map_Test(){
|
||||||
|
this.map = new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void insert(String word) {
|
||||||
|
if (word.trim().length() == 0 || word == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (map.containsKey(word)) {
|
||||||
|
map.put(word, map.get(word) + 1);
|
||||||
|
}else{
|
||||||
|
map.put(word, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void delete(String word) {
|
||||||
|
if (this.search(word) == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (this.map.get(word) == 1) {
|
||||||
|
map.remove(word);
|
||||||
|
}else{
|
||||||
|
this.map.put(word, this.map.get(word) - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int search(String word) {
|
||||||
|
if (word.trim().length() == 0 || word == null) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (map.containsKey(word)) {
|
||||||
|
return map.get(word);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int prefixNumber(String word) {
|
||||||
|
if (word.trim().length() == 0 || word == null) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int count = 0;
|
||||||
|
for (String str : this.map.keySet()) {
|
||||||
|
if (str.startsWith(word)) {
|
||||||
|
count += this.map.get(str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class TrieTree_MapMain{
|
||||||
|
|
||||||
|
public static void main(String[] args){
|
||||||
|
int strArrayLen = 100;
|
||||||
|
int strLen = 50;
|
||||||
|
int testTime = 10000;
|
||||||
|
System.out.println("start");
|
||||||
|
for (int i = 0; i < testTime; i++) {
|
||||||
|
TrieTree_Map trieTree_map = new TrieTree_Map();
|
||||||
|
TrieTree_Map_Test test = new TrieTree_Map_Test();
|
||||||
|
String[] strings = generateRandomString(strArrayLen, strLen);
|
||||||
|
for (int j = 0; j < strings.length; j++) {
|
||||||
|
int random = (int) Math.random();
|
||||||
|
|
||||||
|
if (random < 0.25) {
|
||||||
|
trieTree_map.insert(strings[j]);
|
||||||
|
test.insert(strings[j]);
|
||||||
|
}else if (random<0.5){
|
||||||
|
trieTree_map.delete(strings[j]);
|
||||||
|
test.delete(strings[j]);
|
||||||
|
}else if(random<0.75){
|
||||||
|
int mapCount = trieTree_map.search(strings[j]);
|
||||||
|
int testCount = test.search(strings[j]);
|
||||||
|
if (mapCount != testCount) {
|
||||||
|
System.out.println("mapCount : " + mapCount + " testCount : " + testCount);
|
||||||
|
System.out.println("count fuck!");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
int mapPreCount = trieTree_map.prefixNumber(strings[j]);
|
||||||
|
int testPreCount = test.prefixNumber(strings[j]);
|
||||||
|
if (mapPreCount != testPreCount) {
|
||||||
|
System.out.println("mapPreCount : " + mapPreCount + " testPreCount : " + testPreCount);
|
||||||
|
System.out.println("pre count fuck!");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println("end");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static String[] generateRandomString(int strArrayLen, int strLen) {
|
||||||
|
String[] strArray = new String[(int) (strArrayLen * Math.random() + 1)];
|
||||||
|
|
||||||
|
for (int i = 0; i < strArray.length; i++) {
|
||||||
|
strArray[i] = generateRandomString(strLen);
|
||||||
|
}
|
||||||
|
return strArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String generateRandomString(int strLen) {
|
||||||
|
char[] chars = new char[(int) (Math.random() * strLen + 1)];
|
||||||
|
for (int i = 0; i < chars.length; i++) {
|
||||||
|
int value = (int) (Math.random() * 6);
|
||||||
|
chars[i] = (char) (97 + value);
|
||||||
|
}
|
||||||
|
return String.valueOf(chars);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in new issue