cs 106 introduction to computer science i 10 / 15 / 2007 instructor: michael eckmann

30
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann

Post on 22-Dec-2015

217 views

Category:

Documents


2 download

TRANSCRIPT

CS 106Introduction to Computer Science I

10 / 15 / 2007

Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 106 - Fall 2007

Today’s Topics• Comments and/or Questions?• methods

– type promotion of arguments– another example method

• Sorting

• Searching

a method call with the wrong typepublic int test_value(double input_val){

if (input_val < 0)return –1;

elsereturn 1;

}Example call in some other method: int length = 3;

int returned_val;returned_val = test_value(length); // call is allowed: int length is promoted to double;

a method to find the smallest of 4 ints

• we’ll write a method to find the smallest of four integers (not in an array) and return that value.

• what might we call this method?• what will the parameter list be?• will we return a value? if so, what type?• will we need any local variables besides the

parameters?• Let's write your ideas on the board.

a method to find the smallest of 4 intspublic int smallest_of4(int num1, int num2, int num3, int num4)

{int smallest;if (num1 < num2) // compare the first two

smallest = num1;else

smallest = num2;if (num3 < smallest) // compare the 3rd against the smallest so far

smallest = num3;if (num4 < smallest) // compare the 4th against the smallest so far

smallest = num4;

return smallest;}

example call of this method

int int1=14, int2=-9, int3=10, int4=879;int least;

least = smallest_of4(int1, int2, int3, int4);

// least will contain the value -9

let's write it by passing in an array

- any ideas?

Sorting

• Sorting is an important topic in computer science. • We'll discuss what sorting is and several

algorithms for doing it.• There are a myriad of ways to sort.• Arrays of data lend themselves easily to

being sorted. • Sorting can be done in ascending or

descending order.

Sorting• Original unsorted: 38, 41, 22, 12, 67

• We want the list to be: 12, 22, 38, 41, 67

• To accomplish this, one way would be to:– Go through the whole list once and find the lowest value. – Take it out and put it in a new list. – Go through the remaining list of numbers and find the lowest – Take that one out and put it at the end of the new list. – And so on ... until there's nothing left in the list.

• Does everyone agree that this will achieve a sorted list.

• We didn't say how exactly we'd find the lowest value each time --- the next slide describes a different way to sort and is more detailed.

Sorting

• Another way would be to:– compare the item in the first position to all the other

items in the list and swap them in place if the item in the first position is greater than the item in the other position.

– the second pass does the same thing but with the item in the second position.

– third pass does the same but with the 3rd item.– and so on, until the last position is reached.

• Why would this work or not work?

Sorting

• This algorithm doesn’t require a new list like the first one mentioned. All the sorting is done within the one list.

• After the first pass, what is guaranteed about the list?

• After the second pass, what is guaranteed about the list?

• Etc.

Sorting

• Original unsorted: 38, 41, 22, 12, 67

• compare 38 to 41. 38 is not > 41, so leave them in place.

• compare 38 to 22. Swap them. So, now list is 22, 41, 38, 12, 67

• compare 22 to 12. Swap them. So, now list is 12, 41, 38, 22, 67

• compare 12 to 67. 12 is not > 67, so leave them in place.

• After first pass: 12, 41, 38, 22, 67

Sorting

• After first pass: 12, 41, 38, 22, 67• Now start at second position.

• compare 41 to 38. Swap them. So, now list is 12, 38, 41, 22, 67

• compare 38 to 22. Swap them. So, now list is 12, 22, 41, 38, 67

• compare 22 to 67. Leave them.

• After second pass: 12, 22, 41, 38, 67

Sorting

• After second pass: 12, 22, 41, 38, 67

• Now start at third position.

• compare 41 to 38. Swap them. So, now list is 12, 22, 38, 41, 67

• compare 38 to 67. Leave them.

• After third pass: 12, 22, 38, 41, 67

Sorting

• After third pass: 12, 22, 38, 41, 67

• Now start at fourth position.

• compare 41 to 67. Leave them.

• After fourth pass: 12, 22, 38, 41, 67

• Done now.

Sorting (BubbleSort)

• Another algorithm for sorting is the BubbleSort:–Compares consecutive numbers in the list–Swaps them if the two numbers are not in

ascending order–Compare each consecutive pair of numbers in the

first pass.–Next pass, start at first again but go only up until

the next to last element, and so on…–Do n – 1 passes, where n is length of the list

Sorting (BubbleSort)

• Original unsorted: 38, 41, 22, 12, 67

• compare 38 to 41. 38 is not > 41, so leave them in place.

• compare 41 to 22. Swap them. So, now list is 38, 22, 41, 12, 67

• compare 41 to 12. Swap them. So, now list is 38, 22, 12, 41, 67

• compare 41 to 67. Leave them.

• After first pass: 38, 22, 12, 41, 67

Sorting (BubbleSort)

• After first pass: 38, 22, 12, 41, 67

• Now start at first position again.• compare 38 to 22. Swap them. So, now list is

22, 38, 12, 41, 67• compare 38 to 12. Swap them. So, now list is

22, 12, 38, 41, 67• compare 38 to 41. Leave them.• (don't need to compare the 4th and 5th

positions) – why???

• After second pass: 22, 12, 38, 41, 67

Sorting (BubbleSort)

• After second pass: 22, 12, 38, 41, 67

• Now start at first position again.

• compare 22 to 12. Swap them. So, now list is 12, 22, 38, 41, 67

• compare 22 to 38. Leave them.

• After third pass: 12, 22, 38, 41, 67

Sorting (BubbleSort)

• After third pass: 12, 22, 38, 41, 67

• Now start at first position again.

• compare 12 to 22. Leave them.

• Done.

• After fourth pass: 12, 22, 38, 41, 67

Sorting (BubbleSort)

• Here's a graphical bubble sort algorithm.

• http://math.hws.edu/TMCM/java/xSortLab/

• There are many, many more sorting algorithms that are more efficient in that they work by making fewer comparisons. But these should give you a basic idea of sorting and how it could be accomplished.

Michael Eckmann - Skidmore College - CS 106 - Fall 2005

Passing arrays into methods• An array that is passed in as a parameter to a method CAN have its

elements changed and the original array that was passed in reflects those changes.

• Recall: This is different than passing in variables of primitive types. If we pass in a variable of a primitive type, its value will remain the same when the method is complete, even if its corresponding parameter inside the method changes its value.

• Think of it this way: A COPY of a variable of a primitive type, is made when passed into a method. The method works with the copy and this copy is not “passed back out”.

• The value of the memory location of an array is passed into a method call (not the values.) The method works on the actual memory contents of the array (not a copy of the array) and therefore any changes to the values of the array will remain.

Sorting (BubbleSort)

• How might we write BubbleSort in Java? Any ideas?

• We assume the list is stored in an array.• When we said we’d swap two elements of the

list, how could we do this?• What about how to do the correct number of

passes?

Sorting (BubbleSort)

public static void bubbleSort( int array2[] ){ for ( int pass = 1; pass < array2.length; pass++ ) { for ( int element = 0; element < (array2.length - 1); element++ ) { if ( array2[ element ] > array2[ element + 1 ] ) { swap( array2, element, element + 1 ); } // end if } // end inner for loop (element) } // end outer for loop (pass)} // end method bubbleSort

Sorting (BubbleSort)

public static void swap( int array3[], int first, int second ){

int hold;

hold = array3[ first ];

array3[ first ] = array3[ second ];

array3[ second ] = hold;

}

Sorting (BubbleSort)

• An improvement to make this sorting algorithm not check the highest positions that are already sorted during each pass, could be made.

• That is, the first pass goes through the whole array but after this happens the last position of the array is guaranteed to hold the highest value, so it doesn't need to be compared on the second pass (or any future pass.)

• After the second pass, the highest two positions contain the highest two values so they both don't need to be compared on the third pass and so on...

• Why is this an improvement?• What code could we change to implement this improvement?

Searching arrays

• Oftentimes it is necessary to search an array to find if there is a particular value in it.

• Assume we want to search an integer array for a particular value. What would be a good way to write a method to do this?

• Would there be any parameters? If so, what would they be?

• Would we return anything? If so, what would be good to return?

• What would we return if the value was not found?

Searching arrays

• Let's try to implement this method together.

Searching arrays (Linear search)

// Search array for specified key value public static int linearSearch( int array2[], int key ) {

// loop through array elements

for ( int cntr = 0; cntr < array2.length; cntr++ ){

// if array element equals key value, // return location

if ( array2[ cntr ] == key ) return cntr;

}

return -1; // key not found }

Searching arrays (Linear search)

• Let’s analyze the linear search.• We saw that sorting page counted the number of

comparisons and the number of copies (or assignments). Let's consider comparisons only here.

• How long (that is, how many comparisons) does it take to find the value?– What’s the minimum number of comparisons it would take?– What’s the maximum number of comparisons it would take?– Any idea on the average number of comparisons?