Lab 15.1 — Largest Element Selection Sort (SelectionSort)
The selection sort presented in class repeatedly found smallest elements of the remaining values to be sorted and placed them
in successive positions beginning at 0 and moving forward through the array. An equivalent approach is to successively find the
largest elements and place them in the array beginning at the last element and moving backward through the array.
Code this second version of selection sort.
- Use the version that finds the position of the largest element and swaps it with
the current element (the version residing in the directory
SelectionSort
of the corresponding lecture).
- Thus, only one swap is done for each inner pass through the array
- After each largest element has been placed and swappedinto its proper place, print out the array.
- For data, read in an array of numbers from the file
numbers.text
(assume a header value precedes the numbers).
Sample Test Run
For example if the file numbers.text
contains:
6
2
6
3
4
1
5
the program should produce the following output:
Initial: {2, 6, 3, 4, 1, 5}
Pass #1: {2, 5, 3, 4, 1, 6}
Pass #2: {2, 1, 3, 4, 5, 6}
Pass #3: {2, 1, 3, 4, 5, 6}
Pass #4: {2, 1, 3, 4, 5, 6}
Pass #5: {1, 2, 3, 4, 5, 6}
Pass #6: {1, 2, 3, 4, 5, 6}
Sorted: {1, 2, 3, 4, 5, 6}
- The code for selection sort has been given to you; this is a minor variation on that code.