<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Ytl's Java Blog

    厚積而薄發---每一天都是一個全新的開始
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    Alogrithms to quicksort

    Posted on 2011-05-08 14:13 ytl 閱讀(345) 評論(0)  編輯  收藏

    Quicksort

    Quicksort is a fast sorting algorithm, which is used not only for educational purposes, but widely applied in practice. On the average, it has O(n log n) complexity, making quicksort suitable for sorting big data volumes. The idea of the algorithm is quite simple and once you realize it, you can write quicksort as fast as bubble sort.

    Algorithm

    The divide-and-conquer strategy is used in quicksort. Below the recursion step is described:
    1. Choose a pivot value. We take the value of the middle element as pivot value, but it can be any value, which is in range of sorted values, even if it doesn't present in the array.
    2. Partition. Rearrange elements in such a way, that all elements which are lesser than the pivot go to the left part of the array and all elements greater than the pivot, go to the right part of the array. Values equal to the pivot can stay in any part of the array. Notice, that array may be divided in non-equal parts.
    3. Sort both parts. Apply quicksort algorithm recursively to the left and the right parts.

    Partition algorithm in detail

    There are two indices i and j and at the very beginning of the partition algorithm i points to the first element in the array andj points to the last one. Then algorithm moves i forward, until an element with value greater or equal to the pivot is found. Index j is moved backward, until an element with value lesser or equal to the pivot is found. If i ≤ j then they are swapped and i steps to the next position (i + 1), j steps to the previous one (j - 1). Algorithm stops, when i becomes greater than j.

    After partition, all values before i-th element are less or equal than the pivot and all values after j-th element are greater or equal to the pivot.

    Example. Sort {1, 12, 5, 26, 7, 14, 3, 7, 2} using quicksort.

    Quicksort example

    Notice, that we show here only the first recursion step, in order not to make example too long. But, in fact, {1, 2, 5, 7, 3} and {14, 7, 26, 12} are sorted then recursively.

    Why does it work?

    On the partition step algorithm divides the array into two parts and every element a from the left part is less or equal than every element b from the right part. Also a and b satisfy a ≤ pivot ≤ b inequality. After completion of the recursion calls both of the parts become sorted and, taking into account arguments stated above, the whole array is sorted.

    Complexity analysis

    On the average quicksort has O(n log n) complexity, but strong proof of this fact is not trivial and not presented here. Still, you can find the proof in [1]. In worst case, quicksort runs O(n2) time, but on the most "practical" data it works just fine and outperforms other O(n log n) sorting algorithms.

    Code snippets

    Java

    int partition(int arr[], int left, int right)

    {

         int i = left;

         int j = right;

         int temp;

        int  pivot = arr[(left+right)>>1];

         while(i<=j){

            while(arr[i]>=pivot){

                i++;

            }

            while(arr[j]<=pivot){

                j--;

           }

           if(i<=j){

               temp = arr[i];

               arr[i] = arr[j];

               arr[j] = temp;

               i++;

               j--;

           }

        }

        return i

    }

     

    void quickSort(int arr[], int left, int right) {

          int index = partition(arr, left, right);

          if(left<index-1){

             quickSort(arr,left,index-1);

          }

          if(index<right){

             quickSort(arr,index,right); 

          }

    }

    python

    def quickSort(L,left,right) {

          i = left

          j = right

          if right-left <=1:

                return L

          pivot = L[(left + right) >>1];

          /* partition */

          while (i <= j) {

                while (L[i] < pivot)

                      i++;

                while (L[j] > pivot)

                      j--;

                if (i <= j) {

                      L[i],L[j] = L[j],L[i]

                      i++;

                      j--;

                }

          };

          /* recursion */

          if (left < j)

                quickSort(Lleftj);

          if (i < right)

                quickSort(Liright);

    }

    主站蜘蛛池模板: 亚洲成亚洲乱码一二三四区软件| 免费高清资源黄网站在线观看| 中文字幕一精品亚洲无线一区| 精品无码专区亚洲| 免费观看的a级毛片的网站| 日韩亚洲国产高清免费视频| 在线观看免费人成视频色9| 国产成人精品日本亚洲直接 | 中文字幕免费观看全部电影| 又爽又高潮的BB视频免费看| 精品一区二区三区免费毛片| 亚洲精品动漫人成3d在线| 一区在线免费观看| 亚洲大尺度无码专区尤物| 99久久免费看国产精品| 亚洲人成在线精品| 国产精品成人无码免费| 一区二区三区免费精品视频 | 亚洲色成人网站WWW永久四虎| 四虎影院免费在线播放| 特级一级毛片免费看| 亚洲日韩精品无码专区网址| 四虎国产成人永久精品免费 | 国产亚洲综合视频| 久久精品夜色噜噜亚洲A∨| 久久免费高清视频| 久久精品国产亚洲αv忘忧草| 精品免费国产一区二区| 久久毛片免费看一区二区三区| 亚洲AV乱码久久精品蜜桃| 成人免费午夜无码视频| 男人和女人高潮免费网站| 亚洲国产精品成人久久| 毛片a级毛片免费观看免下载 | 四虎成人精品国产永久免费无码| 亚洲日韩av无码| 成人毛片18岁女人毛片免费看| 亚洲免费视频一区二区三区| 亚洲成人网在线观看| 亚洲精品动漫人成3d在线| 久草视频免费在线|