Day11extra - COP 3503 Computer Science II CLASS NOTES DAY#11 Supplement Elementary Sorting Algorithms Selection Sort Selection sort is an attempt

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

View Full Document Right Arrow Icon
COP 3503 – Computer Science II CLASS NOTES - DAY #11 Supplement Elementary Sorting Algorithms Selection Sort Selection sort is an attempt to localize the exchanges of array elements by finding a misplaced element first and putting it in its final place. The element with the lowest value is selected and exchanged with the element in the first position. Then for the remaining elements the smallest value is found and interchanged with the second position in the array, and so on. Example – Selection Sort Initial array 7 5 2 3 1 4 6 First swap produces (underlined cells indicate sorted portion of the array) 1 5 2 3 7 4 6 Second swap produces 1 2 5 3 7 4 6 Third swap produces 1 2 3 5 7 4 6 Fourth swap produces 1 2 3 4 7 5 6 Fifth swap produces 1 2 3 4 5 7 6 Sixth swap produces – completes sort 1 2 3 4 5 6 7 Day 11 Supplement - 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
Pseudocode and Java Implementations of Selection Sort Pseudocode version selectionsort(data[ ]) for (i = 0; i < data.length-1; i++) select the smallest element among data[i] , …, data[data.length-1]; swap it with data[i]; Generic Java version public void selectionsort( Object[ ] data) { int i, j, least; for (i=0;i < data.length-1; i++) { for j = i + 1; least = i; j < data.length; j++) if (((Comparable) data[j]).compareTo.(data[least]) < 0) least = j; swap(data,least, i); } } where swap( ) exchanges elements data[least], and data[i]. Swap is given by:
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/22/2009 for the course COP 3503c taught by Professor Staff during the Spring '08 term at University of Central Florida.

Page1 / 6

Day11extra - COP 3503 Computer Science II CLASS NOTES DAY#11 Supplement Elementary Sorting Algorithms Selection Sort Selection sort is an attempt

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

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