Skip to content

Commit aed3cd4

Browse files
committed
[Function add]
1.Add conclusions about selection sort and insert sort.
1 parent c1aa792 commit aed3cd4

File tree

9 files changed

+304
-135
lines changed

9 files changed

+304
-135
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package ca.mcmaster.chapter.two.Sort;
2+
3+
public class BubbleSort {
4+
public int[] a = {1, 2,1,4,2,6,234,65,23,5,2,657,2,546};
5+
public int[] sort(){
6+
for(int i = 0; i < a.length; i++){
7+
for(int j = i; j < a.length; j++){
8+
if(a[i] > a[j]){
9+
int temp = a[i];
10+
a[i] = a[j];
11+
a[j] = temp;
12+
}
13+
}
14+
}
15+
return a;
16+
}
17+
public static void main(String[] args) {
18+
BubbleSort sort = new BubbleSort();
19+
int[] result = sort.sort();
20+
for(int i : result)
21+
System.out.println(i);
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package ca.mcmaster.chapter.two.Sort;
2+
3+
public class InsertionSort<T> implements Sort<T> {
4+
@Override
5+
public void sort(Comparable<T>[] a) {
6+
int size = a.length;
7+
for(int i = 0; i < size; i ++){
8+
for(int j = i; j > 0 && Sort.less(a, j, j - 1); j--){ Sort.swap(a, j, j-1); }
9+
}
10+
}
11+
public static void main(String[] args) {
12+
Sort<Integer> sort = new SelectionSort<>();
13+
Integer[] a = {1, 2,1,4,2,6,234,65,23,5,2,657,2,546};
14+
sort.sort(a);
15+
Sort.show(a);
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package ca.mcmaster.chapter.two.Sort;
2+
3+
public class SelectionSort<T> implements Sort<T> {
4+
5+
@Override
6+
public void sort(Comparable<T>[] a) {
7+
int len = a.length;
8+
for(int i = 0; i < len; i++){
9+
for(int j = i; j < len; j++){
10+
if(Sort.less(a, i, j)){ Sort.swap(a, i, j); }
11+
}
12+
}
13+
}
14+
public static void main(String[] args) {
15+
Sort<Integer> sort = new SelectionSort<>();
16+
Integer[] a = {1, 2,1,4,2,6,234,65,23,5,2,657,2,546};
17+
sort.sort(a);
18+
Sort.show(a);
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -1,143 +1,24 @@
11
package ca.mcmaster.chapter.two.Sort;
22

3-
public class Sort {
4-
private static Comparable[] aux;
5-
@SuppressWarnings("rawtypes")
6-
public static void selectionSort(Comparable[] a){
7-
int length = a.length;
8-
for(int i = 0; i < length; i++){
9-
int min = i;
10-
for(int j = i + 1; j < length; j++)
11-
if(less(a[j], a[min])) min = j;
12-
swap(a, i, min);
13-
}
14-
}
15-
16-
public static void insertSort(Comparable[] a){
17-
int length = a.length;
18-
for(int i = 1; i < length; i ++){
19-
for(int j = i; j > 0 && less(a[j], a[j-1]); j--)
20-
swap(a, j, j-1);
21-
}
22-
}
23-
24-
public static void ShellSort(Comparable[] a){
25-
int length = a.length;
26-
int h = 1;
27-
while(h < length/3) h = 3*h + 1;
28-
while(h >= 1){
29-
for(int i = h; i < length; i++){
30-
for(int j = i; j >= h && less(a[j], a[j-h]); j -= h)
31-
swap(a, j, j-h);
32-
}
33-
h /= 3;
34-
}
35-
}
36-
37-
public static void mergeSort(Comparable[] a){
38-
aux = new Comparable[a.length];
39-
mergeSortIn(a, 0, a.length - 1);
40-
}
41-
42-
public static void mergeSortIn(Comparable[] a, int lo, int hi){
43-
if(lo >= hi) return;
44-
int mid = (hi - lo) / 2 + lo;
45-
mergeSortIn(a, lo, mid);
46-
mergeSortIn(a, mid + 1, hi);
47-
merge(a, lo, mid, hi);
48-
}
49-
50-
public static void mergeSortBU(Comparable[] a){
51-
int length = a.length;
52-
aux = new Comparable[length];
53-
for(int sz = 1; sz < length; sz *= 2){
54-
for(int lo = 0; lo < length - sz; lo += sz * 2)
55-
merge(a, lo, lo+sz-1, Math.min(lo+sz+sz-1, length-1));
56-
}
57-
}
58-
59-
public static Boolean less(Comparable a, Comparable b){
60-
return a.compareTo(b) < 0;
61-
}
62-
public static void swap(Comparable[] a, int i, int j){
3+
public interface Sort<T> {
4+
/**
5+
* @Description: Sort the array in a descent order.
6+
* @param a: array to sort
7+
*/
8+
public void sort(Comparable<T>[] a);
9+
@SuppressWarnings("unchecked")
10+
static <T> boolean less(Comparable<T>[] a, int i, int j){
11+
return a[i].compareTo((T)a[j]) < 0;
12+
}
13+
static void swap(Comparable[] a, int i, int j){
6314
Comparable temp = a[i];
6415
a[i] = a[j];
6516
a[j] = temp;
6617
}
67-
public static void show(Comparable[] a){
68-
for(int i = 0; i < a.length; i++){
69-
System.out.print(a[i] + " ");
70-
}
71-
System.out.println();
72-
}
73-
public static Boolean isSorted(Comparable[] a){
74-
for(int i = 1; i < a.length; i++)
75-
if(less(a[i], a[i-1])) return false;
76-
return true;
77-
}
78-
public static void merge(Comparable[] a, int lo, int mid, int hi){
79-
int i = lo, j = mid + 1;
80-
if(less(a[mid], a[mid+1])) return;
81-
for (int k = lo; k < aux.length; k++)
82-
aux[k] = a[k];
83-
for(int k = lo; k < a.length; k++){
84-
if(i > mid) a[k] = aux[j++];
85-
else if(j > hi) a[k] = aux[i++];
86-
else if(less(aux[i], aux[j])) a[k] = aux[i++];
87-
else a[k] = aux[j++];
88-
}
89-
}
90-
91-
public static int partition(Comparable[] a, int lo, int hi){
92-
int i = lo, j = hi + 1;
93-
Comparable v = a[lo];
94-
while(true){
95-
while(less(a[++i], v)) if(i == hi) break;
96-
while(less(v, a[--j])) if(j == lo) break;
97-
if(i >= j) break;
98-
swap(a, i, j);
99-
}
100-
swap(a, lo, j);
101-
return j;
102-
}
103-
104-
public static int M = 5;
105-
public static void quickSort(Comparable[] a, int lo, int hi){
106-
// if(lo >= hi) return;
107-
if(lo + M >= hi) {
108-
insertSort(a);
109-
return;
110-
}
111-
int j = partition(a, lo, hi);
112-
quickSort(a, lo, j-1);
113-
quickSort(a, j+1, hi);
114-
}
115-
116-
public static void quickSort3Way(Comparable[] a, int lo, int hi){
117-
if(lo >= hi) return;
118-
int lt = lo, i = lo + 1, gt = hi;
119-
Comparable v = a[lo];
120-
while(i <= gt){
121-
int cmp = a[i].compareTo(v);
122-
if(cmp < 0) swap(a, lt++, i++);
123-
else if(cmp > 0) swap(a, i, gt--);
124-
else i++;
125-
}
126-
quickSort3Way(a, lo, lt - 1);
127-
quickSort3Way(a, gt + 1, hi);
128-
}
129-
130-
public static void main(String[] args) {
131-
Integer[] a = new Integer[]{18,27,33,55,6,3,23,2,3,5,2,45,1,4,2,5,7,3,7,432,96,7,23,8};
132-
// Integer[] a = new Integer[]{1,3,5,9,2,5,6,7};
133-
// Integer[] a = new Integer[]{2,1,3,4,5,6,7,8,9};
134-
// selectionSort(a);
135-
// insertSort(a);
136-
// ShellSort(a);
137-
// merge(a, 0, 3, 7);
138-
// mergeSortBU(a);
139-
// quickSort(a, 0, a.length -1);
140-
quickSort3Way(a, 0, a.length - 1);
141-
show(a);
18+
static void show(Comparable[] a){
19+
StringBuilder sb = new StringBuilder();
20+
for(int i = 0; i < a.length; i++)
21+
sb.append(a[i] + " ");
22+
System.out.println(sb.toString());
14223
}
14324
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package ca.mcmaster.chapter.two.Sort;
2+
3+
import java.util.LinkedList;
4+
import java.util.Stack;
5+
6+
public class SortOld {
7+
private static Comparable[] aux;
8+
@SuppressWarnings("rawtypes")
9+
public static void selectionSort(Comparable[] a){
10+
int length = a.length;
11+
for(int i = 0; i < length; i++){
12+
int min = i;
13+
for(int j = i + 1; j < length; j++)
14+
if(less(a[j], a[min])) min = j;
15+
swap(a, i, min);
16+
}
17+
}
18+
19+
public static void insertSort(Comparable[] a){
20+
int length = a.length;
21+
for(int i = 1; i < length; i ++){
22+
for(int j = i; j > 0 && less(a[j], a[j-1]); j--)
23+
swap(a, j, j-1);
24+
}
25+
}
26+
27+
public static void ShellSort(Comparable[] a){
28+
int length = a.length;
29+
int h = 1;
30+
while(h < length/3) h = 3*h + 1;
31+
while(h >= 1){
32+
for(int i = h; i < length; i++){
33+
for(int j = i; j >= h && less(a[j], a[j-h]); j -= h)
34+
swap(a, j, j-h);
35+
}
36+
h /= 3;
37+
}
38+
}
39+
40+
public static void mergeSort(Comparable[] a){
41+
aux = new Comparable[a.length];
42+
mergeSortIn(a, 0, a.length - 1);
43+
}
44+
45+
public static void mergeSortIn(Comparable[] a, int lo, int hi){
46+
if(lo >= hi) return;
47+
int mid = (hi - lo) / 2 + lo;
48+
mergeSortIn(a, lo, mid);
49+
mergeSortIn(a, mid + 1, hi);
50+
merge(a, lo, mid, hi);
51+
}
52+
53+
public static void mergeSortBU(Comparable[] a){
54+
int length = a.length;
55+
aux = new Comparable[length];
56+
for(int sz = 1; sz < length; sz *= 2){
57+
for(int lo = 0; lo < length - sz; lo += sz * 2)
58+
merge(a, lo, lo+sz-1, Math.min(lo+sz+sz-1, length-1));
59+
}
60+
}
61+
62+
public static Boolean less(Comparable a, Comparable b){
63+
return a.compareTo(b) < 0;
64+
}
65+
public static void swap(Comparable[] a, int i, int j){
66+
Comparable temp = a[i];
67+
a[i] = a[j];
68+
a[j] = temp;
69+
}
70+
public static void show(Comparable[] a){
71+
for(int i = 0; i < a.length; i++){
72+
System.out.print(a[i] + " ");
73+
}
74+
System.out.println();
75+
}
76+
public static Boolean isSorted(Comparable[] a){
77+
for(int i = 1; i < a.length; i++)
78+
if(less(a[i], a[i-1])) return false;
79+
return true;
80+
}
81+
public static void merge(Comparable[] a, int lo, int mid, int hi){
82+
int i = lo, j = mid + 1;
83+
if(less(a[mid], a[mid+1])) return;
84+
for (int k = lo; k < aux.length; k++)
85+
aux[k] = a[k];
86+
for(int k = lo; k < a.length; k++){
87+
if(i > mid) a[k] = aux[j++];
88+
else if(j > hi) a[k] = aux[i++];
89+
else if(less(aux[i], aux[j])) a[k] = aux[i++];
90+
else a[k] = aux[j++];
91+
}
92+
}
93+
94+
public static int partition(Comparable[] a, int lo, int hi){
95+
int i = lo, j = hi + 1;
96+
Comparable v = a[lo];
97+
while(true){
98+
while(less(a[++i], v)) if(i == hi) break;
99+
while(less(v, a[--j])) if(j == lo) break;
100+
if(i >= j) break;
101+
swap(a, i, j);
102+
}
103+
swap(a, lo, j);
104+
return j;
105+
}
106+
107+
public static int M = 5;
108+
public static void quickSort(Comparable[] a, int lo, int hi){
109+
// if(lo >= hi) return;
110+
if(lo + M >= hi) {
111+
insertSort(a);
112+
return;
113+
}
114+
int j = partition(a, lo, hi);
115+
quickSort(a, lo, j-1);
116+
quickSort(a, j+1, hi);
117+
}
118+
119+
public static void quickSort3Way(Comparable[] a, int lo, int hi){
120+
if(lo >= hi) return;
121+
int lt = lo, i = lo + 1, gt = hi;
122+
Comparable v = a[lo];
123+
while(i <= gt){
124+
int cmp = a[i].compareTo(v);
125+
if(cmp < 0) swap(a, lt++, i++);
126+
else if(cmp > 0) swap(a, i, gt--);
127+
else i++;
128+
}
129+
quickSort3Way(a, lo, lt - 1);
130+
quickSort3Way(a, gt + 1, hi);
131+
}
132+
133+
public static void main(String[] args) {
134+
Integer[] a = new Integer[]{18,27,33,55,6,3,23,2,3,5,2,45,1,4,2,5,7,3,7,432,96,7,23,8};
135+
// Integer[] a = new Integer[]{1,3,5,9,2,5,6,7};
136+
// Integer[] a = new Integer[]{2,1,3,4,5,6,7,8,9};
137+
// selectionSort(a);
138+
// insertSort(a);
139+
// ShellSort(a);
140+
// merge(a, 0, 3, 7);
141+
// mergeSortBU(a);
142+
// quickSort(a, 0, a.length -1);
143+
quickSort3Way(a, 0, a.length - 1);
144+
show(a);
145+
}
146+
}

Diff for: DataStructrue/Sort/InsertionSort.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Insert Sort
2+
>算法复杂度仍然是O(n^2)
3+
>内外两次循环,外循环定位,内循环通过比较使外循环定位之前的元素均排序完成。
4+
5+
```Java
6+
public class InsertionSort<T> implements Sort<T> {
7+
@Override
8+
public void sort(Comparable<T>[] a) {
9+
int size = a.length;
10+
for(int i = 0; i < size; i ++){
11+
for(int j = i; j > 0 && Sort.less(a, j, j - 1); j--){ Sort.swap(a, j, j-1); }
12+
}
13+
}
14+
public static void main(String[] args) {
15+
Sort<Integer> sort = new SelectionSort<>();
16+
Integer[] a = {1, 2,1,4,2,6,234,65,23,5,2,657,2,546};
17+
sort.sort(a);
18+
Sort.show(a);
19+
}
20+
}
21+
```
22+
* 结果
23+
657 546 234 65 23 6 5 4 2 2 2 2 1 1

0 commit comments

Comments
 (0)