24
Liang, Chpt 5 continued

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

  • View
    213

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 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

Liang, Chpt 5 continued

Page 2: 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

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

Page 3: 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

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)

Page 4: 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

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.

Page 5: 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

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) {

}

}

Page 6: 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

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

}

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

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(n2).....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?)

Page 8: 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

O(n2)

2

2

)1(

1.....)3()2()1(

2 nn

nn

nnnn

n n2

1 1

10 100

100 10000

1000 1000000

Page 9: 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

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

Page 10: 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

Variables in methodsWe 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

}

Page 11: 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

“Class-level” variablesWe 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){

}

Page 12: 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

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.

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

Passing arrays in to methodspublic static int max(int num1, int num2)

modifiersreturn type

method nameparameters

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)

modifiersreturn type

method nameparameter

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:

Page 14: 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

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.

Page 15: 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

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

Page 16: 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

What's happening?

list1

0 1 2 3 4 5

list2

0 0 0 0 0 0

Page 17: 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

What's happening?

list1

1 2 3 4 5

list2

0 0 0 0 0 0

099

For a more complete example: Liang, Example 5.6, pp 177ff

Page 18: 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

Three possible solutions

1. Element-by-element copy

2. Create a clone of the array (later)

3. Use System.arrayCopy() -- a static method

Page 19: 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

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

}

Page 20: 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

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?

Page 21: 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

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}

};

Page 22: 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

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

}

}

Page 23: 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

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

Page 24: 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

Reminder: adding matrices

[ [ [[ [ [

a b

c d yx

+w a+w

=z

b+x

d+zc+y

And multiplication......