{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

day11extra - COP 3503 Computer Science II CLASS NOTES...

Info icon This 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
Image of page 1

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

View Full Document Right 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].
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern