Ordered Arrays An array is ordered if the elements are in ascending or descending order. The array may be ordered numerically or alphabetically (which.

Slides:



Advertisements
Similar presentations
Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Advertisements

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
HST 952 Computing for Biomedical Scientists Lecture 9.
Programming Searching Arrays. COMP102 Prog. Fundamentals: Searching Arrays/ Slide 2 Copyright © 2000 by Broks/Cole Publishing Company A division of International.
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the.
Chapter 8 Search and Sort Asserting Java ©Rick Mercer.
Searches & Sorts V Deena Engel’s class Adapted from W. Savitch’s text An Introduction to Computers & Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching.
Searching Arrays Linear search Binary search small arrays
C++ for Engineers and Scientists Third Edition
Searching Arrays. COMP104 Array Sorting & Searching / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and save its.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Week 5 - Monday.  What did we talk about last time?  Linked list implementations  Stacks  Queues.
Computer Science Searching & Sorting.
Searching and Sorting Chapter Sorting Arrays.
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
COMP102 Lab 121 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
C# PROGRAMMING Searching & Sorting. Objective/Essential Standard Essential Standard 3.00 Apply Advanced Properties of Arrays Indicator 3.03 Apply procedures.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
Array (continue).
Arrays The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with.
CSC 211 Data Structures Lecture 13
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
1 Lecture 8 Arrays Part II Sorting Arrays Sorting data  Important computing application  Virtually every organization must sort some data Massive.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Chapter 8 Search and Sort ©Rick Mercer. Outline Understand how binary search finds elements more quickly than sequential search Sort array elements Implement.
Review 1 Arrays & Strings Array Array Elements Accessing array elements Declaring an array Initializing an array Two-dimensional Array Array of Structure.
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.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
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.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University.
An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.
1 Applied Arrays Lists and Strings Chapter 12 2 Applying What You Learn Searching through arrays efficiently Sorting arrays Using character arrays as.
Dr. Sajib Datta CSE 1320 Arrays, Search and Sort.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Sorting and Searching Bubble Sort Linear Search Binary Search.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
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.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
Chapter 19: Recursion.
Data Structures I (CPCS-204)
Arrays … The Sequel Applications and Extensions
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Chapter 8 Search and Sort
Searching and Sorting Arrays
Standard Version of Starting Out with C++, 4th Edition
25 Searching and Sorting Many slides modified by Prof. L. Lilien (even many without an explicit message indicating an update). Slides added or modified.
24 Searching and Sorting.
Searching and Sorting Arrays
Presentation transcript:

Ordered Arrays An array is ordered if the elements are in ascending or descending order. The array may be ordered numerically or alphabetically (which to the computer is the same as numerically since characters are stored internally as numeric values.) Searching of an item becomes easy in an ordered array Addition of a new item in an ordered array is an expensive operation.

Example – searching for a given name in an ordered array.

Using Part of an Array For some applications, we do not know in advance how many elements are to be placed in the array. we must dimension the array large enough to handle all reasonable contingencies. E.g. if array is to hold exam marks for class sizes between 12 and 100 students, we use an upper bound of 100. In such cases, at runtime, we must employ a counter variableto keep track of the actual number of elements actually stored in the array during any run of the program.

Search Algorithms Linear Search – Search iteratively, start from the first item – Stop the search If an item is found If item under inspection is greater that the searched item Whole array is searched

Binary Search 1.Guess a number, normally a number in the mid of the range of interest is selected 2.If the number is too high search in the lower half otherwise the higher half. 3.Go to step 1 until the number is found or you are working with the range n-n and n is not the number.

Working Example of Binary Search Step NumberNumber GuessResultRange of possible values Too High Too Low Too High Too Low Too High Too Low Correct Try to guess number 33 in the range using binary search

Merging Two Ordered Arrays To consolidatetwo lists into a single ordered list: 1. Compare the two names at the top of the first and second lists. a)If one name alphabetically precedes the other, copy it onto the third list and cross it off its original list. b)If the names are the same, copy the name onto the third list and cross out the name from the first and second lists. 2.Repeat Step 1 with the current top names until you reach the end of either list. 3.Copy the names from the remaining list onto the third list.

Passing Arrays to Procedures An array declared in a procedure is local to that procedure. An entire array can be passed to a Function as an argument.

Passing an Array Element A single element of an array can be passed to a procedure just like any ordinary numeric or string variable. int main() int num[20]; num[5] = 10; cout<< Triple(num[5]) <<endl; return 0; } int Triple(int x) { return (3 * x); }