Escolar Documentos
Profissional Documentos
Cultura Documentos
Sorting
Sorters Sorting algorithms Insertion sorting Exchange sorting Selection sorting Merge sorting Distribution sorting
Sorters
a sorter as an abstract machine (an object) its purpose is to sort arrays of data Sorter is an interface method sort takes an array of Comparable objects and sorts the contents of the array AbstractSorter is the base class from which various sorters are derived
Class hierarchy
Insertion sorting
Exchange sorting
algorithms that sort by exchanging pairs of items until the sequence is sorted may exchange adjacent elements as well as widely separated ones insertion sorting can be considered as a special case of exchange sorting
Bubble sort
Quick sort
1. 2.
3.
4.
To sort the sequence S = {s1,s2, s3,, sn} Select one of the elements of S. The selected element, p, is called the pivot. Remove p from S and then partition the remaining elements of S into two distinct sequences, L and G, such that every element in L is less than or equal to the pivot and every element in G is greater than or equal to the pivot. Rearrange the elements of the sequence as follows: S = {L, p, G} Recursively quicksort the unsorted sequences L and G.
Quick sort
Quick sort
Selection sorting
Heap sorting
Heap sorting
Heap sorting
Merge sorting
Merge sorting
Merge sorting
Decision tree
Therefore, the running time of any sorting algorithm that sorts using only binary comparisons is (n log n)
Distribution sorting
distribution sorting does not compare the elements to be sorted requires that we know something about the basis set from which the elements to be sorted are drawn e.g., if the basis set is small, we can use bucket sort if the elements can be represented with a small, finite number of bits (or digits, letters or symbols) we can use radix sort
Bucket sorting
Radix sorting
used when each element of the basis set can be viewed as a sequence of symbols make multiple passes through the data in the first pass sort by the least significant symbol in the last pass sort by the most significant symbol use a bucket sort in each pass
Radix sorting
References
Preiss, ch.14