java版快速排序

Java非递归方式实现快速排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package  sort.algorithm;
import  java.util.Stack;
//快速排序的非递归实现,利用系统的栈stack
public  class  QuickSortNonRecursion {
      public  static  void  main(String[] args) {
          QuickSortNonRecursion qsnr =  new  QuickSortNonRecursion();
          int [] array = { 0 2 11 121 18 99 3 5 101 22 9 100 };
          qsnr.quicksort(array);
           for  ( int  i : array) {
                 System.out.print(i +  "  " );
           }
         }
                 
         public  void  quicksort( int [] array) {
             if  (array ==  null  || array.length ==  1 return ;
             //存放开始与结束索引
             Stack<Integer> s =  new  Stack<Integer>(); 
             //压栈       
             s.push( 0 ); 
             s.push(array.length -  1 ); 
             //利用循环里实现
             while  (!s.empty()) { 
                 int  right = s.pop(); 
                 int  left = s.pop(); 
                 //如果最大索引小于等于左边索引,说明结束了
                 if  (right <= left)  continue
                         
                 int  i = partition(array, left, right); 
                 if  (left < i -  1 ) {
                     s.push(left);
                     s.push(i -  1 );
                
                 if  (i +  1  < right) {
                     s.push(i+ 1 );
                     s.push(right);
                 }
            
         }
         //找到轴心,进行交换
         public  int  partition ( int [] data,  int  first,  int  end)
         {
             int  temp;
             int  i=first,j=end;
             if (first<end)
             {
                 temp=data[i];
                 //当i=j的时候,则说明扫描完成了
                 while (i<j)
                 {
                     //从右边向左边扫描找到一个小于temp的元素
                     while (j>i&&data[j]>temp)j--;
                     if (i<j)
                     {
                         //将该元素赋值给temp
                         data[i]=data[j];
                         //赋值后就应该将i+1指向下一个序号
                         i++;
                     }
                           
                     //然后从左边向右边开始扫描,找到一个大于temp的元素
                     while (i<j&&temp>data[i])i++;
                     if (i<j)
                     {
                         //将该元素赋值给temp
                         data[j]=data[i];
                         //赋值后就应该将j-1指向前一个序号
                         j--;
                     }
                 }
                 //将轴数据放在i位置中
                 data[i]=temp;
             }
             return  i;
         }
     }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值