两个sort方法分别是冒泡和快排的两个算法
import java.util.Arrays;
public class algorithm {public static void main(String[] args){
int []array3 = {6,2,9,14,10,1}; //kuaisupaixu
int []array1 = new int[]{20,40,10,5,9,81}; //maopaopaixu
sort1(array1);
quickSort(array3);
// for(int k=0;k<array3.length;k++){
//System.out.println(array3[k]);
//}
System.out.print(Arrays.toString(array3));
}
public static void sort1(int []array1){
for(int i=0;i<array1.length;i++){
for(int j=0;j<array1.length-i-1;j++){
if(array1[j]>array1[j+1]){
int temp = array1[j];
array1[j] = array1[j+1];
array1[j+1] = temp;
}
}
}
System.out.println("After sort1");
for(int k = 0;k<array1.length;k++){
System.out.print(array1[k]+"\t");
}
}
public static void quickSort(int []array3){
sort3(array3,0,array3.length-1);
}
public static void sort3(int []array3,int start,int end){
int base = array3[start];
int i = start; int j = end;
if(start < end){
do{
while(array3[i]<base&&i<end) i++;
while(array3[j]>base&&j>start) j--;
if(i<=j){
int temp = array3[i];
array3[i] = array3[j];
array3[j] = temp;
i++; j--;
}
}
while(i<=j);
if(start<j){
sort3(array3,start,i-1);
}
if(end>i){
sort3(array3,i+1,end);
}
}
}
}