Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.

Slides:



Advertisements
Similar presentations
Topic 24 sorting and searching arrays "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be."
Advertisements

Visual C++ Programming: Concepts and Projects
Outline Polymorphic References Polymorphism via Inheritance Polymorphism via Interfaces Sorting Searching Event Processing Revisited File Choosers and.
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
Searching and Sorting Topics  Sequential Search on an Unordered File  Sequential Search on an Ordered File  Binary Search  Bubble Sort  Insertion.
CIS 101: Computer Programming and Problem Solving Lecture 6 Usman Roshan Department of Computer Science NJIT.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
Searching/Sorting Introduction to Computing Science and Programming I.
Searching Arrays Linear search Binary search small arrays
Topic 9B – Array Sorting and Searching
Searching and Sorting Arrays
Searching Arrays. COMP104 Array Sorting & Searching / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and save its.
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.
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.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Value Iteration 0: step 0. Insertion Sort Array index67 Iteration i. Repeatedly swap element i with.
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Chapter 16: Searching, Sorting, and the vector Type.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process.
Searching and Sorting Topics Sequential Search on an Unordered File
Computer Science Searching & Sorting.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
Simple Iterative Sorting Sorting as a means to study data structures and algorithms Historical notes Swapping records Swapping pointers to records Description,
© 2004 Pearson Addison-Wesley. All rights reserved October 19, 2007 Sorting ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
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.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
Sorts Tonga Institute of Higher Education. Introduction - 1 Sorting – The act of ordering data Often, we need to order data.  Example: Order a list of.
Chapter 3 Searching and Selection Algorithms. 2 Chapter Outline Sequential search Binary search List element selection.
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.
Sorting and Searching. Selection Sort  “Search-and-Swap” algorithm 1) Find the smallest element in the array and exchange it with a[0], the first element.
Sorting & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Searching & Sorting.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
1 Principles of Computer Science I Honors Section Note Set 5 CSE 1341.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Unit 2 Day 11 FOCS – Human Computer Interaction. Tower Building Presentation Explain your solution.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Chapter 16: Searching, Sorting, and the vector Type.
Searching/Sorting. Searching Searching is the problem of Looking up a specific item within a collection of items. Searching is the problem of Looking.
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.
CMSC 104, Version 8/061L24Searching&Sorting.ppt Searching and Sorting Topics Sequential Search on an Unordered File Sequential Search on an Ordered File.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Searching algorithms with simple arrays
Searching and Sorting Algorithms
Sorting Data are arranged according to their values.
Bubble, Selection & Insertion sort
Searching and Sorting Topics Sequential Search on an Unordered File
Searching and Sorting Topics Sequential Search on an Unordered File
Sort Techniques.
Sorting Data are arranged according to their values.
Searching and Sorting Arrays
Standard Version of Starting Out with C++, 4th Edition
Searching and Sorting Topics Sequential Search on an Unordered File
Principles of Computing – UFCFA3-30-1
Topic 24 sorting and searching arrays
Searching and Sorting Arrays
Principles of Computing – UFCFA3-30-1
Mod 3 Lesson 2 Me First! Sorting
Presentation transcript:

Searching and Sorting

Topics Linear and Binary Searches Selection Sort Bubble Sort

Objectives At the completion of this topic, students should be able to: Explain the difference between a linear and a binary search Write a linear search routine Write a binary search routine Write a bubble sort routine

Find the person who is 23 years old.

Rules 1.You may only talk to one person at a time 2.You may only ask “How old are you?” 3.The person must respond in years and months

Write down your algorithm

Searching an Array Linear Search Binary Search

Linear Search examScores Problem: Determine which element in the array contains the score 87. int thisOne = -1; for (int index = 0; index < SIZE; index++) { if (examScores[index] == 87) thisOne = index; } …

Binary Search In general, a binary search is much, much faster than a linear search, but requires that the array be sorted. examScores Start in the middle Is this the one you are looking for (87)? If not, is this number smaller than 87? In this case it is. Therefore, we can elminate the entire bottom half of the array. Why? Now try again, picking the middle of the remaining array elements.

Arrange the people in order of age – youngest to oldest

Rules 1.You may only talk to one person at a time 2.You may only ask “How old are you?” 3.Responses will be in years and months. 4.You are only allowed to keep track of two people’s ages at any one time. 5. You may only ask two people to switch places at this time. 6. A person cannot move unless asked to.

Write down your algorithm

Sorting Sorting means to put data into some specified order. There are many, many algorithms that have been developed to sort data. In this section we will mention two of them: Selection Sort Bubble Sort

Selection Sort Algorithm Development

Selection Sort Step one: find the lowest card in the hand

Selection sort Step two: Swap the lowest card with the left-most card left-most card

Selection Sort

Now … Make the second card The left-most card left-most card

Selection Sort Find the lowest card in the remaining cards

Selection Sort left-most card It is already the left-most card, so no swap is required

Selection Sort Left-most card Now make the third card the left-most card

Selection Sort And find the lowest card In the remaining cards

Selection Sort Swap it with the left-most card

Selection Sort

Make the 4 th card the left-most card

Selection Sort Find the lowest card in the remaining cards

Selection Sort The lowest card is the left-most card, so no swap is necessary

Selection Sort Make the next card the Leftmost-card

Selection Sort Lowest remaining card is the left-most card so no swap is necessary

Selection Sort Make the next card the left-most card. It is the last card, so we are done

Activity Diagram High Level View Find lowest card in hand Swap it with the left-most card Make the left-most card the card to the right of the current left-most card Is this the last card? Find the lowest card In the set of cards to the right of the current left- most card end yes no start

Study Lab #24 to see how a Bubble Sort works.