A. Notes
Selection Sort is the most straight forward sorting algorithm.
Steps to sort an array ascending are below.
Step 1 − Set MIN to location 0 (i.e. index 0)
Step 2 − Search the minimum element in the array
Step 3 − Swap with value at location MIN (i.e. index MIN)
Step 4 − Increase MIN to point to next element
Step 5 − Repeat until the array is sorted
To sort an array descending, just to make a few changes:
Step 1 − Set MAX to location 0 (i.e. index 0)
Step 2 − Search the maximum element in the array
Step 3 − Swap with value at location MAX (i.e. index MAX)
Step 4 − Increase MAX to point to next element
Step 5 − Repeat until the array is sorted
B. Watch
A good example of step by step sorting process is here.
This YouTube video is only 3 minutes, but very helpful.
C. HW:
On a piece of paper, sort the following array of integers ascending and descending, note down the steps that you used and compare them with the steps in A. Did you sort differently? How?
30099
181263
68883
79769
77407
101294
514924
310043
623400
255034
131737
87337
107430
227931
56400
116882
2. Refer to the links in B, apply the selection sort algorithm(ascending) to the above array on paper without writing code, what the array look like when MIN is 4?
3. Refer to the links in B, apply the selection sort algorithm(descending) to the above array on paper without writing code, what the array look like when MAX is 10?
4. Write Java code for selection sort to sort the above array both ascending and descending.
https://replit.com/@meteacher/SelectionSort#Main.java