package com.jzx.quicksort;
public class TestQuckSort {
public static void main(String[] args) {
//输入数组
int[] arr = {67,35,68,79,62,1,45,8,10};
int low = 0;
int high = arr.length-1;
//打印原有数组
for (int i : arr) {
System.out.print(i+"\t");
}
System.out.println();
//排序
quickSort(arr,low,high);
//输出打印数组
for (int i : arr) {
System.out.print(i+"\t");
}
}
private static void quickSort(int[] arr ,int low ,int high) {
//递归条件low<high
if(low<high){
//分区返回索引值
int index = patition( arr , low , high);
//左边递归
quickSort(arr, low , index-1);
//右边递归
quickSort(arr, index+1 , high);
}
}
private static int patition(int[] arr, int low,int high) {
//第一把基准值设置为第一个放入临时变量中
int temp=arr[low];
//填坑1、循环右边的数组把小于基准数的填入左边的坑中;再把左边的数组大于基准数的值填入右边边的坑
while (low<high) {
//填左边的坑
while ( low < high && arr[high] >= temp) {
high--;
}
arr[low] = arr[high];
//a填右边的坑
while (low < high &&arr[low] <= temp ) {
low++;
}
arr[high] =arr[low];
}
//返回當前索引
arr[low] = temp;
return low;
}
}