/**
* @描述:快速排序
*/
public class Test {
public static void main(String[] args) {
int[] array = new int[10];
//随机生成一个一维数组
for (int i = 0; i < array.length; i++) {
array[i] = (int) (Math.random() * 10);
}
quickSort(array, 0, array.length - 1);
for (int i : array) {
System.out.print(array[i] + " ");
}
}
/**
* 描述:快速排序
*/
public static void quickSort(int[] array,int low,int high) {
if (low < high) {
int i = low;
int j = high;
int x = array[i]; // 基准
while (i < j && array[j] > x)
j--;
if (i < j) {
array[i] = array[j];
i++;
}
// 对称
while (i < j && array[i] < x)
i++;
if (i < j) {
array[j] = array[i];
j--;
}
array[i] = x;
quickSort(array, low, i - 1);
quickSort(array, i + 1, high);
}
}
}
快速排序
最新推荐文章于 2023-12-01 23:14:38 发布