Sorting1.1 - Selection Sort Selection Sort operates by...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: Selection Sort Selection Sort operates by first finding the smallest element in the input list, and moving it to the output list. It then finds the next smallest value and does the same. It continues in this way until all the input elements have been selected and placed in the output list in the correct order. Note that every selection requires a search through the input list. Thus the algorithm has a nested loop structure Selection Sort Example CSE 2011 Prof. J. Elder -6- Last Updated: 4/1/10 11:16 AM Selection Sort for i = n-1 downto 0 jmin = 0 for j = 1 to i O(i ) if A[ j ] < A[jmin] jmin = j add A[jmin] to output remove A[jmin] from input T (n) = n2 i = O(n2 ) i =0 CSE 2011 Prof. J. Elder -7- Last Updated: 4/1/10 11:16 AM Bubble Sort Bubble Sort operates by successively comparing adjacent elements, swapping them if they are out of order. At the end of the first pass, the largest element is in the correct position. A total of n passes are required to sort the entire array. Thus bubble sort also has a nested loop structure Bubble Sort Example CSE 2011 Prof. J. Elder -8- Last Updated: 4/1/10 11:16 AM Expert Opinion on Bubble Sort CSE 2011 Prof. J. Elder -9- Last Updated: 4/1/10 11:16 AM Bubble Sort for i = n-2 downto 0 for j = 0 to i if A[ j ] > A[ j + 1 ] O(i ) swap A[ j ] and A[ j + 1 ] T (n) = n2 i = O(n2 ) i =0 CSE 2011 Prof. J. Elder - 10 - Last Updated: 4/1/10 11:16 AM ...
View Full Document

Page1 / 5

Sorting1.1 - Selection Sort Selection Sort operates by...

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

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