hw1sol - CSCI4041 Homework1 Solution Key Spring 2009...

Info icon This 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, returns array with 2 elements {min, max} if s=t then return {A[s], A[s]} end mid <- s + floor( (t-s+1)/2)
Image of page 1

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern