mramine364

quicksort

May 14th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function quickSort(t){ 
  2.     _quickSort(t,0,t.length-1,0,t.length-1);   
  3. }
  4.  
  5. function _quickSort(t, s, e, sp, ep){
  6.     if( s>=e ){
  7.         return;
  8.     }
  9.     while( sp<ep && t[sp]<t[e] ){
  10.         sp++;
  11.     }
  12.     if( sp==e ){
  13.         _quickSort(t,s,e-1,s,e-1);
  14.     }
  15.     else{
  16.         while(t[ep]>=t[e] && sp<ep ){
  17.             ep--;
  18.         }
  19.         if( sp==ep ){
  20.             var temp = t[sp];
  21.             t[sp] = t[e];
  22.             t[e] = temp;
  23.             if( s!=sp ){
  24.                 _quickSort(t,s,sp-1,s,sp-1);
  25.             }
  26.             _quickSort(t,sp+1,e,sp+1,e);           
  27.         }else{
  28.             var temp = t[sp];
  29.             t[sp] = t[ep];
  30.             t[ep] = temp;
  31.             _quickSort(t,s,e,sp+1,ep);
  32.         }
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment