用JAVA实现快速排序与选择排序
快速排序
public class quicksort {
public static void quickSort(int[] array,int small,int big) {
int i,j,temp,middle;
if(small > big){
return;
}
i = small;
j = big;
middle = array[small];
while(i < j){
while(i < j && array[j] >= middle){
j--;
}
while(i < j && array[i] <= middle){
i++;
}
if (i < j){
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
array[small] = array[i];
array[i] = middle;
quickSort(array, small, j-1);
quickSort(array, j+1, big);
}
public static void main(String[] args) {
int[] arr = {9,7,2,4,6,8,10};
quickSort(arr, 0, arr.length-1);
for(int i = 0; i < arr.length; i++){
System.out.println(arr[i]);
}
}
}
选择排序
public class selectsort {
public static void selectSort(int[] arr){
int up;
int down;
int temp = 0;
for(int i=0; i<arr.length-1; i++){
up = i;
down = i+1;
for(; down<arr.length; down++){
if(arr[up] > arr[down])
up = down;
}
temp = arr[i];
arr[i] = arr[up];
arr[up] = temp;
}
}
public static void main(String[] args) {
int[] array = {8,3,6,7,1,0,2};
selectSort(array);
for(int i : array){
System.out.println(i);
}
}
}