一、问题描述
一维整型数组快速排序(正序)算法
二、源码
package bag;
import java.util.Scanner;
/**
* @author 18101960
*
*/
public class Class {// 划分,返回划分基准点
static int partition(int a[], int start, int end) {
int i = start, j = end;
int tmp = a[start];
while (i != j) {
while (i < j && a[j] >= tmp) {// 从右到左扫描。直到出口
j--;
a[i] = a[j];// 空出基准点的位置用于放适合的数字
}
while (i < j && a[j] <= tmp) {// 从坐到右,直到出口
i++;
a[j] = a[i];
}
a[i] = tmp;
return i;
}
return 0;
}
static void quickSort(int a[], int start, int end) {
if (end - start <= 0) {// 只有一个或者没有元素
return;
} else {
int i = partition(a, start, end); // 划分函数
quickSort(a, start, i - 1);// 左边
quickSort(a, i + 1, end);// 右边
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a[] = new int[10];
System.out.print("输入你想要快排的一组10个整数");
for (int i = 0; i < 10; i++) {
a[i] = sc.nextInt();
}
System.out.print("下面输出快排的结果");
int start = 0;
int end = a.length - 1;
quickSort(a, start, end);
for (int j = 0; j < 10; j++) {
System.out.print(a[j]);
}
}
}// 一维整型数组快速排序(正序)算法