数据结构与算法中的排序
快速排序
package wzf.sorting;
import java.util.Scanner;
//快速排序
public class Kuai {
public static void quickSort(int[] arr,int low,int high){
int i,j,temp,t;
if(low>high){
return;
}
i=low;
j=high;
temp = arr[low];
while (i<j) {
while (temp<=arr[j]&&i<j) {
j--;
}
while (temp>=arr[i]&&i<j) {
i++;
}
if (i<j) {
t = arr[j];
arr[j] = arr[i];
arr[i] = t;
}
}
arr[low] = arr[i];
arr[i] = temp;
quickSort(arr, low, j-1);
quickSort(arr, j+1, high);
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要快速排序的个数:");
int M= sc.nextInt();
int []arr= new int[M];
System.out.print("空格或者换行隔开:");
for(int i =0;i<M;i++) {
arr[i]=sc.nextInt();
}
System.out.println("排序后为:");
quickSort(arr, 0, M-1);
for (int i = 0; i < M; i++) {
System.out.print(arr[i]+" ");
}
}
}
运行截图
代码注解较少,有时间添加。请谅解!