CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.

Slides:



Advertisements
Similar presentations
CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
Advertisements

Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
CS 206 Introduction to Computer Science II 02 / 27 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 29 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 02 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 04 / 28 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 04 / 27 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 12 / 09 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 12 / 05 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 23 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 12 / 03 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
CS 1400 March 30, 2007 Chapter 8 Searching and Sorting.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 09 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
J. Michael Moore Searching & Sorting CSCE 110. J. Michael Moore Searching with Linear Search Many times, it is necessary to search an array to find a.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Searching Arrays Linear search Binary search small arrays
Analysis of Algorithm.
CS 106 Introduction to Computer Science I 04 / 28 / 2010 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 08 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 12 / 08 / 2008 Instructor: Michael Eckmann.
1 Sorting/Searching and File I/O Sorting Searching Reading for this lecture: L&L
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 04 / 06 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 25 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Chapter 8 ARRAYS Continued
Searching Algorithms. The Search Problem Problem Statement: Given a set of data e.g., int [] arr = {10, 2, 7, 9, 7, 4}; and a particular value, e.g.,
CS 106 Introduction to Computer Science I 03 / 19 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann.
4/15: Searching Arrays Look at BubbleSort.java Searching arrays: the linear search Searching arrays: the binary search.
Array Processing - 2. Objectives Demonstrate a swap. Demonstrate a linear search of an unsorted array Demonstrate how to search an array for a high value.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 19 Thanks for Lecture Slides:
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 05 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann.
1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.
1 Lecture 8 Arrays Part II Sorting Arrays Sorting data  Important computing application  Virtually every organization must sort some data Massive.
CS 206 Introduction to Computer Science II 04 / 22 / 2009 Instructor: Michael Eckmann.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
CS 106 Introduction to Computer Science I 10 / 29 / 2007 Instructor: Michael Eckmann.
Arrays An array is a data object that can hold multiple objects, all of the same type. We can think of an array as a storage box which has multiple compartments.
Array Search & Sort (continues). On the fly questions Array declaration: int[] a, b, c; 1. a is an array of integers, b and c are two integers 2. a, b,
SORTING ALGORITHMS King Saud University College of Applied studies and Community Service CSC 1101 By: Nada Alhirabi 1.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 10 / 2008 Instructor: Michael Eckmann.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Searching Arrays Linear search Binary search small arrays
Recitation 13 Searching and Sorting.
Chapter 23 Searching and Sorting
Module 8 – Searching & Sorting Algorithms
Presentation transcript:

CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Spring 2008 Today’s Topics Comments and/or Questions? sorting –bubbleSort searching arrays –linear search –binary search

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

Sorting (BubbleSort)‏ public static void swap( int anArray[], int first, int second )‏ { int hold; hold = anArray[ first ]; anArray[ first ] = anArray[ second ]; anArray[ second ] = hold; }

Sorting (BubbleSort)‏ An improvement to make this sorting algorithm to 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 list[], int key ) { // loop through array elements for ( int cntr = 0; cntr < list.length; cntr++ )‏ { // if array element equals key value, // return location if ( list[ 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 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? Yes --- how might you look up a person in the phone book?

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?