For Week 3
void insertion_sort(int array[], int n)
// PRE: n is the length of array
// POST: array is sorted
{
int i, j, key;
for(j = 1; j < n; j++) //Notice starting with 1 (not 0)
// Invariant for the outer loop: array[0..j-1] is sorted, descending order
{
key = array[j];
for(i = j - 1; (i >= 0) && (array[i] < key); i--) //Move smaller values up one position
{
array[i+1] = array[i];
}
array[i+1] = key; //Insert key into proper position (++)
}
return;
}
W, T, N, J, E, B, A W, T, N, A, B, E, J A, B,W,J,N,T,EFor each tree write down the sequence of letters obtained with an inorder, preorder and postorder traversal.
All enquiries to Annabelle McIver