`
leonzhx
  • 浏览: 786181 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Quick Sort

阅读更多

1.  Basic plan:
  a)  Shuffle the array.
  b)  Partition so that, for some j
        – entry a[j] is in place
        – no larger entry to the left of j
        – no smaller entry to the right of j
  c)  Sort each piece recursively.

 

2.  Partitioning:

  Repeat following steps until i and j pointers cross:

  a)  Scan i from left to right so long as (a[i] < a[lo]).
  b)  Scan j from right to left so long as (a[j] > a[lo]).
  c)  Exchange a[i] with a[j].

 

  When pointers cross.
  a)  Exchange a[lo] with a[j].

 

 

private static int partition(Comparable[] a, int lo, int hi)
{
    int i = lo, j = hi+1;
    while (true)
    {
      while (less(a[++i], a[lo]))
        if (i == hi) break;
      while (less(a[lo], a[--j]))
        if (j == lo) break; // this test is redundant
      if (i >= j) break;
      exch(a, i, j);
    }
    exch(a, lo, j);
    return j;
}

 

 

 

3.  Quick Sort Implementation :

 

public class Quick
{
  private static int partition(Comparable[] a, int lo, int hi)
  { /* see previous slide */ }
  public static void sort(Comparable[] a)
  {
    StdRandom.shuffle(a);
    sort(a, 0, a.length - 1);
  }
  private static void sort(Comparable[] a, int lo, int hi)
  {
    if (hi <= lo) return;
    int j = partition(a, lo, hi);
    sort(a, lo, j-1);
    sort(a, j+1, hi);
  }
}

 

 

4.  Best case: each time the partition element is in the middle of the array to be sorted. Number of compares is ~ N lg N.

 

5.  Worst case: the array to be sorted is already in order ( or in reverse order). Number of compares is ~ ½ N^2 .

 

6.  Proposition: The average number of compares to quicksort an array of N distinct keys is ~ 2N ln N (1.39 N lg N ) and the number of exchanges is ~ ⅓ N ln N. ( 39% more compares than mergesort. But faster than mergesort in practice because of less data movement.)

 

7.  Quick Sort is in-place sorting algorithm and not stable.

 

8.  Improvements:

  a)  Insertion sort small subarrays.
     --Even quicksort has too much overhead for tiny subarrays.
     --Cutoff to insertion sort for ≈ 10 items.
     --Note: could delay insertion sort until one pass at end.

private static void sort(Comparable[] a, int lo, int hi)
{
  if (hi <= lo + CUTOFF - 1)
  {
    Insertion.sort(a, lo, hi);
    return;
  }
  int j = partition(a, lo, hi);
  sort(a, lo, j-1);
  sort(a, j+1, hi);
}

  b)  Median of sample.
     --Best choice of pivot item = median.
     --Estimate true median by taking median of sample.

     --Median-of-3 (random) items. 

private static void sort(Comparable[] a, int lo, int hi)
{
  if (hi <= lo) return;
  int m = medianOf3(a, lo, lo + (hi - lo)/2, hi);
  swap(a, lo, m);
  int j = partition(a, lo, hi);
  sort(a, lo, j-1);
  sort(a, j+1, hi);
}

 

9.  The goal of selection problem : Given an array of N items, find the kth largest. Ex. Min (k = 0), max (k = N - 1), median (k = N / 2).

 

10.  Quick Select :

  a)  Partition array so that:

     --Entry a[j] is in place.
     --No larger entry to the left of j.
     --No smaller entry to the right of j.
  b)  Repeat in one subarray, depending on j; finished when j equals k.

public static Comparable select(Comparable[] a, int k)
{
  StdRandom.shuffle(a);
  int lo = 0, hi = a.length - 1;
  while (hi > lo)
  {
    int j = partition(a, lo, hi);
    if (j < k) lo = j + 1;
    else if (j > k) hi = j - 1;
    else return a[k];
  }
  return a[k];
}

 

 

11.  Proposition: Quick-select takes linear time on average.

 

12.  Quick-select uses ~ ½ N^2 compares in the worst case. but the random shuffle provides a probabilistic guarantee.

 

13.  If the array to be sorted contains large amount of duplicate keys:

    MergeSort :  Between ½ N lg N and N lg N compares.

    Quicksort:  Algorithm goes quadratic unless partitioning stops on equal keys

 

14.  3-way partitioning:  Goal--Partition array into 3 parts so that:
  a)  Entries between lt and gt equal to partition item v.
  b)  No larger entries to left of lt.
  c)  No smaller entries to right of gt.



 

15.  Algorithm of 3-way partitioning :

  a)  Let v be partitioning item a[lo].
  b)  Scan i from left to right.
    – (a[i] < v): exchange a[lt] with a[i]; increment both lt and i
    – (a[i] > v): exchange a[gt] with a[i]; decrement gt
    – (a[i] == v): increment i

 

private static void sort(Comparable[] a, int lo, int hi)
{
  if (hi <= lo) return;
  int lt = lo, gt = hi;
  Comparable v = a[lo];
  int i = lo;
  while (i <= gt)
  {
    int cmp = a[i].compareTo(v);
    if (cmp < 0) exch(a, lt++, i++);
    else if (cmp > 0) exch(a, i, gt--);
    else i++;
  }
  sort(a, lo, lt - 1);
  sort(a, gt + 1, hi);
}

 

 

16.  Randomized quicksort with 3-way partitioning reduces running time from linearithmic to linear in broad class of applications.

 

17.  The reason why Java adopt tuned quick sort for primitives and tuned merge sort for objects is that it's assumed that if primitives are used , it means the user cares much about spaces.

 

18.  Tukey's ninther. Median of the median of 3 samples, each of 3 entries.
  a)  Approximates the median of 9.

  b)  Uses at most 12 compares.

  Better partitioning than random shuffle and less costly.



 

18.  Sorting Summary:



 

  • 大小: 10.5 KB
  • 大小: 8.1 KB
  • 大小: 8 KB
  • 大小: 8.2 KB
  • 大小: 15.8 KB
  • 大小: 62 KB
分享到:
评论

相关推荐

    quick sort

    Use QuickSort algorithm to sort the array that has n elements that are constructed by the random() function. Requirements: The template should be used for all kinds of data type, such as: integer, ...

    sort_exp.rar_quick sort_桶排序

    快速排序(Quick Sort)是由C.A.R. Hoare在1960年提出的,它是一种非常高效的排序算法,其基本思想是分治法。快速排序的基本步骤如下: 1. **选择枢轴元素**:在待排序的数组中选取一个元素作为枢轴,通常选择第一...

    Quick Sort in the worst case

    快速排序(Quick Sort)是一种高效的排序算法,由C.A.R. Hoare在1960年提出。其基本思想是采用分治法(Divide and Conquer),通过一趟排序将待排记录分割成独立的两部分,其中一部分的所有记录都比另一部分的所有...

    Quick sort Analysis.zip

    在这个名为"Quick sort Analysis.zip"的压缩包中,重点是分析快速排序的确定性与随机化实现。确定性快速排序通常是指每次选取固定的基准元素,如选择第一个或最后一个元素,这样对于相同的输入,排序过程完全可预测...

    PHP排序算法之快速排序(Quick Sort)及其优化算法详解

    快速排序(Quick Sort)是一种高效的排序算法,由C. A. R. Hoare在1960年提出。它采用分治法(Divide and Conquer)的策略来把一个序列分为较小和较大的两个子序列,然后递归地排序两个子序列。快速排序在实现时,...

    快速排序C实现 quick sort

    数据结构,排序算法,快速排序算法的C语言实现, quick sort C qsort.c an c implementation of quick sort

    quick sort算法图解

    快速排序算法(Quick Sort)是一种高效的排序算法,由计算机科学家托尼·霍尔(Tony Hoare)在1960年提出。它的基本思想是分治法(Divide and Conquer),通过一个基准值(pivot)将数组分为两部分,其中一部分的...

    python编写 快速排序 Quick Sort

    python编写 快速排序 Quick Sort

    C#,单向链表(Simply Linked List)快速排序(Quick Sort)算法与源代码

    各种数据结构、算法及实用的C#源代码.C#,单向链表(Simply Linked List)快速排序(Quick Sort)算法与源代码.单向链表(单链表)是链表的一种,其特点是链表的链接方向是单向的,对链表的访问要通过顺序读取从头部...

    C#,双向链表(Doubly Linked List)快速排序(Quick Sort)算法与源代码

    C#,双向链表(Doubly Linked List)快速排序(Quick Sort)算法与源代码。双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始...

    以 Quick Sort 算法将 ListBox 内容排序的范例(3k)

    快速排序(Quick Sort)是一种高效的排序算法,由C.A.R. Hoare在1960年提出。它的基本思想是采用分治法(Divide and Conquer),将一个大问题分解为两个或多个相同或相似的子问题,直到最后子问题可以简单的直接求解...

    python 一行代码实现的快速排序 quick sort

    python 一行代码实现的快速排序 quick sort

    快速排序(Quick Sort)

    ### 快速排序(Quick Sort) #### 算法原理 快速排序是一种高效的排序算法,其基本思想是采用分治法(divide and conquer)来解决问题。对于待排序的数组A[0]...A[N-1],快速排序通过选择一个基准元素(pivot),通常...

    快速排序(Quick Sort)源码及运行示例

    快速排序(Quick Sort)源码及运行示例

    快速排序(Quick Sort)作者原版论文PDF

    快速排序(Quick Sort)作者原版论文,快速排序的作者C.A.R Hoare 发表的原著论文。

    算法分析与设计教学课件:Chapter 7 Quick Sort.pptx

    算法分析与设计教学课件:Chapter 7 Quick Sort.pptx

    Quick Sort (C++)

    快速排序是一种高效的排序算法,由英国计算机科学家C.A.R. Hoare在1960年提出。它的基本思想是分治法(Divide and Conquer)。在这个C++实现的快速排序中,我们将深入理解其原理、步骤以及如何用C++语言进行编码。...

    slides for merge and quick sort

    private static void sort(Comparable[] a, Comparable[] aux, int l, int r) { if (r ) return; int m = l + (r - l) / 2; sort(a, aux, l, m); // 排序左半部分 sort(a, aux, m, r); // 排序右半部分 ...

    基于python的排序算法-快速排序Quick Sort

    return quick_sort(left) + middle + quick_sort(right) # 示例 arr = [3, 6, 8, 10, 1, 2, 1] print(quick_sort(arr)) # 输出: [1, 1, 2, 3, 6, 8, 10] ``` 6. **时间复杂度与空间复杂度**: - **时间复杂度**...

Global site tag (gtag.js) - Google Analytics