sorting algorithms

来源:互联网 发布:ubuntu安装包放在哪里 编辑:程序博客网 时间:2024/06/05 05:39
from http://www.concentric.net/~ttwang/sort/sort.htm


* Bubble Sort

Exchange two adjacent elements if they are out of order. Repeat untilarray is sorted. This is a slow algorithm.
* Selection Sort
Find the largest element in the array, and put it in the proper place.Repeat until array is sorted. This is also slow.
* Insertion Sort
Scan successive elements for out of order item, then insert the itemin the proper place. Sort small array fast, big array very slowly.
* Quicksort
Partition array into two segments. The first segment all elements areless than or equal to the pivot value. The second segment all elements aregreater or equal to the pivot value. Sort the two segments recursively.Quicksort is fastest on average, but sometimes unbalanced partitions canlead to very slow sorting.
* Mergesort
Start from two sorted runs of length 1, merge into a single run of twicethe length. Repeat until a single sorted run is left. Mergesort needsN/2 extra buffer. Performance is second place on average, withquite good speed on nearly sorted array. Mergesort is stable in thattwo elements that are equally ranked in the array will not have their relativepositions flipped.
* Heapsort
Form a tree with parent of the tree being larger than its children.Remove the parent from the tree successively. On average, Heapsort isthird place in speed. Heapsort does not need extra buffer, and performanceis not sensitive to initial distributions.
* Shellsort
Sort every Nth element in an array using insertion sort. Repeat usingsmaller N values, until N = 1. On average, Shellsort is fourth place inspeed. Shellsort may sort some distributions slowly.
* Combo Sort
Sorting algorithms can be mixed and matched to yield the desiredproperties. We want fast average performance, good worst case performance,and no large extra storage requirement. We can achieve the goal by startingwith the Quicksort (fastest on average). We modify Quicksort by sortingsmall partitions by using Insertion Sort (best with small partition). Ifwe detect two partitions are badly balanced, we sort the larger partitionby Heapsort (good worst case performance). Of course we cannot undo thebad partitions, but we can stop the possible degenerate case from continuingto generate bad partitions.

原创粉丝点击