180 likes | 196 Views
This slide set provides an overview of the selection sort algorithm for sorting an array. It includes code examples and explanations. Topics covered: selection sort, swapping, enumerated types.
E N D
Programming with Arrays 2 This slide set was compiled from the Absolute Java textbook slides (Walter Savitch) and the professor’s own class materials. CSS161: Fundamentals of Computing
Sorting an Array – Selection Sort • Key algorithm for (int index = 0; index < count; index++) Place the indexth smallest element in a[index] 8 6 11 17 3 15 5 19 28 12 3 6 11 17 8 15 5 19 28 12 3 5 11 17 8 15 6 19 28 12 3 5 6 17 8 15 11 19 28 12 3 5 6 8 17 15 11 19 28 12 3 5 6 8 11 15 17 19 28 12 3 5 6 8 12 12 17 19 28 15 3 5 6 8 12 11 15 19 28 17 3 5 6 8 12 11 15 17 28 19 CSS161: Fundamentals of Computing 3 5 6 8 12 11 15 17 19 28
First Swapping in Selection Sort CSS161: Fundamentals of Computing
Second Swapping in Selection Sort CSS161: Fundamentals of Computing
Code for Selection Sort (1 of 3) public class SelectionSort { /** Precondition: numberUsed <= a.length; The first numberUsed indexed variables have values. Action: Sorts a so that a[0] <= a[1] <= ... <= a[numberUsed - 1]. */ public static void sort(double[] a, int numberUsed) { int index, indexOfNextSmallest; for (index = 0; index < numberUsed - 1; index++) {//Place the correct value in a[index]: indexOfNextSmallest = indexOfSmallest(index, a, numberUsed); interchange(index,indexOfNextSmallest, a); //a[0] <= a[1] <=...<= a[index] and these are the smallest //of the original array elements. The remaining positions //contain the rest of the original array elements. } } CSS161: Fundamentals of Computing
Code Code for Selection Sort (2 of 3) /** Returns the index of the smallest value among a[startIndex], a[startIndex+1], ... a[numberUsed - 1] */ private static int indexOfSmallest(int startIndex, double[] a, int numberUsed) { double min = a[startIndex]; int indexOfMin = startIndex; int index; for (index = startIndex + 1; index < numberUsed; index++) if (a[index] < min) { min = a[index]; indexOfMin = index; //min is smallest of a[startIndex] through a[index] } return indexOfMin; } CSS161: Fundamentals of Computing
Code for Selection Sort (3 of 3) /** Precondition: i and j are legal indices for the array a. Postcondition: Values of a[i] and a[j] have been interchanged. */ private static void interchange(int i, int j, double[] a) { double temp; temp = a[i]; a[i] = a[j]; a[j] = temp; //original value of a[i] } } CSS161: Fundamentals of Computing
Self-Test Exercises • Work on textbook p378’s exercises 20 ~ 21. • Trace Selection Sort with the following set of number: • int a[9] = {4, 7, 8, 5, 6, 0, 1, 2, 3}; CSS161: Fundamentals of Computing
Enumerated Types • Syntax enum Type_Name {VALUE_1, VALUE_2 …, VALUE_N}; • Example enum WorkDay {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY}; • Variables WorkDay meetingDay = null; meetingDay = WorkDay.THURSDAY; if ( meetingDay == WorkDay.MONDAY ) System.out.println( “A blue Monday” ); CSS161: Fundamentals of Computing
An Enumerated Type CSS161: Fundamentals of Computing
Methods Included with Enumerated Type (1of 3) CSS161: Fundamentals of Computing
Methods Included with Enumerated Type (2 of 3) CSS161: Fundamentals of Computing
Methods Included with Enumerated Type (3 of 3) CSS161: Fundamentals of Computing
The values( ) Method CSS161: Fundamentals of Computing
The values( ) Method CSS161: Fundamentals of Computing
Enumerated Type in a switch Statement (1 of 3) CSS161: Fundamentals of Computing
Enumerated Type in a switch Statement (2 of 3) CSS161: Fundamentals of Computing
Enumerated Type in a switch Statement (3 of 3) CSS161: Fundamentals of Computing