Sorting: quick sorting

Source: Internet
Author: User
1   Import  Java. util. arrays;  2   Import  Java. util. Stack;  3   4   5   Public   Class  Quicksort {  6       7       8       Public   Static  Void  Main (string [] ARGs ){  9           Int [] Arr = {72, 6, 57, 88, 60, 42, 83, 73, 48, 85 };  10   //  Quicksort (ARR, 0, arr. Length-1 );  11   Quicksort (ARR );  12   System. Out. println (arrays. tostring (ARR ));  13   }  14      15       /**  16   * Fast sorting by recursion  17   *  @ Param  S  18   *  @ Param  L  19   *  @ Param  R  20       */  21       Private   Static   Void Quicksort ( Int [] S, Int L, Int  R ){  22           If (L> = R ){  23               Return  ;  24  }  25           26           Int I = L;  27           Int J = R;  28           Int X = S [l];  29           30           While (I < J ){  31              While (I <J & x <= S [J]) {  32 J -- ;  33   }  34               If (I < J ){  35 S [I ++] = S [J];  36   }  37               38              While (I <J & x> S [I]) {  39 I ++ ;  40   }  41               If (I < J ){  42 S [j --] = S [I];  43   }  44   } 45           46 S [I] = X;  47 Quicksort (S, L, I-1 );  48 Quicksort (S, I + 1 , R );  49   }  50       51       /**  52   * Fast sorting in non-recursive Mode  53  *  @ Param  S  54        */  55       Private   Static   Void Quicksort ( Int  [] S ){  56 Stack <Note> stack = New Stack <Note> ();  57           58          Int L = 0 ;  59           Int R = S. Length-1 ;  60           61           Int  I;  62           Int  J;  63           Int  X;  64           65 Stack. Push ( New  Note (L, R ));  66           67           While (! Stack. Empty ()){  68 L = Stack. Peek (). L;  69 R = Stack. Peek (). R;  70               71 I = L;  72 J =R;  73               74   Stack. Pop ();  75               76               If (L> = R ){  77                   Continue  ;  78   }  79               80 X = S [I]; 81               82               83               While (I < J ){  84                   While (I <J & x <= S [J]) {  85 J -- ;  86   }  87                   If (I < J ){ 88 S [I ++] = S [J];  89   }  90                   91                   While (I <J & x> S [I]) {  92 I ++ ;  93   }  94                   If (I < J ){ 95 S [j --] = S [I];  96   }  97   }  98               99 S [I] = X;  100               101 Stack. Push ( New Note (L, I-1 ));  102 Stack. Push (New Note (I + 1 , R ));  103   }  104           105   }  106       107       Private   Static   Class  Note {  108           Public   Int  L; 109           Public   Int  R;  110           Public Note ( Int L, Int  R ){  111               Super  ();  112               This . L = L;  113               This . R = R;  114   }  115   }  116       117 }
 

I sorted it out myself, but I won't talk about the details. Refer to the blog:Http://www.cnblogs.com/morewindows/archive/2011/08/13/2137415.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.