public class MainApplication {
public static void main(String[] args) {
// SpringApplication.run(MainApplication.class,args);
// 冒泡排序的思想是相邻交换
Integer[] a={1,7,8,10,2,85,2,8,16,25};
// Integer[] a={1,7,2,8};
// bubbleSort(a);
// for (int i:a) {
// System.out.println( i);
// }
mergeSort(a,0,a.length-1);
// merge(a,0,3,7);
for (int i:a) {
System.out.println( i);
}
}
public static void bubbleSort(Integer[] list){
for (int i = 0; i < list.length-1; i++) {
boolean flag=false;
for (int j = 0; j < list.length-1-i; j++) {
if(list[j]>list[j+1]){
int temp=list[j];
list[j]=list[j+1];
list[j+1]=temp;
flag=true;
}
}
if (flag == false) {
return;
}
}
}
public static void mergeSort(Integer[] list,int low,int high){
if(low<high){
int mid=(low+high)/2;
mergeSort(list,low,mid);
mergeSort(list,mid+1,high);
merge(list,low,mid,high);
}
}
public static void merge(Integer[] list,int start,int middle,int end){
Integer[] temp=new Integer[list.length];
int i=start;
int j=middle+1;
int n=0;
while (i<=middle && j <=end){
if(list[i]<=list[j]){
temp[n++]=list[i++];
}else{
temp[n++]=list[j++];
}
}
while (i<=middle){
temp[n++]=list[i++];
}
while (j <=end ){
temp[n++]=list[j++];
}
for (int k = 0; k < n; k++) {
list[start+k]=temp[k];
}
}
}
冒泡排序和归并排序java版本
于 2022-04-11 22:13:38 首次发布