Pseudocode Conventions
Notes
-
INSERTION-SORT(A)
- for j = 2 to to A.length
- key = A[j]
- i = j-1
- while i > 0 and A[i] > key
- A[i+1] = A[i]
- i = i - 1
- A[i+1] = key
- I will mostly follow the book's pseudocode notation
- Why pseudocode ?
-
void InsertionSort(vector& A) { for(int j = 1; j < A.size(); ++j) { int key = A[j]; int i = j-1; while (i >= 0 and A[i] > key) { A[i+1] = A[i]; --i; } A[i+1] = key; } } - Is there a major difference?
- You might want to look at the full code.
-
- Pseudocode conventions (from the book)
- Use program control structures
- if, if/else, if/elseif/elseif/else
- case
- while, for, repeat-until
- No code blocks, indentation like python
- // for comments
-
a = b = cis permitted - variables are local, no globals
- Parameters are pass by value
- Objects are passed by pointer.
- But arrays are passed by pointer.
- [] for array access, arrays start at 1
- Already noted
- I am sure I will goof this up.
- Objects have attributes (A.length)
- Return can return 0 or more objects
- Short circuit evaluation
- line 4 above will not cause an error.
- error is like a c++ exception
- Use program control structures