1 / 18

Programming with Arrays 2

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.

amccutcheon
Download Presentation

Programming with Arrays 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. First Swapping in Selection Sort CSS161: Fundamentals of Computing

  4. Second Swapping in Selection Sort CSS161: Fundamentals of Computing

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. An Enumerated Type CSS161: Fundamentals of Computing

  11. Methods Included with Enumerated Type (1of 3) CSS161: Fundamentals of Computing

  12. Methods Included with Enumerated Type (2 of 3) CSS161: Fundamentals of Computing

  13. Methods Included with Enumerated Type (3 of 3) CSS161: Fundamentals of Computing

  14. The values( ) Method CSS161: Fundamentals of Computing

  15. The values( ) Method CSS161: Fundamentals of Computing

  16. Enumerated Type in a switch Statement (1 of 3) CSS161: Fundamentals of Computing

  17. Enumerated Type in a switch Statement (2 of 3) CSS161: Fundamentals of Computing

  18. Enumerated Type in a switch Statement (3 of 3) CSS161: Fundamentals of Computing

More Related