Author: Hayden Smith 2021
Why?
What?
Sorting refers to arranging a collection of items in order.
Typically items are sorted based on an ordering relation of that property:
For array a[N], we would sort it by saying lo = 0, hi = N-1
Pre-conditions:
Post-conditions:
Generally speaking, most algorithms do a sort "in-place" (consists of doing a series of intelligent swapping between elements). These swaps help move unsorted items to the beginning of the array.
// we deal with generic Items
typedef SomeType Item;
// abstractions to hide details of Items
#define swap(A,B) {Item t; t = A; A = B; B = t;}
// Sorts a slice of an array of Items, a[lo..hi]
void sort(Item a[], int lo, int hi); // TODO
// Check for sortedness (to validate functions)
bool isSorted(Item a[], int lo, int hi)
{
for (int i = lo; i < hi; i++) {
if (!less(a[i], a[i+1])) return false;
}
return true;
}