Skip to content

Commit 3e36aee

Browse files
committed
sort
1 parent defd14a commit 3e36aee

File tree

13 files changed

+334
-395
lines changed

13 files changed

+334
-395
lines changed
Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
package com.github.kuangcp.found;
22

33
/**
4-
* TODO 当数据有重复的时候,这个算法并不能做到返回数据第一次出现的地方
4+
* 当数据有重复的时候,这个算法并不能做到返回数据第一次出现的地方
55
*/
66
public class BinarySearch {
7-
private static int index = -2;
7+
private static int index = -2;
88

9-
private static void found(int[] arr, int left, int right, int data) {
9+
private static void found(int[] arr, int left, int right, int data) {
1010

11-
int mid = (left + right) / 2;
12-
if (arr[mid] == data) {
13-
index = mid;
14-
}
11+
int mid = (left + right) / 2;
12+
if (arr[mid] == data) {
13+
index = mid;
14+
}
1515

16-
if (arr[mid] > data && left < mid - 1) {
17-
found(arr, left, mid, data);
18-
}
16+
if (arr[mid] > data && left < mid - 1) {
17+
found(arr, left, mid, data);
18+
}
1919

20-
if (arr[mid] < data && right > mid + 1) {
21-
found(arr, mid, right, data);
20+
if (arr[mid] < data && right > mid + 1) {
21+
found(arr, mid, right, data);
22+
}
2223
}
23-
}
2424

25-
/**
26-
* find num from arr
27-
*
28-
* @return index not found then return -1
29-
*/
30-
public int find(int[] arr, int data) {
31-
found(arr, 0, arr.length, data);
32-
return index + 1;
33-
}
25+
/**
26+
* find num from arr
27+
*
28+
* @return index not found then return -1
29+
*/
30+
public int find(int[] arr, int data) {
31+
found(arr, 0, arr.length, data);
32+
return index + 1;
33+
}
3434
}

algorithms/src/main/java/com/github/kuangcp/sort/Bubble.java

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,22 @@
88
*
99
* @author kcp
1010
*/
11-
public enum Bubble implements SortAlgorithm {
11+
public class Bubble {
1212

13-
INSTANCE;
13+
public static int[] sort(int[] data) {
14+
int[] result = Arrays.copyOf(data, data.length);
1415

15-
public int[] sort(int[] data) {
16-
int[] result = Arrays.copyOf(data, data.length);
17-
18-
for (int i = 1; i < result.length; i++) {
19-
//用来冒泡的语句,0到已排序的部分
20-
for (int j = 0; j < result.length - i; j++) {
21-
//大就交换,把最大的沉入最后
22-
if (result[j] > result[j + 1]) {
23-
int temp = result[j + 1];
24-
result[j + 1] = result[j];
25-
result[j] = temp;
16+
for (int i = 1; i < result.length; i++) {
17+
//用来冒泡的语句,0到已排序的部分
18+
for (int j = 0; j < result.length - i; j++) {
19+
//大就交换,把最大的沉入最后
20+
if (result[j] > result[j + 1]) {
21+
int temp = result[j + 1];
22+
result[j + 1] = result[j];
23+
result[j] = temp;
24+
}
25+
}
2626
}
27-
}
27+
return result;
2828
}
29-
return result;
30-
}
3129
}

algorithms/src/main/java/com/github/kuangcp/sort/Insert.java

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,28 @@
55
/**
66
* 插入法排序,由小到大
77
* 最坏的情况就是数列是有序的大到小,那么需要比较和移动 n(n+1)/2 次 时间复杂度是O(n^2)
8-
*
8+
* <p>
99
* 指针从第二个数开始,后移,发现当前数比前面一个数小就把前面那个数后移,往前比较,知道找到那个数小于当前数为止,指针后移
1010
* 直至到最后一个
1111
* 思想是,指针位置之前的数都是有序的
1212
*/
13-
public enum Insert implements SortAlgorithm {
13+
public class Insert {
1414

15-
INSTANCE;
15+
public static int[] sort(int[] data) {
16+
int[] result = Arrays.copyOf(data, data.length);
17+
int i, j, tmp;
1618

17-
public int[] sort(int[] data) {
18-
int[] result = Arrays.copyOf(data, data.length);
19-
int i, j, tmp;
20-
21-
for (i = 1; i < result.length; i++) {
22-
tmp = result[i];
23-
j = i - 1;
24-
//比较,如果是比前面还要小,就将数字往后移动一位。将小的那一位插入到合适位置
25-
while (j >= 0 && tmp < result[j]) {
26-
result[j + 1] = result[j];
27-
j--;
28-
}
29-
result[j + 1] = tmp;
19+
for (i = 1; i < result.length; i++) {
20+
tmp = result[i];
21+
j = i - 1;
22+
//比较,如果是比前面还要小,就将数字往后移动一位。将小的那一位插入到合适位置
23+
while (j >= 0 && tmp < result[j]) {
24+
result[j + 1] = result[j];
25+
j--;
26+
}
27+
result[j + 1] = tmp;
28+
}
29+
return result;
3030
}
31-
return result;
32-
}
3331

3432
}

algorithms/src/main/java/com/github/kuangcp/sort/Quick.java

Lines changed: 113 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -9,137 +9,134 @@
99
*
1010
* @author Myth
1111
*/
12-
public enum Quick implements SortAlgorithm {
12+
public class Quick {
1313

14-
INSTANCE;
15-
16-
@Override
17-
public int[] sort(int[] data) {
18-
int[] result = Arrays.copyOf(data, data.length);
19-
QuickSort sort = new FirstImpl();
20-
sort.sortData(result, 0, result.length - 1);
21-
return result;
22-
}
23-
24-
25-
interface QuickSort {
26-
27-
void sortData(int[] data, int low, int high);
28-
}
14+
public static int[] sort(int[] data) {
15+
int[] result = Arrays.copyOf(data, data.length);
16+
QuickSort sort = new FirstImpl();
17+
sort.sortData(result, 0, result.length - 1);
18+
return result;
19+
}
2920

30-
/**
31-
* 高效的写法
32-
*/
33-
static class FirstImpl implements QuickSort {
3421

35-
@Override
36-
public void sortData(int[] data, int low, int high) {
37-
if (low >= high) {
38-
return;
39-
}
22+
interface QuickSort {
4023

41-
int lowIndex = low;
42-
int highIndex = high;
24+
void sortData(int[] data, int low, int high);
25+
}
4326

44-
int value = data[low];
45-
while (lowIndex < highIndex) {
46-
// 找出右边小于低位所在的标识值
47-
while (lowIndex < highIndex && data[highIndex] >= value) {
48-
highIndex -= 1;
27+
/**
28+
* 高效的写法
29+
*/
30+
static class FirstImpl implements QuickSort {
31+
32+
@Override
33+
public void sortData(int[] data, int low, int high) {
34+
if (low >= high) {
35+
return;
36+
}
37+
38+
int lowIndex = low;
39+
int highIndex = high;
40+
41+
int value = data[low];
42+
while (lowIndex < highIndex) {
43+
// 找出右边小于低位所在的标识值
44+
while (lowIndex < highIndex && data[highIndex] >= value) {
45+
highIndex -= 1;
46+
}
47+
data[lowIndex] = data[highIndex];
48+
49+
// 找出左边大于标识值
50+
while (lowIndex < highIndex && data[lowIndex] <= value) {
51+
lowIndex += 1;
52+
}
53+
data[highIndex] = data[lowIndex];
54+
}
55+
56+
data[lowIndex] = value;
57+
58+
sortData(data, low, lowIndex - 1);
59+
sortData(data, highIndex + 1, high);
4960
}
50-
data[lowIndex] = data[highIndex];
61+
}
5162

52-
// 找出左边大于标识值
53-
while (lowIndex < highIndex && data[lowIndex] <= value) {
54-
lowIndex += 1;
63+
/**
64+
* 个人手写
65+
*/
66+
static class SecondImpl implements QuickSort {
67+
68+
@Override
69+
public void sortData(int[] data, int low, int high) {
70+
int lowIndex = low;
71+
int highIndex = high;
72+
int index = data[low];
73+
74+
while (lowIndex < highIndex) {
75+
while (lowIndex < highIndex && data[highIndex] >= index) {
76+
highIndex--;
77+
}
78+
if (lowIndex < highIndex) {
79+
int temp = data[highIndex];
80+
data[highIndex] = data[lowIndex];
81+
data[lowIndex] = temp;
82+
lowIndex++;
83+
}
84+
85+
while (lowIndex < highIndex && data[lowIndex] <= index) {
86+
lowIndex++;
87+
}
88+
if (lowIndex < highIndex) {
89+
int temp = data[highIndex];
90+
data[highIndex] = data[lowIndex];
91+
data[lowIndex] = temp;
92+
highIndex--;
93+
}
94+
}
95+
96+
if (lowIndex > low) {
97+
sortData(data, low, lowIndex - 1);
98+
}
99+
if (highIndex < high) {
100+
sortData(data, lowIndex + 1, high);
101+
}
55102
}
56-
data[highIndex] = data[lowIndex];
57-
}
103+
}
58104

59-
data[lowIndex] = value;
105+
/**
106+
* 对象数组排序
107+
*/
108+
public <T extends Comparable<? super T>> T[] quickSort(T[] data, int start, int end) {
109+
int low = start + 1, high = end;
110+
T key = data[start];
60111

61-
sortData(data, low, lowIndex - 1);
62-
sortData(data, highIndex + 1, high);
63-
}
64-
}
65-
66-
/**
67-
* 个人手写
68-
*/
69-
static class SecondImpl implements QuickSort {
70-
71-
@Override
72-
public void sortData(int[] data, int low, int high) {
73-
int lowIndex = low;
74-
int highIndex = high;
75-
int index = data[low];
76-
77-
while (lowIndex < highIndex) {
78-
while (lowIndex < highIndex && data[highIndex] >= index) {
79-
highIndex--;
112+
if (start >= end) {
113+
return (data);
80114
}
81-
if (lowIndex < highIndex) {
82-
int temp = data[highIndex];
83-
data[highIndex] = data[lowIndex];
84-
data[lowIndex] = temp;
85-
lowIndex++;
115+
116+
while (true) {
117+
while (data[high].compareTo(key) > 0) {
118+
high--;
119+
}
120+
while (data[low].compareTo(key) < 0 && low < high) {
121+
low++;
122+
}
123+
if (low >= high) {
124+
break;
125+
}
126+
if (data[low] == key) {
127+
high--;
128+
} else {
129+
low++;
130+
}
86131
}
87132

88-
while (lowIndex < highIndex && data[lowIndex] <= index) {
89-
lowIndex++;
133+
if (start < low - 1) {
134+
this.quickSort(data, start, low - 1);
90135
}
91-
if (lowIndex < highIndex) {
92-
int temp = data[highIndex];
93-
data[highIndex] = data[lowIndex];
94-
data[lowIndex] = temp;
95-
highIndex--;
136+
if (high + 1 < end) {
137+
this.quickSort(data, high + 1, end);
96138
}
97-
}
98-
99-
if (lowIndex > low) {
100-
sortData(data, low, lowIndex - 1);
101-
}
102-
if (highIndex < high) {
103-
sortData(data, lowIndex + 1, high);
104-
}
105-
}
106-
}
107-
108-
/**
109-
* 对象数组排序
110-
*/
111-
public <T extends Comparable<? super T>> T[] quickSort(T[] data, int start, int end) {
112-
int low = start + 1, high = end;
113-
T key = data[start];
114-
115-
if (start >= end) {
116-
return (data);
117-
}
118139

119-
while (true) {
120-
while (data[high].compareTo(key) > 0) {
121-
high--;
122-
}
123-
while (data[low].compareTo(key) < 0 && low < high) {
124-
low++;
125-
}
126-
if (low >= high) {
127-
break;
128-
}
129-
if (data[low] == key) {
130-
high--;
131-
} else {
132-
low++;
133-
}
140+
return data;
134141
}
135-
136-
if (start < low - 1) {
137-
this.quickSort(data, start, low - 1);
138-
}
139-
if (high + 1 < end) {
140-
this.quickSort(data, high + 1, end);
141-
}
142-
143-
return data;
144-
}
145142
}

0 commit comments

Comments
 (0)