-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path算法.java
98 lines (97 loc) · 2.35 KB
/
算法.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
public class 算法{
int[] a=new int[]{4,1,3,5,8,7};
// 冒泡排序
void bubbleSort(int[] a){
for(int i=0;i<a.length;i++){
for(int j=1;j<a.length-i;j++){
if(a[j]>a[j-1]){
int temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
}
}
}
}
// 直接插入排序
void insertSort(int[] a){
for(int i=1;i<a.length;i++){
int low=a[i];
int leftIndex=i;
while(leftIndex-1>=0&&a[leftIndex-1]>low){
a[leftIndex]=a[leftIndex-1];
leftIndex--;
}
a[leftIndex]=low;
}
}
// 选择排序
void selectSort(int[] a){
for(int i=0;i<a.length;i++){
int low=i;
for(int j=i+1;j<a.length;j++){
if(a[j]<a[low]){
low=j;
}
}
if(low!=i){
int temp=a[low];
a[low]=a[i];
a[i]=temp;
}
}
}
// 快速排序
void quickSort(int[] a,int low,int high){
if(low>high){
return;
}
int i=low,j=high,k=a[low];
while(i<j){
while(i<j&&a[j]>k){
j--;
}
while(i<j&&a[i]<=k){
i++;
}
if(i<j){
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
a[low]=a[i];
a[i]=k;
quickSort(a, low, i-1);
quickSort(a, i+1, high);
}
// 归并排序
void mergeSort(int[] a,int low,int high,int[] temp){
if(low>=high){
return;
}
int mid=(low+high)/2;
mergeSort(a, low, mid, temp);
mergeSort(a, mid+1, high, temp);
merge(a,low,mid,high,temp);
}
void merge(int[] a,int low,int mid,int high,int[] temp){
int i=0,j=low,k=mid+1;
while(j<=mid&&k<=high){
if(a[j]<=a[k]){
temp[i++]=a[j++];
}
else{
temp[i++]=a[k++];
}
}
while(j<=mid){
temp[i++]=a[j++];
}
while(k<=high){
temp[i++]=a[k++];
}
for(int k=0;k<i;k++){
a[low+k]=temp[k];
}
}
}