« 返回首页

图解排序算法

刷新 问题规模: 20 · 30 · 40 · 50     放大: 1x · 2x · 3x
初始条件:  随机排列 · 接近排序完的 · 反向排列 · 很少有不同的

讨论

Sorting an array that consists of a small number of unique keys is common in practice. One would like an algorithm that adapts to O(n) time when the number of unique keys is O(1). In this example, there are 4 unique keys.

The traditional 2-way partitioning quicksort exhibits its worse-case O(n2) behavior here. For this reason, any quicksort implementation should use 3-way partitioning, where the array is partitioned into values less than, equal, and greater than the pivot. Because the pivot values need not be sorted recursively, 3-way quick sort adapts to O(n) time in this case.

Shell sort also adapts to few unique keys, though I do not know its time complexity in this case.

指示

  • 点击刷新来重新启动在行、列或整个表格中的动画。
  • 可以直接点击动画图像来启动或重新启动它。
  • 点击问题的规模来重置所有的动画。

关键

  • 黑色的值是已经排好序的。
  • 灰色的值是还未完成排序的。
  • 一个红色的三角形标记算法的位置。
  • 深灰色的值表示当前时间间隔(希尔排序, 归并排序, 快速排序)。
  • 一对红色的三角形标记着左、右指针(快速排序)。

参考文档

Algorithms in Java, Parts 1-4, 3rd edition by Robert Sedgewick. Addison Wesley, 2003.

Programming Pearls by Jon Bentley. Addison Wesley, 1986.

Quicksort is Optimal by Robert Sedgewick and Jon Bentley, Knuthfest, Stanford University, January, 2002.

Dual Pivot Quicksort: Code and Discussion.

Bubble-sort with Hungarian ("Csángó") folk dance YouTube video, created at Sapientia University, Tirgu Mures (Marosvásárhely), Romania.

Select-sort with Gypsy folk dance YouTube video, created at Sapientia University, Tirgu Mures (Marosvásárhely), Romania.

Sorting Out Sorting, Ronald M. Baecker with the assistance of David Sherman, 30 minute color sound film, Dynamic Graphics Project, University of Toronto, 1981. Excerpted and reprinted in SIGGRAPH Video Review 7, 1983. Distributed by Morgan Kaufmann, Publishers. Excerpt.