- import java.util.Arrays;
- public class QuickSort{
- private static int partition(int data[],int low,int high){//分治
- int key = data[low];
- while(low<high){
- while(low<high && data[high]>key)//从右向左
- high--;
- data[low] = data[high];
- while(low<high && data[low]<key)//从左向右
- low++;
- data[high] = data[low];
- }
- data[low] = key;//把轴元素放在轴所在地位置
- return low;//返回轴所在的位置
- }
- private static void quickSort(int data[],int low,int high){//递归
- int q;
- if(low<high){
- q = partition(data,low,high);
- quickSort(data,q+1,high);
- quickSort(data,low,q-1);
- }
- }
- public static void main(String args[]){
- int a[] = new int[]{1,4,6,2,3,5,7};
- quickSort(a,0,a.length -1);
- System.out.println(Arrays.toString(a));
- }
快速排序 : 分治和递归思想
最新推荐文章于 2022-11-29 20:52:18 发布