CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.

Slides:



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

Bubble Sort Algorithm 1.Initialize the size of the list to be sorted to be the actual size of the list. 2.Loop through the list until no element needs.
CS 206 Introduction to Computer Science II 02 / 27 / 2009 Instructor: Michael Eckmann.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
CS 206 Introduction to Computer Science II 03 / 23 / 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 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 11 / 2006 Instructor: Michael Eckmann.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 11: Sorting and Searching  Searching Linear Binary  Sorting.
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
Chapter 11 Sorting and Searching. Topics Searching –Linear –Binary Sorting –Selection Sort –Bubble Sort.
Simple Sorting Algorithms
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
CS 330 Programming Languages 10 / 11 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 20 / 2008 Instructor: Michael Eckmann.
Searching and Sorting SLA Computer Science 4/16/08 Allison Mishkin.
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 10 / 04 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 16 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 08 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 20 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 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.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
CS 206 Introduction to Computer Science II 10 / 08 / 2008 Instructor: Michael Eckmann.
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.
Simple Sort Algorithms Selection Sort Bubble Sort Insertion Sort.
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.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 19 Thanks for Lecture Slides:
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
CS 330 Programming Languages 10 / 07 / 2008 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.
CS 206 Introduction to Computer Science II 04 / 22 / 2009 Instructor: Michael Eckmann.
12. Sorting Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Sorting Many computer applications.
The Bubble Sort by Mr. Dave Clausen La Cañada High School.
CS 106 Introduction to Computer Science I 10 / 29 / 2007 Instructor: Michael Eckmann.
Sorting an array bubble and selection sorts. Sorting An arrangement or permutation of data An arrangement or permutation of data May be either: May be.
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.
SORTING ALGORITHMS King Saud University College of Applied studies and Community Service CSC 1101 By: Nada Alhirabi 1.
CS 206 Introduction to Computer Science II 10 / 10 / 2008 Instructor: Michael Eckmann.
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.
12. Searching/Sorting Programming in C++ Computer Science Dept Va Tech August, 2000 © Barnette ND, McQuain WD, Keenan MA 1 Simple Searching Many.
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.
Recitation 13 Searching and Sorting.
Introduction to Programming
Module 8 – Searching & Sorting Algorithms
Presentation transcript:

CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Spring 2007 Today’s Topics questions, comments? Sorting algorithms

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 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 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) How might we write this in Java? 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) Here's a graphical bubble sort algorithm. 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. Now, let's take a look at the code in the book to sort an array using a bubble sort algorithm.

Michael Eckmann - Skidmore College - CS Spring 2007 Passing arrays into methods It is extremely important to note that an array that is passed in as a parameter to a method CAN have its elements change and the original array that was passed in reflects those changes. 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.)

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