diff options
| author | mo khan <mo@mokhan.ca> | 2021-09-11 14:56:21 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2021-09-11 14:56:21 -0600 |
| commit | 8868869969165aad158fcdd4b9f3e05866282395 (patch) | |
| tree | 145ea9c846e36aeda75d03af0284db48e376a489 | |
| parent | c9c0c297dd52c94668add0e7a0a5f1fb82f8dd66 (diff) | |
add notes on insertion sort
| -rw-r--r-- | notes.md | 43 |
1 files changed, 43 insertions, 0 deletions
@@ -223,3 +223,46 @@ Problem 1-1: Comparison of running times | 2^n | | | | | | n! | | | | | ``` + +# Chapter 2 Getting Started + + +2.1 Insertion Sort + +Solves the sorting problem. + +Input: A sequence of `n` numbers `{a1,a2,...,aN}` +Output: A permutation (reordering) of the input sequence such that: + + `{a1 <= a2 <= ... <= aN}` + +The numbers that we want to sort are known as keys. + +e.g. + +```plaintext + +a. [5, 2, 4, 6, 1, 3] + x i +b. [2, 5, 4, 6, 1, 3] + x x i +c. [2, 4, 5, 6, 1, 3] + x x x i +d. [2, 4, 5, 6, 1, 3] + x x x x i +e. [1, 2, 4, 5, 6, 3] + x x x x x i +f. [1, 2, 3, 4, 5, 6] +``` + +```plaintext +A = {5,2,4,6,1,3} + +for j = 2 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 +``` |
