Skip to content

Commit 87b4604

Browse files
committed
JS-Sorting-Algorithm
1 parent 643b68b commit 87b4604

File tree

15 files changed

+558
-258
lines changed

15 files changed

+558
-258
lines changed

Algorithms/src/main/java/com/jsonlog/algorithms/helloWorld.java

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.jsonlog.algorithms;
22

3+
import com.jsonlog.algorithms.sort.BubbleSort;
4+
35
import java.util.Arrays;
46
import java.util.Date;
57
import java.util.Random;
@@ -13,14 +15,35 @@ public static void main(String[] args) {
1315

1416

1517

16-
random();
18+
// random();
1719
// array();
1820
// movebit();
19-
//
21+
sort();
22+
23+
24+
2025
// try {
2126
// Thread.currentThread().sleep(5 * 1000);
2227
// } catch(InterruptedException e) {}
2328
}
29+
static void sort(){
30+
// 生成随机数组
31+
int[] array = randomArray(-1000, 1000, 100);
32+
// 使用 Arrays.sort() 排序作为对比
33+
// int[] sortedArray = Arrays.copyOf(array, array.length);
34+
// Arrays.sort(sortedArray);
35+
36+
BubbleSort bubbleSort = new BubbleSort();
37+
sout(bubbleSort.sort(array));
38+
}
39+
40+
static void sout(int[] array){
41+
for(int i : array){
42+
System.out.print(i+" ");
43+
}
44+
System.out.println();
45+
}
46+
2447
static void random(){
2548
for (int i = 0; i < 10; i++) {
2649
Random rand = new Random(47);//no change
@@ -78,4 +101,41 @@ static void movebit(){
78101
i = 127 << 1;
79102
System.out.println(""+i);
80103
}
104+
/**
105+
* 随机指定范围内N个不重复的数
106+
* 在初始化的无重复待选数组中随机产生一个数放入结果中,
107+
* 将待选数组被随机到的数,用待选数组(len-1)下标对应的数替换
108+
* 然后从len-2里随机产生下一个随机数,如此类推
109+
*
110+
* @param max 指定范围最大值
111+
* @param min 指定范围最小值
112+
* @param n 随机数个数
113+
* @return int[] 随机数结果集
114+
*/
115+
static public int[] randomArray(int min, int max, int n) {
116+
int len = max - min + 1;
117+
118+
if (max < min || n > len) {
119+
return null;
120+
}
121+
122+
//初始化给定范围的待选数组
123+
int[] source = new int[len];
124+
for (int i = min; i < min + len; i++) {
125+
source[i - min] = i;
126+
}
127+
128+
int[] result = new int[n];
129+
Random rd = new Random();
130+
int index = 0;
131+
for (int i = 0; i < result.length; i++) {
132+
//待选数组0到(len-2)随机一个下标
133+
index = Math.abs(rd.nextInt() % len--);
134+
//将随机到的数放入结果集
135+
result[i] = source[index];
136+
//将待选数组中被随机到的数,用待选数组(len-1)下标对应的数替换
137+
source[index] = source[len];
138+
}
139+
return result;
140+
}
81141
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.jsonlog.algorithms.sort;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* @author jsonlog
7+
* @date 2019-08-24
8+
*/
9+
public class BubbleSort {
10+
static public int[] sort(int[] sourceArray){
11+
// 对 arr 进行拷贝,不改变参数内容
12+
int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);
13+
14+
for (int i = 1; i < arr.length; i++) {
15+
// 设定一个标记,若为true,则表示此次循环没有进行交换,也就是待排序列已经有序,排序已经完成。
16+
boolean flag = true;
17+
18+
for (int j = 0; j < arr.length - i; j++) {
19+
if (arr[j] > arr[j + 1]) {
20+
int tmp = arr[j];
21+
arr[j] = arr[j + 1];
22+
arr[j + 1] = tmp;
23+
24+
flag = false;
25+
}
26+
}
27+
28+
if (flag) {
29+
break;
30+
}
31+
}
32+
return arr;
33+
}
34+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.jsonlog.algorithms.sort;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* @author jsonlog
7+
* @date 2019-08-24
8+
*/
9+
public class BucketSort {
10+
private static final InsertSort insertSort = new InsertSort();
11+
12+
public int[] sort(int[] sourceArray) throws Exception {
13+
// 对 arr 进行拷贝,不改变参数内容
14+
int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);
15+
16+
return bucketSort(arr, 5);
17+
}
18+
19+
private int[] bucketSort(int[] arr, int bucketSize) throws Exception {
20+
if (arr.length == 0) {
21+
return arr;
22+
}
23+
24+
int minValue = arr[0];
25+
int maxValue = arr[0];
26+
for (int value : arr) {
27+
if (value < minValue) {
28+
minValue = value;
29+
} else if (value > maxValue) {
30+
maxValue = value;
31+
}
32+
}
33+
34+
int bucketCount = (int) Math.floor((maxValue - minValue) / bucketSize) + 1;
35+
int[][] buckets = new int[bucketCount][0];
36+
37+
// 利用映射函数将数据分配到各个桶中
38+
for (int i = 0; i < arr.length; i++) {
39+
int index = (int) Math.floor((arr[i] - minValue) / bucketSize);
40+
buckets[index] = arrAppend(buckets[index], arr[i]);
41+
}
42+
43+
int arrIndex = 0;
44+
for (int[] bucket : buckets) {
45+
if (bucket.length <= 0) {
46+
continue;
47+
}
48+
// 对每个桶进行排序,这里使用了插入排序
49+
bucket = insertSort.sort(bucket);
50+
for (int value : bucket) {
51+
arr[arrIndex++] = value;
52+
}
53+
}
54+
55+
return arr;
56+
}
57+
58+
/**
59+
* 自动扩容,并保存数据
60+
*
61+
* @param arr
62+
* @param value
63+
*/
64+
private int[] arrAppend(int[] arr, int value) {
65+
arr = Arrays.copyOf(arr, arr.length + 1);
66+
arr[arr.length - 1] = value;
67+
return arr;
68+
}
69+
70+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.jsonlog.algorithms.sort;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* @author jsonlog
7+
* @date 2019-08-24
8+
*/
9+
public class CountingSort {
10+
static public int[] sort(int[] sourceArray){
11+
// 对 arr 进行拷贝,不改变参数内容
12+
int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);
13+
14+
int maxValue = getMaxValue(arr);
15+
16+
return countingSort(arr, maxValue);
17+
}
18+
19+
static private int[] countingSort(int[] arr, int maxValue) {
20+
int bucketLen = maxValue + 1;
21+
int[] bucket = new int[bucketLen];
22+
23+
for (int value : arr) {
24+
bucket[value]++;
25+
}
26+
27+
int sortedIndex = 0;
28+
for (int j = 0; j < bucketLen; j++) {
29+
while (bucket[j] > 0) {
30+
arr[sortedIndex++] = j;
31+
bucket[j]--;
32+
}
33+
}
34+
return arr;
35+
}
36+
37+
static private int getMaxValue(int[] arr) {
38+
int maxValue = arr[0];
39+
for (int value : arr) {
40+
if (maxValue < value) {
41+
maxValue = value;
42+
}
43+
}
44+
return maxValue;
45+
}
46+
47+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.jsonlog.algorithms.sort;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* @author jsonlog
7+
* @date 2019-08-24
8+
*/
9+
public class HeapSort {
10+
static public int[] sort(int[] sourceArray){
11+
// 对 arr 进行拷贝,不改变参数内容
12+
int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);
13+
14+
int len = arr.length;
15+
16+
buildMaxHeap(arr, len);
17+
18+
for (int i = len - 1; i > 0; i--) {
19+
swap(arr, 0, i);
20+
len--;
21+
heapify(arr, 0, len);
22+
}
23+
return arr;
24+
}
25+
26+
static private void buildMaxHeap(int[] arr, int len) {
27+
for (int i = (int) Math.floor(len / 2); i >= 0; i--) {
28+
heapify(arr, i, len);
29+
}
30+
}
31+
32+
static private void heapify(int[] arr, int i, int len) {
33+
int left = 2 * i + 1;
34+
int right = 2 * i + 2;
35+
int largest = i;
36+
37+
if (left < len && arr[left] > arr[largest]) {
38+
largest = left;
39+
}
40+
41+
if (right < len && arr[right] > arr[largest]) {
42+
largest = right;
43+
}
44+
45+
if (largest != i) {
46+
swap(arr, i, largest);
47+
heapify(arr, largest, len);
48+
}
49+
}
50+
51+
static private void swap(int[] arr, int i, int j) {
52+
int temp = arr[i];
53+
arr[i] = arr[j];
54+
arr[j] = temp;
55+
}
56+
57+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.jsonlog.algorithms.sort;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* @author jsonlog
7+
* @date 2019-08-24
8+
*/
9+
public class InsertSort {
10+
static public int[] sort(int[] sourceArray){
11+
// 对 arr 进行拷贝,不改变参数内容
12+
int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);
13+
14+
// 从下标为1的元素开始选择合适的位置插入,因为下标为0的只有一个元素,默认是有序的
15+
for (int i = 1; i < arr.length; i++) {
16+
17+
// 记录要插入的数据
18+
int tmp = arr[i];
19+
20+
// 从已经排序的序列最右边的开始比较,找到比其小的数
21+
int j = i;
22+
while (j > 0 && tmp < arr[j - 1]) {
23+
arr[j] = arr[j - 1];
24+
j--;
25+
}
26+
27+
// 存在比其小的数,插入
28+
if (j != i) {
29+
arr[j] = tmp;
30+
}
31+
32+
}
33+
return arr;
34+
}
35+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.jsonlog.algorithms.sort;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* @author jsonlog
7+
* @date 2019-08-24
8+
*/
9+
public class MergeSort {
10+
static public int[] sort(int[] sourceArray){
11+
// 对 arr 进行拷贝,不改变参数内容
12+
int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);
13+
14+
if (arr.length < 2) {
15+
return arr;
16+
}
17+
int middle = (int) Math.floor(arr.length / 2);
18+
19+
int[] left = Arrays.copyOfRange(arr, 0, middle);
20+
int[] right = Arrays.copyOfRange(arr, middle, arr.length);
21+
22+
return merge(sort(left), sort(right));
23+
}
24+
25+
static protected int[] merge(int[] left, int[] right) {
26+
int[] result = new int[left.length + right.length];
27+
int l = 0, r = 0, len = 0;
28+
while (len < left.length + right.length) {
29+
if (left[l] <= right[r]) {
30+
result[len++] = left[l++];
31+
32+
if (l == left.length) {
33+
for (int i = r; i < right.length; i++) {
34+
result[len++] = right[r++];
35+
}
36+
}
37+
} else {
38+
result[len++] = right[r++];
39+
40+
if (r == right.length) {
41+
for (int i = l; i < left.length; i++) {
42+
result[len++] = left[l++];
43+
}
44+
}
45+
}
46+
}
47+
48+
return result;
49+
}
50+
51+
}

0 commit comments

Comments
 (0)