33
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann

CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann

  • View
    219

  • Download
    0

Embed Size (px)

Citation preview

CS 106Introduction to Computer Science I

10 / 16 / 2006

Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 106 - Fall 2006

Today’s Topics• Comments and/or Questions?• sorting in general

– bubble sort

– examples of other sorting algorithms

• searching

– linear search

– binary search

Sorting

• Sorting is a large topic in some computer science courses. • We'll discuss what sorting is and at least two

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 and take it out

– 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 number in the first position to all the other

numbers in the list and swap them in place if the number in the first position is greater than the number in the other position.

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

– third pass does the same but with the 3rd number.

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

Searching arrays

• Can we do better? (That is, can we guarantee less comparisons on average to find a particular value?)

Searching arrays

• If the array was sorted could we modify the linear search in any way to stop sooner if we don't find the key?

• How might we implement the change to the Java code implementation of linear search?

Binary Search

• Now let's think beyond linear search.

• First question: Has anyone heard of binary search?

• What if we knew the array was sorted in ascending order?

• Could we use that to our advantage to reduce the number of comparisons a search algorithm would do on average?

Binary Search• We could compare to the middle element of the array.

• If it is equal to the middle element, we’re done.

• If it is less than the middle element, where would we now concentrate our search?

• If it is greater than the middle element, where would we now concentrate our search?

Binary Search

• Any idea how we might write code to implement this algorithm?

Binary Search• Any idea how we might write code to implement this

algorithm?

• Let's discuss some ideas before we get right to the code.– What parameters might our method have?– What element to compare to first?

• How do we calculate that index?– How do we determine what part of the array to now do

a search?

• Let's take a look at an implementation and do an example call.

Binary Search // method to perform binary search of an array

public static int binarySearch( int array2[], int key )

{

int low = 0; // low element subscript

int high = array2.length - 1; // high element subscript

int middle; // middle element subscript

// loop until low subscript is greater than high subscript

while ( low <= high )

{

// determine middle element subscript

middle = ( low + high ) / 2;

// if key matches middle element, return middle location

if ( key == array2[ middle ] )

return middle;

// if key less than middle element, set new high element

else if ( key < array2[ middle ] )

high = middle - 1;

// key greater than middle element, set new low element

else

low = middle + 1;

} // end while loop

return -1; // key not found

} // end method binarySearch

Searching arrays (Binary search)

• Let’s analyze the binary search.• To simplify the discussion, we can count the 2

comparisons in the if/else/if/else together to be 1 comparison.

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