{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lect13

lect13 - Recall from last lecture Bubble Sort Algorithm 1...

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

View Full Document Right Arrow Icon
Recall from last lecture: Bubble Sort Algorithm 1 Input the numbers x (1) ,...,x ( n ) (as a vector). 2 Input (or compute) n . 3 for k = 1 ,...,n - 1 for j = 1 ,...n - k if ( x ( j ) > x ( j + 1)) swop x ( j ) and x ( j + 1) end if end for j end for k 4 Output the sorted list 1
Background image of page 1

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

View Full Document Right Arrow Icon
Programs: bubble.m , complexity2.m Time for execution grows with O ( n 2 ). We showed this by counting the number of comparisons and swops: n ( n - 1) / 2. Can we do better? 2
Background image of page 2
Selection Sort Algorithm Items 1, 2 and 4 are as for bubble sort, but item 3 is replaced by the following 3 for k = 1 ,...,n - 1 assign l = k for j = k + 1 ,...,n if ( x ( j ) < x ( l )) assign l = j end if end for j if ( l 6 = k ) swop x ( k ) and x ( l ) end if end for k Whole thing coded as selection.m 3
Background image of page 3

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

View Full Document Right Arrow Icon
input vector x with the smallest element, then the second with the second smallest, and so on. The j -loop finds the position l of a smallest entry of the sub-vector [ x ( k ) ,...,x ( n )], after the sub-vector [ x (1) ,...,x ( k - 1)] has already been sorted. 4
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 11

lect13 - Recall from last lecture Bubble Sort Algorithm 1...

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

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