sorting 4 - Sorting Analysis Sorts to Consider Selection...

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

View Full Document Right Arrow Icon
Sorting Analysis
Background image of page 1

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

View Full DocumentRight Arrow Icon
Sorting Analysis 2 Sorts to Consider Selection sort Bubble sort Insertion sort Mergesort Quicksort Radixsort Why do we care about sorting?
Background image of page 2
Sorting Analysis 3 Selection sort One of the most intuitive sorts Find the largest item, and swap it with the item currently in the last position. Repeat this step on the “unsorted” part of the collection. initial array : 29 10 14 37 13 after 1st swap: 29 10 14 13 37 after 2nd swap: 13 10 14 29 37 after 3rd swap: 13 10 14 29 37 after 4th swap: 10 13 14 29 37 Algorithm: for (last = n-1; last >= 1; --last) { find the index of the largest item swap the largest item with the last item } Order of magnitude = O(n 2 )
Background image of page 3

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

View Full DocumentRight Arrow Icon
Sorting Analysis 4 Bubble sort Not a particularly good algorithm, but simple. Compare adjacent items and exchange them if they are out of order. Repeat. The largest item will eventually “bubble” to the correct position. Pass 1
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.

This note was uploaded on 03/17/2010 for the course CS 162 taught by Professor Fant during the Spring '08 term at Portland State.

Page1 / 10

sorting 4 - Sorting Analysis Sorts to Consider Selection...

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