- package com.itheima;
- public class Test20 {
- public static void main(String[] args) {
- QuickSort qs=new QuickSort();
- int arr1[]={44,22,2,32,54,22,88,77,99,11};
- qs.arr1=arr1;
- qs.sort(0,qs.arr1.length-1);
- qs.printArray();
- }
- }
- class QuickSort(){
- public int arr1[];
- private int partition(int[] arr,int low,int high){
- int key=arr[low];
- while(low<high){
- while(low<high && arr[high]>=key)
- high--;
- arr[low]=arr[high];
- while(low<high && arr[low]<=key)
- low++;
- arr[high]=arr[low];
- }
- arr[low]=key;
- return low;
- }
- public void sort(int low,int high){
- if(lwo<high){
- int result=partition(arr1,low,high)
- sort(low,result-1);
- sort(result+1,high);
- }
- public void printArray(){
- for(int i=0;i<=arr1.length-1;i++)
- System.out.print(arr[i]+' ');
- }
- }
复制代码 |