常用排序算法之快速排序

        快速排序,采用分治策略,将数组A[n]分组进行递归快速排序。
        步骤:
  1. 以A[0]为基准元素,将数组中比其小的元素放在其左边,比其大的放在其右边;
  2. 将基准元素左右的数组分别再进行步骤1(即递归)。
        代码实现:
public class QuickSort {


/**
* <p>MethodName:main </p>
* <p>Description: </p>
* <p>@param args</p> 
* <p>Return:void </p> 
* @author Sunny
* @date 2016-9-26下午7:36:43 
*/
public static void main(String[] args) {
int[] arr={7,6,4,9,0,5,2,8,3,1};
   quickSort(arr,0,9);
}

public static void quickSort(int[] arr,int low,int high){
if(low>=high) return;
int l=low,h=high,t=arr[low];
while(l<h){
while(arr[l]<t) l++;
while(arr[h]>t) h--; 
if(l<h){
ArrayUtils.exchangeElements(arr, l, h);
ArrayUtils.printArray(arr);     
}
System.out.println("左指针指向:"+arr[l]+" 右指针指向:"+arr[h]);
}
quickSort(arr,low,h-1);
quickSort(arr,l+1,high);
}


}


     结果展示:

1 6 4 9 0 5 2 8 3
左指针指向:1 右指针指向:7

1 6 4 7 0 5 2 8 3 9 
左指针指向:7 右指针指向:9

1 6 4 3 0 5 2 8 7
左指针指向:3 右指针指向:7

1 6 4 3 0 5 2 7 8
左指针指向:7 右指针指向:8

左指针指向:7 右指针指向:7

0 6 4 3 1 5 2 7 8 9 
左指针指向:0 右指针指向:1

0 1 4 3 6 5 2 7 8 9 
左指针指向:1 右指针指向:6

左指针指向:1 右指针指向:1

0 1 2 3 6 5 4 7 8 9 
左指针指向:2 右指针指向:4

0 1 2 3 4 5 6 7 8 9 
左指针指向:4 右指针指向:6

左指针指向:4 右指针指向:4

左指针指向:2 右指针指向:2

左指针指向:5 右指针指向:5

左指针指向:8 右指针指向:8
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值