CS 106 Introduction to Computer Science I

Similar documents
CS 106 Introduction to Computer Science I

CS 206 Introduction to Computer Science II

C/C++ Programming Lecture 18 Name:

CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II

Searching & Sorting in Java Bubble Sort

CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II

Objectives. Chapter 23 Sorting. Why study sorting? What data to sort? Insertion Sort. CS1: Java Programming Colorado State University

CS 106 Introduction to Computer Science I

8/2/10. Looking for something COMP 10 EXPLORING COMPUTER SCIENCE. Where is the book Modern Interiors? Lecture 7 Searching and Sorting TODAY'S OUTLINE

CSCI 261 Computer Science II

CS 206 Introduction to Computer Science II

Searching for Information. A Simple Method for Searching. Simple Searching. Class #21: Searching/Sorting I

CS 206 Introduction to Computer Science II

CS 106 Introduction to Computer Science I

LINEAR INSERTION SORT

CS 206 Introduction to Computer Science II

CSCI 136 Data Structures & Advanced Programming. Lecture 12 Fall 2018 Profs Bill & Jon

Sorting/Searching and File I/O. Sorting Searching Reading for this lecture: L&L

CS 106 Introduction to Computer Science I

LECTURE 17. Array Searching and Sorting

CS 106 Introduction to Computer Science I

Sorting Algorithms. Array Data is being arranged in ascending order using the bubble sort algorithm. #1 #2 #3 #4 #5 #6 #7

What is an algorithm? CISC 1100/1400 Structures of Comp. Sci./Discrete Structures Chapter 8 Algorithms. Applications of algorithms

CS Sorting Terms & Definitions. Comparing Sorting Algorithms. Bubble Sort. Bubble Sort: Graphical Trace

12/1/2016. Sorting. Savitch Chapter 7.4. Why sort. Easier to search (binary search) Sorting used as a step in many algorithms

Programming II (CS300)

CS 206 Introduction to Computer Science II

public static <E extends Comparable<? super E>>void BubbleSort(E[] array )

Objectives of the lesson

CISC 1100: Structures of Computer Science

The University Of Michigan. EECS402 Lecture 07. Andrew M. Morgan. Sorting Arrays. Element Order Of Arrays

So on the survey, someone mentioned they wanted to work on heaps, and someone else mentioned they wanted to work on balanced binary search trees.

Chapter 10 - Notes Applications of Arrays

ECE 122. Engineering Problem Solving Using Java

Algorithm Analysis. Performance Factors

Sorting: Overview/Questions

Search,Sort,Recursion

CSE101-lec#19. Array searching and sorting techniques. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

Sorting and Searching

CS 106 Introduction to Computer Science I

ECE 242 Data Structures and Algorithms. Simple Sorting I. Lecture 4. Prof.

CS 206 Introduction to Computer Science II

Introduction to Computer Science Midterm 3 Fall, Points

Chapter 8 Search and Sort

Using pointers with functions

Heap sort. Carlos Moreno uwaterloo.ca EIT

ECE 242 Fall 13 Exam I Profs. Wolf and Tessier

Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc.

CS 163 Practice Final Exam Winter 2012

STUDENT LESSON A17 Quadratic Sorting Algorithms

Sorting and Searching Algorithms

Programming in OOP/C++

C Arrays. Group of consecutive memory locations Same name and type. Array name + position number. Array elements are like normal variables

Topics. Sorting. Sorting. 1) How can we sort data in an array? a) Selection Sort b) Insertion Sort

What is sorting? Lecture 36: How can computation sort data in order for you? Why is sorting important? What is sorting? 11/30/10

Arrays. Week 4. Assylbek Jumagaliyev

CS 206 Introduction to Computer Science II

CSE 2123 Sorting. Jeremy Morris

Lecture 7: Searching and Sorting Algorithms

CS 106 Introduction to Computer Science I

SCJ2013 Data Structure & Algorithms. Bubble Sort. Nor Bahiah Hj Ahmad & Dayang Norhayati A. Jawawi

Algorithm for siftdown(int currentposition) while true (infinite loop) do if the currentposition has NO children then return

Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi

(Refer Slide Time: 01.26)

Introduction. Sorting. Definitions and Terminology: Program efficiency. Sorting Algorithm Analysis. 13. Sorting. 13. Sorting.

Chapter 10. Sorting and Searching Algorithms. Fall 2017 CISC2200 Yanjun Li 1. Sorting. Given a set (container) of n elements

CS 206 Introduction to Computer Science II

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program)

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program) A few types

Sorting Pearson Education, Inc. All rights reserved.

Fundamental problem in computing science. putting a collection of items in order. Often used as part of another algorithm

CS 103 Unit 8b Slides

Objectives of This Chapter

Topic 14 Searching and Simple Sorts

Arrays a kind of data structure that can store a fixedsize sequential collection of elements of the same type. An array is used to store a collection

8.1. Chapter 8: Introduction to Search Algorithms. Linear Search. Linear Search. Linear Search - Example 8/23/2014. Introduction to Search Algorithms

O(n): printing a list of n items to the screen, looking at each item once.

Most of the class will focus on if/else statements and the logical statements ("conditionals") that are used to build them. Then I'll go over a few

Recursion: The Beginning

CS125 : Introduction to Computer Science. Lecture Notes #38 and #39 Quicksort. c 2005, 2003, 2002, 2000 Jason Zych

To become familiar with array manipulation, searching, and sorting.

Analysis of Algorithms. CS 1037a Topic 13

Introduction. two of the most fundamental concepts in computer science are, given an array of values:

3/20/2015. Chapter 19 Sorting and Searching SELECTION SORT INSERTION SORT MERGE SORT THE QUICKSORT ALGORITHM MERGE SORT VS SELECTION SORT

Searching & Sorting in Java Shell Sort

CS 115 Exam 3, Fall 2009

Chapter Contents. An Introduction to Sorting. Selection Sort. Selection Sort. Selection Sort. Iterative Selection Sort. Chapter 9

SORTING AND SEARCHING

Heap sort. Carlos Moreno uwaterloo.ca EIT

Search Lesson Outline

Outline. What s wrong with Enumerations? Indexing Searching Self-Organizing Vectors Binary Searching Sorting Command line Arguments.

LECTURE 08 SEARCHING AND SORTING ARRAYS

QUIZ. 0] Define arrays 1] Define records 2] How are arrays and records: (a) similar? (b) different?

Searching, Sorting. Arizona State University 1

C How to Program, 7/e by Pearson Education, Inc. All Rights Reserved.

Outline. 7.1 Introduction. 7.2 Arrays. 7.2 Arrays

C++ PROGRAMMING SKILLS Part 4: Arrays

C Arrays Pearson Education, Inc. All rights reserved.

Transcription:

CS 106 Introduction to Computer Science I 06 / 11 / 2015 Instructor: Michael Eckmann

Today s Topics Comments and/or Questions? Sorting Searching Michael Eckmann - Skidmore College - CS 106 - Summer 2015

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 3 rd 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: Repeatedly compares two numbers that border each other in the list Swaps them if the two numbers are not in ascending order Compare each bordering 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.

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. On the other hand, the value of the memory location of an array is passed into a method call (not the values.) The method looks to that location and works on the contents of the array (not a copy of the array) and therefore any changes to the values of the array will remain. Michael Eckmann - Skidmore College - CS 106 - Summer 2015

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 idx1, int idx2 ) { int hold; hold = anarray[ idx1 ]; anarray[ idx1 ] = anarray[ idx2 ]; anarray[ idx2 ] = 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 ] ) { } // end if swap( list, idx, idx + 1 );

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?

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.

// 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 Binary Search // 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?