Exercise#3WithSolutions

# Exercise#3WithSolutions - MIE335 Questions 1 Algorithm...

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

1 Algorithm analysis Look at the following pseudo code. procedure doSomething( A : list of items ) do swapped = false for i = 1 to length(A) - 1 if A[i] > A[i+1] then swap( A[i], A[i+1] ) swapped = true end if end for while (swapped == true) end procedure ˆ What does this algorithm do? Sort items in ascending order ˆ What is the worst running time (Big-O)? O( n 2 ). This algorithm requires both inner and outer loop to complete before termination. ˆ Use this algorithm for the following input: A = { 5,3,7,1 } . Show all steps. Hint: you may wish to label your iterations! Iteration 1 5 3 7 1 3 5 7 1 3 5 7 1 3 5 7 1 3 5 7 1 3 5 1 7 Iteration 2 3 5 1 7 3 5 1 7 3 5 1 7 3 1 5 7 3 1 5 7 3 1 5 7 Iteration 3 3 1 5 7 1 3 5 7 1 3 5 7 1 3 5 7 1 3 5 7 1 3 5 7 ˆ How many iterations will the above example take? 4. Needs one additional pass after everything is sorted. 1

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 03/31/2012 for the course MIE 335 taught by Professor Frances during the Spring '12 term at University of Toronto- Toronto.

### Page1 / 3

Exercise#3WithSolutions - MIE335 Questions 1 Algorithm...

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

View Full Document
Ask a homework question - tutors are online