WechatIMG355.jpeg
1.冒泡算法 :大數(shù)像氣泡一樣往上冒胸懈。
public static void bubbleSort(int[] numbers){
int temp=0;
int size=numbers.length;
for(int i=0;i<size-1;i++){
for(int j=0;j<size-1-i;j++){
if(numbers[j]>numbers[j+1]){
temp=numbers[j];
numbers[j]=numbers[j+1];
numbers[j+1]=temp;
}
}
}
}
2.快速排序:找到中間值,分成左右兩組恰响,左邊的數(shù)值均小于右邊的數(shù)值趣钱。然后遞歸處理。直到排序為有序數(shù)據(jù)胚宦。簡單高效首有。
查找中軸(最低位作為中軸)所在位置:
public static int getMiddle(int[] numbers,int low,int high){
int temp=numbers[low];
while(low<high){
while(low<high&&numbers[high]>=temp){
high--;
}
numbers[low]=numbers[high];
while(low<high&&numbers[low]<temp){
low++;
}
numbers[high]=numbers[low];
}
numbers[low]=temp;
return low;
}
遞歸實現(xiàn)
public static void quickSort(int[] numbers,int low,int high){
if(low<high){
int middle=getMiddle(numbers,low,high);
quickSort(numbers,low,middle-1);
quickSort(numbers,middle+1,high);
}
}
3.選擇排序:在無序的數(shù)組中選擇最小的數(shù)字與第一位交換,然后在剩下的數(shù)組再選擇最小的數(shù)組與第二位交換枢劝。依次推進(jìn)井联,直到有序。
public static void selectSort(int[] numbers) {
int size = numbers.length;
int temp;
for(int i=0;i<size-1;i++){
int k=i;
for(int j=size-1;j>i;j--){
if(numbers[j]<numbers[k]){
k=j;
}
}
temp=numbers[i];
numbers[i]=numbers[k];
numbers[k]=temp;
}
}