快速排序的java版

  1. package quicksort;
  2. public class Swap{
  3.     private int x;
  4.     private int y;
  5.     public Swap(int x,int y){
  6.     this.x=x;
  7.     this.y=y;
  8.     }
  9.     
  10.     public void doSwap(){
  11.     int temp=x;
  12.     x=y;
  13.     y=temp;
  14.     }
  15.     public int getx(){return x;}
  16.     public int gety(){return y;}
  17. }
  1. package quicksort;
  2. public class QuickSort{
  3.     private int[] array;
  4.     private int left;
  5.     private int right;
  6.     
  7.     public QuickSort(int[] array,int left,int right){
  8.         this.array=array;
  9.         this.left=left;
  10.         this.right=right;
  11.         this.doQuickSort(array,left,right);
  12.     }
  13.     
  14.     private int partition(int[] array,int left,int right){
  15.         int i=left;
  16.         int j=right+1;
  17.         int point=array[left];
  18.         while(true){
  19.             while(array[++i]<point&&i<right);
  20.             while(array[--j]>point);
  21.             if(i>=j)break;
  22.             Swap temp=new Swap(array[i],array[j]);
  23.             temp.doSwap();
  24.             array[i]=temp.getx();
  25.             array[j]=temp.gety();
  26.         }
  27.         array[left]=array[j];
  28.         array[j]=point;
  29.         return j;//balance point
  30.     }
  31.     
  32.     private void doQuickSort(int[] array,int left,int right){
  33.         if(left<right){
  34.             int point=partition(array,left,right);
  35.             doQuickSort(array,left,point-1);
  36.             doQuickSort(array,point+1,right);
  37.         }
  38.     }
  39.     
  40.     public static void main(String[] args){
  41.         int[] array={2,4,5,6,76,3,312,44,21,332,3};//排序数组
  42.         QuickSort test=new QuickSort(array,0,9);//QuickSort(数组,数组起始下标,数组末尾下标)
  43.         
  44.         for(int i=0;i<=9;i++){
  45.             System.out.println(array[i]);
  46.         }
  47.     }
  48. }



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值