diff options
Diffstat (limited to 'notes.md')
| -rw-r--r-- | notes.md | 20 |
1 files changed, 20 insertions, 0 deletions
@@ -460,6 +460,26 @@ the rate of growth in determining computational efficiency for large inputs. > manner for the first `n - 1` elements of `A`. Write pseudocode for *selection sort*. + + n = A.length + for i = 1 to n - 1 { + min = i + for j = i + 1 to n { + if A[j] < A[min] + min = j + A[i], A[min] = A[min], A[i] + + What loop invariant does this algorithm maintain? + + * intialization: inner loop initializes to the right index of the outer loop + * maintenance: with the inner right array of the outer array. + * termination: when end of loop is reached + Why does it need to run for only the first n-1 elements, rather than for all `n` elements? + + * The last element of the array should has nothing to be compared against. + Give the best-case and worst-case running times of selection sort in 𝚯-notation. + + = 𝚯(n^2). two loops, one nested within the other. |
