QuickSort快速排序(罗召勇版)

 1 package quicksort;
 2 
 3 import java.util.Arrays;
 4 
 5 public class QuickSort {
 6     public static void main(String[] args) {
 7         int[] arr = new int[]{11,9,6,22,36,21,17,5,4,99,3};
 8         quickSort(arr,0,arr.length-1);
 9         System.out.println(Arrays.toString(arr));
10     }
11 
12     public static void quickSort(int[] arr,int start,int end){
13         if(start<end){
14             //将数组中的第一个元素作为参照
15             int standard = arr[start];
16             //记录元素下标
17             int low = start;
18             int high = end;
19             //循环找出比参照数小的数和大的数
20             while (low<high){
21                 //右边的数字比参照数大
22                 while (low<high&&arr[high]>=standard){
23                     high--;
24                 }
25                 //否则,右边的数直接移到左边
26                 arr[low]=arr[high];
27                 //左边的数比参照数小
28                 while (low<high&&arr[low]<=standard){
29                     low++;
30                 }
31                 //否则,左边的数直接到右边
32                 arr[high]=arr[low];
33             }
34             arr[low]=standard;
35             quickSort(arr,start,low);
36             quickSort(arr,low+1,end);
37         }
38     }
39 }

 

posted @ 2021-02-28 00:37  TRAODM  阅读(52)  评论(0)    收藏  举报