hw1sol - CSCI4041 Homework1 Solution Key Spring 2009...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CSCI4041 Homework1 Solution Key Spring 2009 1 [20pt] [10pt] Pseudocode 1 2 3 4 5 6 7 8 9 10 11 12 13 14 SORT(A,n) // A: array, n: size of array for i <- 1 to n-1 // find small lest smallest <- i for j <- i+1 to n if(A[j] < A[smallest]) then smallest <- j end end // j loop // swap i and smallest Temp <- A[i] A[i] <- A[smallest] A[smallest] <- Temp end // i loop end // SORT (-1) for each incorrect loop start/end index (-1) for each mistakes of keeping smallest index in i-loop [4pt] Loop invariant For each iteration of I-loop (line 1-13), every element of index < i is sorted in increasing order (2pt), and every element of index i is greater than every element of index < i (2pt). [2pt] According to the loop invariant, left n-1 elements are sorted, and n-th element is greater than its n-1 left elements in n-th iteration. So n-th iteration is not needed. [2pt] Best case: Θ (n 2 ) [2pt] Worst case: Θ (n 2 ) 2. [20pt] [10] Pseudocode 1 2 3 4 5 6 7 8 9 10 11 12 13 FIND_MIN_MAX(A,s,t) // A: array, i:start index, t: last index,
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/09/2009 for the course CSCI 4041 taught by Professor Arindambanerjee during the Spring '09 term at Minnesota.

Page1 / 2

hw1sol - CSCI4041 Homework1 Solution Key Spring 2009...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online