Searching and Sorting Copyright Prentice Hall (with additions / modifications by Evan Korth)

Slides:



Advertisements
Similar presentations
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 11: Sorting and Searching  Searching Linear Binary  Sorting.
Advertisements

Upcoming schedule F 11/11 hw#4 due F 11/20 midterm #2.
Searching and Sorting Copyright Prentice Hall (with modifications by Evan Korth)
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.
Upcoming schedule F 11/12 hw#4 due F 11/19 hw#5 due F 11/25 midterm #2.
1 Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Outline ArrayList Searching Sorting Copyright © 2012 Pearson Education, Inc.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Chapter 19 Searching, Sorting and Big O
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
SEARCHING (Linear/Binary). Searching Algorithms  method of locating a specific item of information in a larger collection of data.  two popular search.
 2005 Pearson Education, Inc. All rights reserved Searching and Sorting.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Copyright Prentice Hall Modified by Sana odeh, NYU
Searching Arrays Linear search Binary search small arrays
Chapter 23 Sorting Jung Soo (Sue) Lim Cal State LA.
Searching and Sorting Arrays
Chapter 16: Searching, Sorting, and the vector Type
Sections 10.5 – 10.6 Hashing.
16 Searching and Sorting.
Searching and Sorting Algorithms
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
May 17th – Comparison Sorts
COP 3503 FALL 2012 Shayan Javed Lecture 15
Lecture 14 Searching and Sorting Richard Gesick.
Introduction to Search Algorithms
Arrays 2.
Chapter 9: Searching, Sorting, and Algorithm Analysis
Chapter 7 Single-Dimensional Arrays
Teach A level Computing: Algorithms and Data Structures
Algorithms Chapter 3 With Question/Answer Animations
Data Structures and Organization (p.2 – Arrays)
2008/12/03: Lecture 20 CMSC 104, Section 0101 John Y. Park
CSc 110, Spring 2017 Lecture 39: searching.
Searching and Sorting Topics Sequential Search on an Unordered File
Introduction to Programming
Chapter 5 Arrays Introducing Arrays
Searching and Sorting Topics Sequential Search on an Unordered File
Lecture 11 Searching and Sorting Richard Gesick.
Searching and Sorting Arrays
MSIS 655 Advanced Business Applications Programming
Standard Version of Starting Out with C++, 4th Edition
UMBC CMSC 104 – Section 01, Fall 2016
25 Searching and Sorting Many slides modified by Prof. L. Lilien (even many without an explicit message indicating an update). Slides added or modified.
Search,Sort,Recursion.
Searching and Sorting Topics Sequential Search on an Unordered File
Searching CLRS, Sections 9.1 – 9.3.
24 Searching and Sorting.
ITEC 2620M Introduction to Data Structures
Principles of Computing – UFCFA3-30-1
Search,Sort,Recursion.
Searching and Sorting Arrays
CPS120: Introduction to Computer Science
Chapter 23 Searching and Sorting
CPS120: Introduction to Computer Science
Chapter 19 Searching, Sorting and Big O
Principles of Computing – UFCFA3-30-1
Presentation transcript:

Searching and Sorting Copyright Prentice Hall (with additions / modifications by Evan Korth)

Road Map F Search –Linear search –Binary search F Sort –Selection sort –Bubble sort (not covered in the book) F Reading: –Liang 4: chapter 5, 5.7 & 5.8 –Liang 5: chapter 5, 5.6 & 5.7 –Liang 6: chapter 6, 6.7 & 6.8

Searching Arrays Searching is the process of looking for a specific element in an array; for example, discovering whether a certain score is included in a list of scores. Searching, like sorting, is a common task in computer programming. There are many algorithms and data structures devoted to searching. In this section, two commonly used approaches are discussed, linear search and binary search.

Linear Search The linear search approach compares the key element, key, with each element in the array list[]. The method continues to do so until the key matches an element in the list or the list is exhausted without a match being found. If a match is made, the linear search returns the index of the element in the array that matches the key. If no match is found, the search returns -1.

Example 5.10 Testing Linear Search  Objective: Implement and test the linear search method by creating an array of 10 elements of int type randomly and then display this array. Prompt the user to enter a key for testing the linear search. LinearSearchRun html

Searching F Linear Search is not very efficient. F In the worst case, the target we are searching could be placed at the very end of the array, requiring that we search every single element in the array. F On average, the target might be somewhere in the middle of the array, requiring that we search half of all the elements in the array. F For example, if we had a 1000 element array: –worst case: we must search 1000 elements –on average: we search about 500 elements

Binary Search F Binary Search is a more efficient option for searching arrays. F There are two tricks to using Binary Search: –First, the array must be sorted. (Before you use Binary Search, you might first sort the array.) –Second, Binary Search works by dividing the search area in half after each comparison (more on this soon.) F Binary Search is used very commonly in computer science, and there is a related concept called Binary Search Trees. –If you take an Algorithms course, you will definitely spend time researching Binary Search Trees.

Binary Search Pseudo Code 1. Create a sorted array of sample data. 2. Prompt user for a search target. 3. Locate the middle of the array. 4. Compare the middle element with the search target: a)If the search key is equal to the middle element, we have a match! Exit Loop b)If the search key is less than the middle element, search the first half of the array (back to Step 4) c)If the search key is larger than the middle element, search the second half of the array (back to Step 4)

Binary Search Applet F Search / Sort Applets: – ndan/dsal/appldsal.html u Blue: indicates that these elements have been eliminated from the search.

Example 5.11 Testing Binary Search  Objective: Implement and test the binary search method. The program first creates an array of 10 elements of int type. It displays this array and then prompts the user to enter a key for testing binary search. BinarySearchRun

Binary Search, cont.

Binary Search Efficiency F Binary Search is very efficient. F For example, if you have 1024 elements in you array, in the worst case, binary search only requires 10 comparisons. –Linear Search Worst Case: 1024 –Binary Search Worst Case: 10 F If you have 1 billion elements, binary search only requires 30 comparisons! –Linear Search Worst Case: 1 billion –Binary Search Worst Case: 30!

Sorting F Sorting data is one of the most important computing applications. F Examples: –Banks sort all checks by account number in order to prepare individual bank statements. –Telephone companies sort accounts by last name, first name in order to create phone books. F Sorting data has attracted intense research efforts in computer science. F In this lecture, we explore the two of the simplest known sorting algorithms.

Selection Sort Applet F Before examining any code, let us first try out a Java applet for Selection Sort. F The Applet lets you step through each step in the algorithm, so that you can gain an intuitive sense of how it works. F Just go to: –Click the “Start Again” Button to get a new set of random numbers. –Click “Go” to sort. –Click “Step” to step through the algorithm.

Example 5.12: (Cont) Using Arrays in Sorting int[] myList = {2, 9, 5, 4, 8, 1, 6}; // Unsorted Find the largest element in myList and swap it with the last element in myList. 2, 9, 5, 4, 8, 1, 6 => 2, 6, 5, 4, 8, 1, 9 (size = 7) 2, 6, 5, 4, 8, 1 => 2, 6, 5, 4, 1, 8 (size = 6) 2, 6, 5, 4, 1 => 2, 1, 5, 4, 6 (size = 5) 2, 1, 5, 4 => 2, 1, 4, 5 2, 1, 4 => 2, 1, 4, 2, 1 => 1, 2 Sort it to produce 1, 2, 4, 5, 6, 8, 9

Example 5.12 Using Arrays in Sorting  Objective: Use the selectionSort method to write a program that will sort a list of double floating-point numbers. int[] myList = {2, 9, 5, 4, 8, 1, 6}; // Unsorted Sort it to produce 1, 2, 4, 5, 6, 8, 9 2, 9, 5, 4, 8, 1, 6 SelectionSortRun

Bubble Sort F Bubble sort is another option for sorting a list of numbers. F It’s called bubble sort because –larger values gradually “bubble” their way upward to the end of the array like air bubbles rising in water. F The technique is to make several passes through the array. –On each pass, pairs of elements are compared. –If the pair is in increasing order, we leave the values as they are. –If the pair is in decreasing order, we swap the values.

Bubble Sort Applet F Before examining any code, let us first try out a Java applet for Bubble Sort. F The Applet lets you step through each step in the algorithm, so that you can gain an intuitive sense of how it works. F Just go to: –Click the “Start Again” Button to get a new set of random numbers. –Click “Go” to sort. –Click “Step” to step through the algorithm.

Advantages / Disadvantages of Bubble Sort and Selection Sort F The main advantage of these two sorting algorithms is that it is easy to program. F The main disadvantage is that it is very slow. F Much work in computer science has been geared towards creating more efficient sorting algorithms. F For example: –Merge Sort –Bi-directional Bubble Sort –Quick Sort F For another quick demo, check out: plets/SortDemo/example1.html