Searching for Data Relationship between searching and sorting Simple linear searching Linear searching of sorted data Searching for string or numeric data.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Zabin Visram Room CS115 CS126 Searching
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
CSE Lecture 3 – Algorithms I
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 9: Searching, Sorting, and Algorithm Analysis
HST 952 Computing for Biomedical Scientists Lecture 9.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Searching Arrays Linear search Binary search small arrays
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
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.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Chapter 8 ARRAYS Continued
Illuminating Computer Science CCIT 4-6Sep
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Chapter 19 Searching, Sorting and Big O
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
Algorithm and Programming Array Dr. Ir. Riri Fitri Sari MM MSc International Class Electrical Engineering Dept University of Indonesia 15 March 2009.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
C Lecture Notes 1 Arrays Lecture 6. C Lecture Notes 2 6.1Introduction Arrays –Structures of related data items –Static entity – same size throughout program.
Simple Iterative Sorting Sorting as a means to study data structures and algorithms Historical notes Swapping records Swapping pointers to records Description,
CSC 211 Data Structures Lecture 13
Symbol Tables and Search Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
1 Lecture 8 Arrays Part II Sorting Arrays Sorting data  Important computing application  Virtually every organization must sort some data Massive.
CHAPTER 8 SEARCHING CSEB324 DATA STRUCTURES & ALGORITHM.
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
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.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
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.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
Visual C++ Programming: Concepts and Projects Chapter 8A: Binary Search (Concepts)
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.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Data Structures Arrays and Lists Part 2 More List Operations.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 8A Binary Search (Concepts)
12. Searching/Sorting Programming in C++ Computer Science Dept Va Tech August, 2000 © Barnette ND, McQuain WD, Keenan MA 1 Simple Searching Many.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays + Functions Outline 6.5Passing Arrays to Functions.
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.
Chapter 9: Sorting and Searching Arrays
Searching CSCE 121 J. Michael Moore.
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Searching.
Searching and Sorting Arrays
Searching CLRS, Sections 9.1 – 9.3.
Presentation transcript:

Searching for Data Relationship between searching and sorting Simple linear searching Linear searching of sorted data Searching for string or numeric data Binary searching of sorted data Search efficiency Stdlib library binary split search function

Relationship between searching and sorting Searching and sorting are closely related operations. To understand why this is so, think about why it is quicker to find someone's telephone number in a phone directory if you already know their name than it would be to find their name if you knew their phone number, e.g. by dialling It would similarly be much more difficult to find a word in a dictionary corresponding to a definition than it is to look up the definition knowing the word. Phone directories and dictionaries are sorted in the order they are because an assumption has been made about the preferred search key. Searching for a key within a sorted list of keys is clearly much easier than searching for a key within an unsorted list.

Simple linear searching 1 When searching an unsorted array, it is not until the last element used has been examined that it is proven that a particular value does not appear in the array. Thus searching must continue either until the required value is found or until all elements of the array in use have been examined.

Simple linear searching 2 Array index starts at 0 WHILE there are more items to search: IF record key at index same as search key: RETURN array index of record key END IF index = index + 1 END WHILE RETURN -1 meaning key not found

Simple linear search 3

Simple linear searching 4

Linear searching of sorted data 1 Using the algorithm above is inefficient if the keys within the array are in ascending order and the key we are searching for isn't present in the array. This is because we continued searching even when it is obvious that the key we are looking for isn't going to be found because we have already compared all keys in the array less than or equal to the search key. It is possible to stop searching once we know the key can't be in the array.

Linear searching of sorted data 2 STRCMP(3) Linux Programmer’s Manual NAME strcmp - compare two strings SYNOPSIS #include int strcmp(const char *s1, const char *s2); DESCRIPTION The strcmp() function compares the two strings s1 and s2. It returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2.

Linear searching of sorted data 3 The integer cmp is given an initial value of -1. So long as the key in the array is less than the search key, cmp will be assigned -1. The search stops, either when the search key is found, or when cmp is assigned +1 because pb[i].name is greater than key.

Searching for string or numeric data 1 In the examples above we were searching for a person's name in the phone book. How would our find function be changed if we were to search for a particular phone extension number stored as an integer instead ? Presumably, the phone book is sorted by name, not by extension number, so we'll have to check all records before knowing an extension isn't present. Warning: it isn't a good idea to search for an exact floating point value due to the probability of decimal to binary conversion rounding errors. You also can't match floating point values in a case statement for the same reason.

Searching for string or numeric data 2

Binary searching of sorted data 1 A sequential search will obviously be very inefficient if a large list of items are to be searched. It is, however, the only solution if the data is unsorted. For sorted data the binary search is more efficient. A binary search is analogous to the way you look up a word in a dictionary or a name in a telephone directory. You might grasp the next paragraph more easily if you first think of a word and then look it up in a dictionary, or look up the telephone number for a friend or business in the phone book, writing down as you carry out your search exactly how you decide where you will make each guess about where in the dictionary or phone book you will next look for the entry.

Binary searching of sorted data 2 The middle element of the list to be searched is compared with the required value, from which it can be determined which half of the list the required value is in. The process is then repeated on the relevant half of the list. Each iteration of the process reduces the number of items still to be searched by a factor of 2, until eventually only one item remains.

Binary searching of sorted data 3 HIGH=highest array index used LOW=lowest array index used WHILE one or more records remain to be searched: MID=(HIGH+LOW)/2 // integer result IF search key < array[MID]: HIGH = MID - 1 IF search key > array[MID]: LOW = MID + 1 END WHILE IF search key is equal to array[MID]: RETURN MID ELSE RETURN -1 // search key is not in array

Binary searching of sorted data 4

Search efficiency 1 The time taken for a search routine to find a key (or to determine that the key is not in the array) will depend on the actual data used. Thus all figures will be average figures assuming randomly distributed data. The time will be proportional to the number of comparisons required which in turn will depend on the number of items to be searched (n). The number of times it is possible to divide n records by 2 using integer division is log 2 n.

Search efficiency 2

Search efficiency 3 Thus the sequential search takes on average n/2 comparisons to find a key; ie 500,000 comparisons to search 1,000,000 items. The binary search takes about 20 comparisons to search the same number of items. Frequently we will simply quote the order of the algorithm; i.e. the sequential search is of order n, which can be written O(n) whilst the binary search is O(log 2 n).

Library binary split search function 1 For a library function to be able to search for any type of value in any type of sorted array, the prototype will have to be general purpose. So void (i.e. untyped) pointers are used to indicate the search key and array base addresses. A custom comparison function also has to be passed into the library binary search function. We'll be looking in detail at how generic (i.e general purpose) functions are designed later in this module. The next 2 slides are from the documention of the stdlib.h library bsearch function. Don't worry if this seems a little bit cryptic !

Library binary split search function 2 BSEARCH(3) Linux Programmer’s Manual NAME bsearch - binary search of a sorted array SYNOPSIS #include void *bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));

Library binary split search function 3 DESCRIPTION The bsearch() function searches an array of nmemb objects, the initial member of which is pointed to by base, for a member that matches the object pointed to by key. The size of each member of the array is specified by size. The contents of the array should be in ascending sorted order according to the comparison function referenced by compar. The compar routine is expected to have two arguments which point to the key object and to an array member, in that order, and should return an integer less than, equal to, or greater than zero if the key object is found, respectively, to be less than, to match, or be greater than the array member. RETURN VALUE The bsearch() function returns a pointer to a matching member of the array, or NULL if no match is found. If there are multiple elements that match the key, the element returned is unspecified.