Sorting algorithms are often classified by
Computational complexity
worst, average and best behavior
Memory usage
Recursive or non-recursive
Stability
Whether or not they are a comparison sort
General method
insertion, exchange (bubble sort and quicksort), selection (heapsort), merging, serial or parallel…
Stable sorting algorithms
Maintain the relative order of records with equal values
If two items compare as equal, then their relative order will be preserved
When sorting only part of the data is examined when determining the sort order
procedure selectionSort( A : list of sortable items )
n = length(A)
for i = 0 to n-1 inclusive do
min = i;
for j = i+1 to n inclusive do
if A[j] < A[min]
min = j
end if
end for
if min != j
swap(A[j], A[min])
end if
end for
end procedure
procedure bubbleSort( A : list of sortable items )
n = length(A)
repeat
swapped = false
for i = 1 to n-1 inclusive do
/* if this pair is out of order */
if A[i-1] > A[i] then
/* swap them and remember something changed */
swap( A[i-1], A[i] )
swapped = true
end if
end for
until not swapped
end procedure
procedure insertionSort( A : list of sortable items )
i = 1
while i < length(A)
j = i
while j > 0 and A[j-1] > A[j]
swap A[j] and A[j-1]
j = j - 1
end while
i = i + 1
end while
end procedure
algorithm quicksort(A, lo, hi) is
if lo < hi then
p = partition(A, lo, hi)
quicksort(A, lo, p)
quicksort(A, p + 1, hi)
algorithm partition(A, lo, hi) is
pivot = A[lo]
i = lo - 1
j = hi + 1
loop forever
do
i = i + 1
while A[i] < pivot
do
j = j - 1
while A[j] > pivot
if i >= j then
return j
swap A[i] with A[j]
merge(A, lo, mid, hi)
{
/* Merge A[lo..mid] with A[mid+1..hi] */
i = lo
j = mid+1
for (int k = lo; k <= hi; k++)
/* Copy a[lo..hi] to aux[lo..hi] */
Aux[k] = A[k];
for (int k = lo; k <= hi; k++)
/* Merge back to a[lo..hi] */
if (i > mid) A[k] = Aux[j++];
else if (j > hi ) A[k] = Aux[i++];
else if (less(Aux[j], Aux[i])) A[k] = Aux[j++];
else A[k] = Aux[i++];
}
sort(A, lo, hi)
{
/* Sort a[lo..hi] */
if (hi <= lo) return;
int mid = lo + (hi - lo)/2;
/* Sort left half */
sort(a, lo, mid);
/* Sort right half */
sort(a, mid+1, hi);
/* Merge results */
merge(a, lo, mid, hi);
}
Name | Best | Avg | Worst | Memory | Stable |
---|---|---|---|---|---|
Bubble | n | n^2 | n^2 | 1 | Yes |
Insertion | n | n^2 | n^2 | 1 | Yes |
Quick | n*log(n) | n*log(n) | n^2 | log(n) | Depends |
Merge | n*log(n) | n*log(n) | n*log(n) | n | Yes |
Heap | n*log(n) | n*log(n) | n*log(n) | n | No |
Bogo | n | n*n! | n*n! | 1 | No |
for each item in the list:
if that item has the desired value,
stop the search and return the location.
return nothing.
binarySearch(A, key, lo, hi)
if (to < from)
// set is empty, so return value showing not found
return KEY_NOT_FOUND
// calculate midpoint to cut set in half
middle = midpoint(from, to)
if (A[middle] > key)
// key is in lower subset
return binarySearch(A, key, from, middle - 1)
else if (A[middle] < key)
// key is in upper subset
return binarySearch(A, key, middle + 1, to)
else
// key has been found
return middle
binarySearch(A, int key)
while (from <= to)
//calculate the midpoint for roughly equal partition x/
middle = midpoint(from, to)
// determine which subarray to search
if (A[middle] < key)
// change from index to search upper subarray
from = middle + 1
else if (A[middle] > key)
// change to index to search lower subarray
to = middle - 1
else
// key found at index middle
return middle
return KEY_NOT_FOUND