0% found this document useful (0 votes)
8 views2 pages

ICS4UI - Sorting Algorithmsm - Selection Sort

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

ICS4UI - Sorting Algorithmsm - Selection Sort

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

ICS 4UI​ ​ ​ Advanced Algorithms​ ​ ​ Date:______

Selection Sort

Sorting is the process of putting items in a designated order, either from low to
high or high to low. The selection sort algorithm starts by finding the lowest item
in a list and swapping it with the first. Next, the lowest item among items 2
through the last is found and swapped with item 2, and then the lowest item
among items 3 through the last is swapped with item 3. This process is
continued until the last item is reached, at which all the items are sorted.

The selection sort algorithm compares an element to the items in the array after
that element. This algorithm can be implemented with nested for loops. The
outer loop controls which element to compare and the inner for loop iterates
through the array after the element (the subarray). The selection sort
pseudocode for sorting an array of items from low to high appears like:

for (arrayIndex = 0 to numItems – 1)


for (j = arrayIndex+1 to numItems – 1) {
if (items[j] < items[arrayIndex]) {
​ ​ swap items[j] and items[arrayIndex]
​ }
}
}

Write the actual code (with proper syntax) to create a selectionSort method. I
have given you the method header:

public static void selectionSort(int [] items) {

//you try writing the actual code, use the pseudocode above for
assistance. Don’t forget to use a temp variable when swapping. Use this
sorting method on the TestSorts program below.

//If you are having difficulties thinking of this code, reach out to me

}
Discuss with a classmate:
1.​ Why the outside loop starts a 0 and ends at numItems-1;
2.​ Why the inside loop starts at arrayIndex+1 and ends at numItems-1;
3.​ What the purpose is of a temp variable in the swap routine;
4.​ What change needs to be made such that the array will be sorted into
descending order (from largest to smallest).
Create an application called TestSorts.java, that has the user enter how many
random numbers (1-100) they want generated. Store them in an array, display
the unsorted array, and then the sorted array. Output could look similar to:
Enter the number of elements: 10
Unsorted:
91​ 36​ 4​ 65​ 45​ 67​ 87​ 93​ 24​ 48
Sorted
4​ 24​ 36​ 45​ 48​ 65​ 67​ 87​ 91​ 93

Sorting Objects

Relational operators, such as > and <, cannot be used to compare objects.
Objects use methods of their class to determine if one object is greater than, less
than, or equal to another. The equals() method in a class is used to determine
equality. For determining order, the compareTo() method is used. You have used
this method with Strings as well implementing your own version of it with Circles.

Objects that are to be sorted must have a class that implements the Comparable
interface. The String, Double, and Integer classes implement the Comparable
interface. Remember our Circle class way back…you should have implemented
the Comparable interface with the Puck class.

When a class implements an interface, what must you make sure to do when you
code the class?

An interface cannot be used to instantiate a class. However, an interface can be


used as a data type. An interface data type can reference any class that
implements it. This polymorphic behaviour makes it possible to implement a
generic sort that works with any list of objects that implement the Comparable
interface.

Write the proper syntax code for a selectionSort method that sorts an array of
Objects: (code will be similar to your previous sort method)

public static void selectionSort(Comparable[] items) {

//try this one on your own as well. You will need to use the
“compareTo()” method created/overwritten from your other classes.

//Reach out to me if you need some help with this one as well.

}

Below is a suggestion on how to test your Object sort:
Create a new file called testCircleSorts, in your Circle project you made and test
your method by creating a user defined number of randomly sized Circles (in an
array of Circles), which will then output the Circle’s radius unsorted and sorted.

You might also like