Presentation is loading. Please wait.

Presentation is loading. Please wait.

Liang, Chpt 5 continued. Sorting arrays The telephone book is easy to use, because the entries are sorted Sorting is a common task, and many many algorithms.

Similar presentations


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

1 Liang, Chpt 5 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: Liang 196-197 // ‘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 Complexity of Selection Sort? The complexity of an algorithm provides a measure of how execution time will scale as the size of the data grows Linear Search was O(n).....if the data size doubles, the time to do Linear Search also doubles Selection Sort is O(n 2 ).....if the data size doubles, the time to do Selection Sort increases by a factor of 4. This is an expensive algorithm.....but it may be worthwhile (when?)

8 O(n 2 ) n n 2 1 10 100 100 10000 1000 1000000

9 Where do we put our arrays (and other variables)? We can declare variables (like array variables) in three different places in a program: In the main method public class Fintan{ } … public static void main(String[] args){ } … String[] nameList = new String[6]; A variable declared in the main method is only visible within that method. To use nameList in another method, you have to “pass” it to that method.

10 Variables in methods We can also declare variables (like array variables) in other methods: public class Fintan{ } … public static int myMethod(){ } … String[] nameList = new String[6]; A variable declared in the myMethod method is only visible within that method. The nameList variable cannot be used in the main method. public static void main(String[] args){ // nameList is not visible here: // nameList[0] = “fintan” will not work }

11 “Class-level” variables We do not have to always declare variables within a method. We can also declare variables as just within the class itself: public class Fintan{ } public static int myMethod(){ } … public static String[] nameList = new String[6]; The nameList variable is declared as part of the class Fintan. It is visible within the main method, the myMethod method, and any other methods. Since nameList is public it is also visible outside the class as Fintan.nameList and could be used by other programs. As with two above methods, nameList is declared static (which means there is only one nameList variable in this class). public static void main(String[] args){ }

12 Where to put variables? Whether you put variables within main, within methods, or at class level, is a question of style and cleanness in your programs. Put variables at the class level if they’re important to and used by the whole class. Put variables in methods if they’re only going to be used by that method (and don’t need to be accessed outside the method). Put variables in the main method if you’re going to pass them into other methods.

13 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:

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

15 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]);

16 What's happening? list1 012345 list2 000000

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

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

19 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]; }

20 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?

21 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} };

22 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(); }

23 Example 5.7: adding and multiplying matrices Study Liang Example 5.7, pp 183 Method addMatrix uses 2 nested for-loops Method multiplyMatrix uses 3 nested for- loops! Higher dimensional matrices are possible: int[][][] arr3d = new int[5][5][5]; Use these at your own risk!!!!!

24 Reminder: adding matrices [[ [ [[[ ab cd y x + w a+w = z b+x d+z c+y And multiplication......


Download ppt "Liang, Chpt 5 continued. Sorting arrays The telephone book is easy to use, because the entries are sorted Sorting is a common task, and many many algorithms."

Similar presentations


Ads by Google