CS 106 Introduction to Computer Science I 03 / 08 / 2010 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.
CS 206 Introduction to Computer Science II 03 / 23 / 2009 Instructor: Michael Eckmann.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
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 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 12 / 04 / 2006 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.
CS 206 Introduction to Computer Science II 01 / 21 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 24 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 31 / 2008 Happy Halloween!!! Instructor: Michael Eckmann.
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 11 / 04 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 20 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 25 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 23 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 27 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 22 / 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 03 / 30 / 2007 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 04 / 09 / 2008 Instructor: Michael Eckmann.
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 01 / 23 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 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 / 07 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
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.
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
Sorting. Algorithms Sorting reorders the elements in an array or list in either ascending or descending order. Sorting reorders the elements in an array.
BUBBLE SORT. Introduction Bubble sort, also known as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to.
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.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
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.
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 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Sorting Sorting takes an unordered array and makes it an ordered one
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
CS 106 Introduction to Computer Science I 10 / 10 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 Bubblesort compares the numbers in pairs from left to right exchanging.
Sorting Example Bubble Sort
Presentation transcript:

CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Spring 2010 Today’s Topics Comments and/or Questions? Sorting –BubbleSort –Graphical look at other sorting algorithms –Implementation of bubbleSort

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 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. 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 Spring 2010 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 swap( int anArray[], int first, int second )‏ { int hold; hold = anArray[ first ]; anArray[ first ] = anArray[ second ]; anArray[ second ] = hold; }

Sorting (BubbleSort)‏ This is code to do ONE pass: for ( int idx = 0; idx < (list.length - 1); idx++ )‏ { if ( list[ idx ] > list[ idx + 1 ] ) { swap( list, idx, idx + 1 ); } // end if }

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