Doing something you intrinsically are enthusiastic with.

2016年10月14日 星期五

Quicksort-instant reveiw

晚上8:36 Posted by Unknown No comments
You may checked this video to see how quicksort is working.

There are many similar forms of quicksort.

You can randomly choose one pivot or two pivot to go while the code below only

deals with the single pivot case. Like the video, we burn the the candle from one side.






1. Candle burned from one side


  1. void quick_sort(int array[], int leftint right)
  2. {
  3.     if (left < right)
  4.     {
  5.         // divide (partition)
  6.         int pivot = array[(left+right)/2];  
  7.       // Can choose any number you want
  8.         swap(array[right], array[(left+right)/2]);
  9.       // to the rightmost
  10.         int i = leftj = left;
  11.         for (; j < right; ++j)
  12.             if (array[j] <= pivot)
  13.             {
  14.                 if (i < jswap(array[i], array[j]);    
  15.                 i = i + 1;
  16.             }
  17.         if (i < rightswap(array[i], array[right]);   
  18.  
  19.         // then conquer
  20.         quick_sort(arraylefti-1);
  21.         quick_sort(arrayi+1right);
  22.  
  23.         // no need to combine sub-solutions
  24.     }
  25. }

0 意見:

張貼留言