Presentation is loading. Please wait.

Presentation is loading. Please wait.

Horstmann chapter 8 continued. Sorting arrays The telephone book is easy to use, because the entries are sorted Sorting is a common task, and many many.

Similar presentations


Presentation on theme: "Horstmann chapter 8 continued. Sorting arrays The telephone book is easy to use, because the entries are sorted Sorting is a common task, and many many."— Presentation transcript:

1 Horstmann chapter 8 continued

2 Sorting arrays The telephone book is easy to use, because the entries are sorted Sorting is a common task, and many many algorithms are available An array need only be sorted once, and can then be searched many times We will use one specific algorithm: Selection Sort

3 Selection Sort Initially, the whole array is unsorted Until the array is sorted, do the following: Find the largest element in the unsorted array If it is not the final element in the unsorted portion of the array, swap it with the final element of the unsorted portion Reduce the unsorted portion by one (from the right)

4 Selection Sort illustrated... 2 9 5 4 8 1 6 // the whole list is unsorted (els 0..6) 2 9 5 4 8 1 6 // the largest element is non-final 2 6 5 4 8 1 9 // swap them. Elements 0..5 are now unsorted 2 6 5 4 8 1 9 // 8 is largest and non-final 2 6 5 4 1 8 9 // 6 is largest and non-final 2 1 5 4 6 8 9 // 5 is largest and non-final 2 1 4 5 6 8 9 // 4 is largest, and final: do nothing 2 1 4 5 6 8 9 // 2 is largest and non-final 1 2 4 5 6 8 9 // finished once the unsorted bit is less tha 2 el.s In this example, the underlined section of the array is the unsorted part of that array, and the non-underlined section is the part that is correctly sorted.

5 Anatomy of the SelectionSort program public class SelectionSort { public static void main(String[] args){ // make a list, print it, sort it, print it again } public static void printList(double[] list) { } public static void selectionSort(double[] list) { } }

6 Implementing Selection Sort // ‘i’ holds the index of the end of the // unsorted section of the list. This goes // down by one each time we do a selection for(int i=list.length-1; i>0; i--) { // find max in list[0]..list[i] // swap max with list[i] if necessary }

7 Passing arrays in to methods public static int max(int num1, int num2) modifiers return type method name parameters each parameter has a type and a name. This tells us that method max is public, takes two integers as arguments (in the method these are called num1 and num2 ), and returns an integer as its answer. public static int max(int[] mylist) modifiers return type method name parameter This method takes an array as its argument This tells us that method max is public, takes an array of integers as its argument (within the method this will be called mylist ), and returns an integer as its answer. We can also give arrays as arguments:

8 An array argument in the main method public static void main(String[] args) { This line tells us that main is a method which takes an array of Strings as its argument, and calls that array args public class Fintan { … What does this mean? It means that when we run our program from the command line, we can give extra arguments that go in the args array: >javac Fintan.java >java Fintan first second third Now first second and third are arguments to the main method of the Fintan program: args[0]=“first”, args[1]=“second”, rgs[2]=“third”. These args can be used in the program.

9 Generalised for Loop (a ‘for each’ loop) A “for each” loop for all elements of a collection: double[] data =...; double sum = 0; for (double e : data) // read this as "for each e in data" { sum = sum + e; } Standard for loop doing the same thing for (int i = 0; i < data.length; i++) { double e = data[i]; sum = sum + e; }

10 Syntax 8.3: The "for each" Loop for (Type variable-name : collection) statement Example: for (double e : data) sum = sum + e; Purpose: To execute a loop for each element in the collection. In each iteration, the variable is assigned the next element of the collection. Then the statement is executed.

11 Different forms of “for” loop. Arrays and for-loops are closely connected. Why? Because you use a for-loop to go through the elements in an array. When writing a program, there is a noticeable difference between what you can do with a for (double e: data) loop and a for (int x=0; x <data.length;x++) loop. What is the difference? In the “for each” loop, you don’t know the number of the current element. In the standard for loop, that number is in the variable being used.

12 Copying Arrays Why does this run into problems? int[] list1 = {0, 1, 2, 3, 4, 5}; int[] list2 = new int[list1.length]; list2 = list1; list1[0] = 99; System.out.println(list2[0]);

13 What's happening? list1 012345 list2 000000

14 What's happening? list1 12345 list2 000000 099 For a more complete example: Liang, Example 5.6, pp 177ff

15 Three possible solutions 1.Element-by-element copy 2.Create a clone of the array (later) 3.Use System.arrayCopy() -- a static method

16 Element-by-element copy int[] source = {0,1,2,3,4,5}; int[] target = new int[source.length]; for(int i=0; i<source.length; i++) { target[i] = source[i]; }

17 Using System.arrayCopy System.arrayCopy(source, 0, target, 0, sourceArray.length); source array starting position.....(source) target array starting position.......(target) How many elements should be copied?

18 Multidimensional Arrays This array is one-dimensional: int[] list1 = {4, 2, 6, 3, 8, 4, 9, 5}; This array is 2-D (a matrix): int[][] matrix1 = { {1, 2, 3, 4}, {2, 4, 6, 8}, {3, 6, 9, 12} };

19 Creating a 5*5 matrix of random values double[][] ranMatrix = new double[5][5]; for(int i=0; i<5; i++) { for(int j=0; j<5; j++) { ranMatrix[i][j] = Math.random(); }


Download ppt "Horstmann chapter 8 continued. Sorting arrays The telephone book is easy to use, because the entries are sorted Sorting is a common task, and many many."

Similar presentations


Ads by Google