4. SEARCHING AND SORTING LINEAR SEARCH

Size: px
Start display at page:

Download "4. SEARCHING AND SORTING LINEAR SEARCH"

Transcription

1 4. SEARCHING AND SORTING SEARCHING Searching and sorting are fundamental operations in computer science. Searching refers to the operation of finding the location of a given item in a collection of items. Sorting refers to the operations of arranging data in some given order, such as increasing or decreasing, with numerical data, or alphabetically, with character data. Searching : Searching is an operation which finds the location of a given element in a list. The search is said to be successful or unsuccessful depending on whether the element that is to be searched is found or not. The two standard searching techniques are: Linear search Binary search. LINEAR SEARCH Linear search is the simplest method of searching. In this method, the element to be found is sequentially searched in the list (Hence also called sequential search). This method can be applied to a sorted or an unsorted list. Hence, it is used when the records are not stored in order. Principle : The algorithm starts its search with the first available record and proceeds to the next available record repeatedly until the required data item to be searched for is found or the end of the list is reached. Algorithm : ALGORITHM LINEARSEARCH(K, N, X ) // K is the array containing the list of data items // N is the number of data items in the list // X is the data item to be searched Repeat For I = 0 to N -1 Step 1 If K( I ) = X Then WRITE( ELEMENT IS PRESENT AT LOCATION I) QUIT Else I I+1 End If End Repeat WRITE( ELEMENT NOT PRESENT IN THE COLLECTION ) End LINEARSEARCH Page 1

2 In the above algorithm, K is the list of data items containing N data items. X is the data item, which is to be searched in K. If the data item to be searched is found then the position where it is found will be displayed. If the data item to be searched is not found then the appropriate message will be displayed to indicate the user, that the data item is not found. The data item X is compared with each and every element in the list K During this comparison, if X matches with a data item in K, then the position where the data item was found will gets displayed and the control comes out of the loop and the procedure comes to an end. If X does not match with any of the data items in K, then finally the element not found will be displayed. Example: X Number to be searched : 40 i = 0 i =1 i = 2 i = 3 i = 4 i = 5 i = 6 i = 7 i = 8 i = 9 X K[0] X K[1] X K[2] X K[3] X K[4] X K[5] X K[6] X = K[7] I = 7 : Number found at location 7 i.e., as a 8 th element Page 2

3 Program: //Linear Search #include <stdio.h> #include <conio.h> int k[10],n=10; search(int x) { for (int i=0; i<n; i++) { if (k[i]==x){ printf( Element present at location %d, i); exit(0); printf( \nelement not found in the collection ); void main() { clrscr(); int i,x; printf( \nenter the elements ); for (i=0; i<10; i++) scanf( %d,.&k[i]); printf( \nenter the element that you want to search ); scanf( %d,&x); search(x); The search( ) function gets the number to be searched in the variable x as a argument and compares it with each and every element in the array K. If the number x is found in the array, then the position i, where it is found will gets printed. If the number is not found in the entire list, then the function will display the not found message to the user. In the main( ) function receives the n values from the user and stored in the array K. The user is prompted to enter the number to be searched and is passed to the search( ) function as a argument. The search which receives the value x will give the appropriate message. Advantages: 1. Simple and straight forward method. 2. Can be applied on both sorted and unsorted list. Disadvantages: 1. Inefficient when the number of data items in the list increases. Page 3

4 BINARY SEARCH Binary search method is very fast and efficient. This method requires that the list of elements be in sorted order. Binary search cannot be applied on an unsorted list. Principle: The data item to be searched is compared with the approximate middle entry of the list. If it matches with the middle entry, then the position will be displayed. If the data item to be searched is lesser than the middle entry, then it is compared with the middle entry of the first half of the list and procedure is repeated on the first half until the required item is found. If the data item is greater than the middle entry, then it is compared with the middle entry of the second half of the list and procedure is repeated on the second half until the required item is found. This process continues until the desired number is found or the search interval becomes empty. Algorithm: ALGORITHM BINARYSEARCH(K, N, X) // K is the array containing the list of data items // N is the number of data items in the list // X is the data item to be searched Lower 0, Upper N 1 While Lower Upper Mid ( Lower + Upper ) / 2 If (X < K[Mid])Then Upper Mid -1 Else If (X>K[Mid]) Then Lower Mid + 1 Else Write( ELEMENT FOUND AT, MID) Quit End If End If End While Write( ELEMENT NOT PRESENT IN THE COLLECTION ) End BINARYSEARCH In Binary Search algorithm given above, K is the list of data items containing N data items. X is the data item, which is to be searched in K. If the data item to be searched is found then the position where it is found will be printed. If the data item to be searched is not found then Element Not Found message will be printed, which will indicate the user, that the data item is not found. Initially lower is assumed 0 to point the first element in the list and upper is assumed as N-1 to point the last element in the list because the range of any array is 0 to N-1. The mid position of the list is calculated by finding the average between lower and upper and X is Page 4

5 compared with K[mid]. If X is found equal to K[mid] then the value mid will gets printed, the control comes out of the loop and the procedure comes to an end. If X is found lesser than K[mid], then upper is assigned mid 1, to search only in the first half of the list. If X is found greater than K[mid], then lower is assigned mid + 1, to search only in the second half of the list. This process is continued until the element searched is found or the collection becomes becomes empty. Example: X Number to be searched : 40 U Upper L Lower=N-1 M Mid i = 0 i =1 i = 2 i = 3 i = 4 i = 5 i = 6 i = 7 i = 8 i = L = 0 M = (0+9)/2 =4 U = 9 X< K[4] U = 4 1 = L = 0 M = (0+3)/2=1 U = 3 X > K[1] L = = L, M = 2 U = 3 K > A [2] L = = L, M, U = 3 K = A[3] P = 3 : Number found at position 3 Page 5

6 Program: #include<stdio.h> #include<conio.h> int K[10],N=10; binarysearch(int x) { int mid, lower=0, upper=n-1; while (lower<=upper) { mid=(lower+upper)/2; if (K[mid]>X) upper=mid-1; else if(k[mid]<x) lower=mid+1; else Write( Element Found at location, MID); Quit Write( Element not found in the collection ) main(){ int i,x; printf( \nenter the elements ); for(i=0;i<n;i++) scanf( %d,&k[i]); printf( \nenter the element to search ); scanf( %d,&x); binarysearch(x); The binarysearch( ) function gets the element to be searched in the variable X. Initially lower is assigned 0 and upper is assumed N 1. The mid position is calculated and if K[mid] is found equal to X, then mid position will gets displayed. If X is less than K[mid] upper is assigned mid 1 to search only in first half of the list else lower is assigned mid + 1 to search only in the second half of the list. This is process is continued until lower is less than or equal to upper. If the element is not found even after the loop is completed, then the Not Found Message will be displayed to the user indicating that the element is not found. Advantages: 1. Searches several times faster than the linear search. 2. In each iteration, it reduces the number of elements to be searched from n to n/2. Disadvantages: 1. Binary search can be applied only on a sorted list. Page 6

7 HASH SEARCH Hash search is one of the fastest searching mechanisms used to locate the given element in the collection. This searching method uses the concept called Hashing to get the address of the location in the table to store/retrieve the element. Hashing is the process of applying the hashing function to the given value to get the address of the location for its storage or retrieval. There are so many hashing techniques which can be used or a user defined function may also be used. These functions may do some arithmetic manipulation on the given value and return the result. The result will be treated as an address for the given element. Some of the hashing functions are 1. Division method 2. Mid Square Method 3. Folding method The table where the values get stored is called Hash Table. The Hash table is the collection of several buckets say HT(0),HT(1). Each bucket has the capacity to hold any number of values which will be decided by the designer of the table. The buckets may have the respective number of slots and each slot being large enough to hold one element. If the bucket size=1 then each bucket can hold only one element. Insert X into Hash table: The given value X which has to be inserted into the hash table will be given as an input to the hashing function. The function will return the address for its storage. While storing the record/element, if the address calculated is the one which already contain some data, the collision said to be occurred. In order to overcome this problem any of the collision resolution techniques will be used. One of the most widely used techniques is linear probing. The sequential search is made in the linear probing to find out the next empty location & the value gets stored there. If all the locations are occupied by some elements then overflow is said to be occurred. Algorithm: ALGORITHM INSERTHT(HT, N, X) // HT is the Hash table // N is the number of buckets // X is the data item to be insert Begin I 1 Y X MOD N While(I<=N) Begin IF (HT(Y)= ) THEN HT(Y) X Quit Else Y (Y+1)MOD N I I+1 End Write( Table is Full ) End Page 7

8 In the above algorithm HT is the Hash table which has been initialized with a special value say. The algorithm receives three values as arguments. The given value X will be given to hashing function to get the address for its storage. The search is made in the location Y of the hash table HT. If the specified location is empty then it will be stored. The search for the next empty location will be made if the location is having some element with it. Search X in the Hash table: The value X will be given to the hashing function to get the address for the search. If the corresponding location of the hash table contains the value X means the successful search message will be displayed. The search will be made on the remaining buckets if the first search gets failed. The Unsuccessful search message will be given to the user if the element cannot be found in the remaining N-1 buckets. Algorithm: ALGORITHM SEARCHHT(HT, N, X) // HT is the Hash table // N is the number of buckets // X is the data item to be search Begin I 1 Y X MOD N While(I<=N) Begin IF (HT(Y)=X) THEN Write( X IS PRESENT ) Quit Else Y (Y+1)MOD N I I+1 End Write( X IS ABSENT ) End Page 8

9 INDEXED SEQUENTIAL SEARCH Indexed Sequential Files: The sequential file structures are inefficient during the searching process. But all the data base activities involve searching of a particular record and then doing some process over it. But in case of sequential files, the delay in retrieving the records is so high particularly for those which are stored at the end of the files. Hence to overcome this difficulty, a technique called indexing is adopted where the records are stored as blocks in the memory and the highest key value of each block is stored in an index file. Whenever a record is to be searched, its key value is compared with the index file and the control is taken directly to the particular memory block where the record is present. This eliminates the need of searching through all the records which are stored in other memory blocks before that. Now the sequential search is done only within the block where the record is present whereas the block is directly accessed using the index value. This greatly reduces the number of comparisons before searching a particular record. An index file consists of three areas. They are Prime area Index area Overflow area The prime area is where the records of the file are stored when the sequential file is initially created. Consider that the external storage device is a hard disk which consists of many cylinders. Each cylinder in a hard disk consist so many tracks and sectors. The prime area is starts from track 1 leaving the track 0 empty. All the records in the file are stored in the prime area starting from track 1. At the end of the prime area in every cylinder, some extra space is left for accommodating extra records which may come in the future. This area is called the overflow area. This is used when in each cylinder there is an overflow of records after the prime area is completely filled. In some cases, there is a master overflow area which is used to hold the overflow of records when all the overflow areas are also filled up. The third important area in an indexed sequential file is called index area which occupies track 0 in every cylinder. The highest key value in every track of the prime area is stored in the index area along with the track number. Thus the index area maintains the index of records in stored in all the tracks of a cylinder. Hence this type of index is also called track index. There is another index maintained for the track indexes of all cylinders. This index stores the highest key value of each track index along with the cylinder number and this type of index is called the cylinder index. Some type of file systems even maintains an index called master index, where the highest key value of each row in the cylinder index is stored along with the row number. This makes the searching even more efficient. Page 9

10 INDEXED SEQUENTIAL ACCESS METHOD The file structure described above is uses a method called indexed sequential access method to search for a given record. Given a key, the program first compares the key value with the values in the entries of the master index. If the key value is greater than the entry in the master index, then the comparison is made with the next cell entry. If the key value is found to be lesser than the value present in the cell of the master index, then the row number corresponding to that is retrieved and the control taken to that particular row directly in the cylinder index. Now the key value is again compared with each and every value in the cylinder index and stops when a greater value is found. After a greater value than the key value is found in the cylinder index, the cylinder number corresponding to that is retrieved and the control is directly taken to that particular cylinder. Now the key value is compared with each and every value in the track index and when a greater value is found, the corresponding track number is retrieved and control is taken to that track directly. This is the lowest level in the hierarchy and now a sequential search is made for the key value within this track alone. If the record matching to this key value is found then, the record is retrieved and loaded to the memory other wise record not found error is displayed. Page 10

11 In the above example, a key value is 923 is given and is searched using ISAM technique. First the key value is compared with the values in the master index. Since the first value in the master index (4000) is greater than 923, the control is transferred to the row one in the cylinder index. Now, 923 is compared with each value in the row 1 of cylinder index. The first value in the row 1 of cylinder index (1000) is greater than 923. Hence the control is transferred to track 10 where 923 is sequentially searched and retrieved. In this case the number of comparisons required to search for the key value 923 is 23 in track in track index + 1 in row 1 of cylinder index + 1 in master index = 35 comparisons. But if the same data is stored as ordinary sequential file, then the number of comparisons that would have taken to find out the record 923 is 923 comparisons. This shows that the ISAM has drastically reduced the searching time when compared to the ordinary sequential file. Page 11

DATA STRUCTURES/UNIT 3

DATA STRUCTURES/UNIT 3 UNIT III SORTING AND SEARCHING 9 General Background Exchange sorts Selection and Tree Sorting Insertion Sorts Merge and Radix Sorts Basic Search Techniques Tree Searching General Search Trees- Hashing.

More information

Hash Table and Hashing

Hash Table and Hashing Hash Table and Hashing The tree structures discussed so far assume that we can only work with the input keys by comparing them. No other operation is considered. In practice, it is often true that an input

More information

Sorting & Searching. Hours: 10. Marks: 16

Sorting & Searching. Hours: 10. Marks: 16 Sorting & Searching CONTENTS 2.1 Sorting Techniques 1. Introduction 2. Selection sort 3. Insertion sort 4. Bubble sort 5. Merge sort 6. Radix sort ( Only algorithm ) 7. Shell sort ( Only algorithm ) 8.

More information

Data Structures And Algorithms

Data Structures And Algorithms Data Structures And Algorithms Hashing Eng. Anis Nazer First Semester 2017-2018 Searching Search: find if a key exists in a given set Searching algorithms: linear (sequential) search binary search Search

More information

Topic HashTable and Table ADT

Topic HashTable and Table ADT Topic HashTable and Table ADT Hashing, Hash Function & Hashtable Search, Insertion & Deletion of elements based on Keys So far, By comparing keys! Linear data structures Non-linear data structures Time

More information

Comp 335 File Structures. Hashing

Comp 335 File Structures. Hashing Comp 335 File Structures Hashing What is Hashing? A process used with record files that will try to achieve O(1) (i.e. constant) access to a record s location in the file. An algorithm, called a hash function

More information

Hashing. 1. Introduction. 2. Direct-address tables. CmSc 250 Introduction to Algorithms

Hashing. 1. Introduction. 2. Direct-address tables. CmSc 250 Introduction to Algorithms Hashing CmSc 250 Introduction to Algorithms 1. Introduction Hashing is a method of storing elements in a table in a way that reduces the time for search. Elements are assumed to be records with several

More information

CSCD 326 Data Structures I Hashing

CSCD 326 Data Structures I Hashing 1 CSCD 326 Data Structures I Hashing Hashing Background Goal: provide a constant time complexity method of searching for stored data The best traditional searching time complexity available is O(log2n)

More information

Data and File Structures Chapter 11. Hashing

Data and File Structures Chapter 11. Hashing Data and File Structures Chapter 11 Hashing 1 Motivation Sequential Searching can be done in O(N) access time, meaning that the number of seeks grows in proportion to the size of the file. B-Trees improve

More information

Question Bank Subject: Advanced Data Structures Class: SE Computer

Question Bank Subject: Advanced Data Structures Class: SE Computer Question Bank Subject: Advanced Data Structures Class: SE Computer Question1: Write a non recursive pseudo code for post order traversal of binary tree Answer: Pseudo Code: 1. Push root into Stack_One.

More information

Data Structure Lecture#22: Searching 3 (Chapter 9) U Kang Seoul National University

Data Structure Lecture#22: Searching 3 (Chapter 9) U Kang Seoul National University Data Structure Lecture#22: Searching 3 (Chapter 9) U Kang Seoul National University U Kang 1 In This Lecture Motivation of collision resolution policy Open hashing for collision resolution Closed hashing

More information

Hashing for searching

Hashing for searching Hashing for searching Consider searching a database of records on a given key. There are three standard techniques: Searching sequentially start at the first record and look at each record in turn until

More information

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) WINTER 18 EXAMINATION Subject Name: Data Structure Model wer Subject Code: 17330 Important Instructions to examiners: 1) The answers should be examined by key words and not as word-to-word as given in

More information

Algorithms and Data Structures

Algorithms and Data Structures Lesson 4: Sets, Dictionaries and Hash Tables Luciano Bononi http://www.cs.unibo.it/~bononi/ (slide credits: these slides are a revised version of slides created by Dr. Gabriele D Angelo)

More information

COMP171. Hashing.

COMP171. Hashing. COMP171 Hashing Hashing 2 Hashing Again, a (dynamic) set of elements in which we do search, insert, and delete Linear ones: lists, stacks, queues, Nonlinear ones: trees, graphs (relations between elements

More information

CS 2412 Data Structures. Chapter 10 Sorting and Searching

CS 2412 Data Structures. Chapter 10 Sorting and Searching CS 2412 Data Structures Chapter 10 Sorting and Searching Some concepts Sorting is one of the most common data-processing applications. Sorting algorithms are classed as either internal or external. Sorting

More information

Fundamentals of Database Systems Prof. Arnab Bhattacharya Department of Computer Science and Engineering Indian Institute of Technology, Kanpur

Fundamentals of Database Systems Prof. Arnab Bhattacharya Department of Computer Science and Engineering Indian Institute of Technology, Kanpur Fundamentals of Database Systems Prof. Arnab Bhattacharya Department of Computer Science and Engineering Indian Institute of Technology, Kanpur Lecture - 18 Database Indexing: Hashing We will start on

More information

Chapter 17. Disk Storage, Basic File Structures, and Hashing. Records. Blocking

Chapter 17. Disk Storage, Basic File Structures, and Hashing. Records. Blocking Chapter 17 Disk Storage, Basic File Structures, and Hashing Records Fixed and variable length records Records contain fields which have values of a particular type (e.g., amount, date, time, age) Fields

More information

Objectives of the lesson

Objectives of the lesson Learning Outcome 1) DEMONSTRATE KNOWLEDGE AND UNDERSTANDING OF THE PROCEDURAL APPROACH TO SOFTWARE DEVELOPMENT. Knowledge & Understanding 2) DEVELOP A PROBLEM BASED STRATEGY FOR CREATING AND APPLYING PROGRAMMED

More information

5. Hashing. 5.1 General Idea. 5.2 Hash Function. 5.3 Separate Chaining. 5.4 Open Addressing. 5.5 Rehashing. 5.6 Extendible Hashing. 5.

5. Hashing. 5.1 General Idea. 5.2 Hash Function. 5.3 Separate Chaining. 5.4 Open Addressing. 5.5 Rehashing. 5.6 Extendible Hashing. 5. 5. Hashing 5.1 General Idea 5.2 Hash Function 5.3 Separate Chaining 5.4 Open Addressing 5.5 Rehashing 5.6 Extendible Hashing Malek Mouhoub, CS340 Fall 2004 1 5. Hashing Sequential access : O(n). Binary

More information

An array is a collection of similar elements that is all the elements in an array should have same data type. This means that an array can store

An array is a collection of similar elements that is all the elements in an array should have same data type. This means that an array can store An array is a collection of similar elements that is all the elements in an array should have same data type. This means that an array can store either all integers, all floating point numbers, all characters,

More information

a) State the need of data structure. Write the operations performed using data structures.

a) State the need of data structure. Write the operations performed using data structures. Important Instructions to examiners: 1) The answers should be examined by key words and not as word-to-word as given in the model answer scheme. 2) The model answer and the answer written by candidate

More information

4.1 COMPUTATIONAL THINKING AND PROBLEM-SOLVING

4.1 COMPUTATIONAL THINKING AND PROBLEM-SOLVING 4.1 COMPUTATIONAL THINKING AND PROBLEM-SOLVING 4.1.2 ALGORITHMS ALGORITHM An Algorithm is a procedure or formula for solving a problem. It is a step-by-step set of operations to be performed. It is almost

More information

CS 310 Hash Tables, Page 1. Hash Tables. CS 310 Hash Tables, Page 2

CS 310 Hash Tables, Page 1. Hash Tables. CS 310 Hash Tables, Page 2 CS 310 Hash Tables, Page 1 Hash Tables key value key value Hashing Function CS 310 Hash Tables, Page 2 The hash-table data structure achieves (near) constant time searching by wasting memory space. the

More information

TABLES AND HASHING. Chapter 13

TABLES AND HASHING. Chapter 13 Data Structures Dr Ahmed Rafat Abas Computer Science Dept, Faculty of Computer and Information, Zagazig University arabas@zu.edu.eg http://www.arsaliem.faculty.zu.edu.eg/ TABLES AND HASHING Chapter 13

More information

Computer Science 4U Unit 1. Programming Concepts and Skills Algorithms

Computer Science 4U Unit 1. Programming Concepts and Skills Algorithms Computer Science 4U Unit 1 Programming Concepts and Skills Algorithms Algorithm In mathematics and computer science, an algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation,

More information

General Idea. Key could be an integer, a string, etc e.g. a name or Id that is a part of a large employee structure

General Idea. Key could be an integer, a string, etc e.g. a name or Id that is a part of a large employee structure Hashing 1 Hash Tables We ll discuss the hash table ADT which supports only a subset of the operations allowed by binary search trees. The implementation of hash tables is called hashing. Hashing is a technique

More information

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) WINTER 17 EXAMINATION Subject Name: Data Structure Using C Model Answer Subject Code: 17330 Important Instructions to examiners: 1) The answers should be examined by key words and not as word-to-word as

More information

AAL 217: DATA STRUCTURES

AAL 217: DATA STRUCTURES Chapter # 4: Hashing AAL 217: DATA STRUCTURES The implementation of hash tables is frequently called hashing. Hashing is a technique used for performing insertions, deletions, and finds in constant average

More information

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

CSE101-lec#19. Array searching and sorting techniques. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming CSE101-lec#19 Array searching and sorting techniques Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU Outline Introduction Linear search Binary search Bubble sort Introduction The process of finding

More information

Symbol Table. Symbol table is used widely in many applications. dictionary is a kind of symbol table data dictionary is database management

Symbol Table. Symbol table is used widely in many applications. dictionary is a kind of symbol table data dictionary is database management Hashing Symbol Table Symbol table is used widely in many applications. dictionary is a kind of symbol table data dictionary is database management In general, the following operations are performed on

More information

Visit ::: Original Website For Placement Papers. ::: Data Structure

Visit  ::: Original Website For Placement Papers. ::: Data Structure Data Structure 1. What is data structure? A data structure is a way of organizing data that considers not only the items stored, but also their relationship to each other. Advance knowledge about the relationship

More information

Hash Tables Outline. Definition Hash functions Open hashing Closed hashing. Efficiency. collision resolution techniques. EECS 268 Programming II 1

Hash Tables Outline. Definition Hash functions Open hashing Closed hashing. Efficiency. collision resolution techniques. EECS 268 Programming II 1 Hash Tables Outline Definition Hash functions Open hashing Closed hashing collision resolution techniques Efficiency EECS 268 Programming II 1 Overview Implementation style for the Table ADT that is good

More information

Chapter 1 Disk Storage, Basic File Structures, and Hashing.

Chapter 1 Disk Storage, Basic File Structures, and Hashing. Chapter 1 Disk Storage, Basic File Structures, and Hashing. Adapted from the slides of Fundamentals of Database Systems (Elmasri et al., 2003) 1 Chapter Outline Disk Storage Devices Files of Records Operations

More information

Lecture 6 Sorting and Searching

Lecture 6 Sorting and Searching Lecture 6 Sorting and Searching Sorting takes an unordered collection and makes it an ordered one. 1 2 3 4 5 6 77 42 35 12 101 5 1 2 3 4 5 6 5 12 35 42 77 101 There are many algorithms for sorting a list

More information

Hashing. Hashing Procedures

Hashing. Hashing Procedures Hashing Hashing Procedures Let us denote the set of all possible key values (i.e., the universe of keys) used in a dictionary application by U. Suppose an application requires a dictionary in which elements

More information

Successful vs. Unsuccessful

Successful vs. Unsuccessful Hashing Search Given: Distinct keys k 1, k 2,, k n and collection T of n records of the form (k 1, I 1 ), (k 2, I 2 ),, (k n, I n ) where I j is the information associated with key k j for 1

More information

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

8.1. Chapter 8: Introduction to Search Algorithms. Linear Search. Linear Search. Linear Search - Example 8/23/2014. Introduction to Search Algorithms Chapter 8: Searching and Sorting Arrays 8.1 Introduction to Search Algorithms Introduction to Search Algorithms Search: locate an item in a list of information Two algorithms we will examine: Linear search

More information

DS ata Structures Aptitude

DS ata Structures Aptitude DS ata Structures Aptitude 1. What is data structure? A data structure is a way of organizing data that considers not only the items stored, but also their relationship to each other. Advance knowledge

More information

Open Addressing: Linear Probing (cont.)

Open Addressing: Linear Probing (cont.) Open Addressing: Linear Probing (cont.) Cons of Linear Probing () more complex insert, find, remove methods () primary clustering phenomenon items tend to cluster together in the bucket array, as clustering

More information

Introducing Hashing. Chapter 21. Copyright 2012 by Pearson Education, Inc. All rights reserved

Introducing Hashing. Chapter 21. Copyright 2012 by Pearson Education, Inc. All rights reserved Introducing Hashing Chapter 21 Contents What Is Hashing? Hash Functions Computing Hash Codes Compressing a Hash Code into an Index for the Hash Table A demo of hashing (after) ARRAY insert hash index =

More information

Answers. 1. (A) Attempt any five : 20 Marks

Answers. 1. (A) Attempt any five : 20 Marks Important Instructions to examiners: 1) The answers should be examined by key words and not as word-to-word as given in the model answer scheme. 2) The model answer and the answer written by candidate

More information

CMSC 341 Hashing (Continued) Based on slides from previous iterations of this course

CMSC 341 Hashing (Continued) Based on slides from previous iterations of this course CMSC 341 Hashing (Continued) Based on slides from previous iterations of this course Today s Topics Review Uses and motivations of hash tables Major concerns with hash tables Properties Hash function Hash

More information

CSE 214 Computer Science II Searching

CSE 214 Computer Science II Searching CSE 214 Computer Science II Searching Fall 2017 Stony Brook University Instructor: Shebuti Rayana shebuti.rayana@stonybrook.edu http://www3.cs.stonybrook.edu/~cse214/sec02/ Introduction Searching in a

More information

Adapted By Manik Hosen

Adapted By Manik Hosen Adapted By Manik Hosen Basic Terminology Question: Define Hashing. Ans: Concept of building a data structure that can be searched in O(l) time is called Hashing. Question: Define Hash Table with example.

More information

! A Hash Table is used to implement a set, ! The table uses a function that maps an. ! The function is called a hash function.

! A Hash Table is used to implement a set, ! The table uses a function that maps an. ! The function is called a hash function. Hash Tables Chapter 20 CS 3358 Summer II 2013 Jill Seaman Sections 201, 202, 203, 204 (not 2042), 205 1 What are hash tables?! A Hash Table is used to implement a set, providing basic operations in constant

More information

Hashing Techniques. Material based on slides by George Bebis

Hashing Techniques. Material based on slides by George Bebis Hashing Techniques Material based on slides by George Bebis https://www.cse.unr.edu/~bebis/cs477/lect/hashing.ppt The Search Problem Find items with keys matching a given search key Given an array A, containing

More information

HASH TABLES.

HASH TABLES. 1 HASH TABLES http://en.wikipedia.org/wiki/hash_table 2 Hash Table A hash table (or hash map) is a data structure that maps keys (identifiers) into a certain location (bucket) A hash function changes the

More information

Hashing HASHING HOW? Ordered Operations. Typical Hash Function. Why not discard other data structures?

Hashing HASHING HOW? Ordered Operations. Typical Hash Function. Why not discard other data structures? HASHING By, Durgesh B Garikipati Ishrath Munir Hashing Sorting was putting things in nice neat order Hashing is the opposite Put things in a random order Algorithmically determine position of any given

More information

In-Memory Searching. Linear Search. Binary Search. Binary Search Tree. k-d Tree. Hashing. Hash Collisions. Collision Strategies.

In-Memory Searching. Linear Search. Binary Search. Binary Search Tree. k-d Tree. Hashing. Hash Collisions. Collision Strategies. In-Memory Searching Linear Search Binary Search Binary Search Tree k-d Tree Hashing Hash Collisions Collision Strategies Chapter 4 Searching A second fundamental operation in Computer Science We review

More information

Programming for Engineers Arrays

Programming for Engineers Arrays Programming for Engineers Arrays ICEN 200 Spring 2018 Prof. Dola Saha 1 Array Ø Arrays are data structures consisting of related data items of the same type. Ø A group of contiguous memory locations that

More information

Chapter 13 Disk Storage, Basic File Structures, and Hashing.

Chapter 13 Disk Storage, Basic File Structures, and Hashing. Chapter 13 Disk Storage, Basic File Structures, and Hashing. Copyright 2004 Pearson Education, Inc. Chapter Outline Disk Storage Devices Files of Records Operations on Files Unordered Files Ordered Files

More information

Module 2: Classical Algorithm Design Techniques

Module 2: Classical Algorithm Design Techniques Module 2: Classical Algorithm Design Techniques Dr. Natarajan Meghanathan Associate Professor of Computer Science Jackson State University Jackson, MS 39217 E-mail: natarajan.meghanathan@jsums.edu Module

More information

Data Structures and Algorithms(10)

Data Structures and Algorithms(10) Ming Zhang "Data Structures and Algorithms" Data Structures and Algorithms(10) Instructor: Ming Zhang Textbook Authors: Ming Zhang, Tengjiao Wang and Haiyan Zhao Higher Education Press, 2008.6 (the "Eleventh

More information

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size]; Arrays An array is a collection of two or more adjacent memory cells, called array elements. Array is derived data type that is used to represent collection of data items. C Array is a collection of data

More information

Lecture 16. Reading: Weiss Ch. 5 CSE 100, UCSD: LEC 16. Page 1 of 40

Lecture 16. Reading: Weiss Ch. 5 CSE 100, UCSD: LEC 16. Page 1 of 40 Lecture 16 Hashing Hash table and hash function design Hash functions for integers and strings Collision resolution strategies: linear probing, double hashing, random hashing, separate chaining Hash table

More information

Chapter 18 Indexing Structures for Files. Indexes as Access Paths

Chapter 18 Indexing Structures for Files. Indexes as Access Paths Chapter 18 Indexing Structures for Files Indexes as Access Paths A single-level index is an auxiliary file that makes it more efficient to search for a record in the data file. The index is usually specified

More information

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING B.E SECOND SEMESTER CS 6202 PROGRAMMING AND DATA STRUCTURES I TWO MARKS UNIT I- 2 MARKS

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING B.E SECOND SEMESTER CS 6202 PROGRAMMING AND DATA STRUCTURES I TWO MARKS UNIT I- 2 MARKS DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING B.E SECOND SEMESTER CS 6202 PROGRAMMING AND DATA STRUCTURES I TWO MARKS UNIT I- 2 MARKS 1. Define global declaration? The variables that are used in more

More information

Chapter4: Data Structures. Data: It is a collection of raw facts that has implicit meaning.

Chapter4: Data Structures. Data: It is a collection of raw facts that has implicit meaning. Chapter4: s Data: It is a collection of raw facts that has implicit meaning. Data may be single valued like ID, or multi valued like address. Information: It is the processed data having explicit meaning.

More information

Module 5: Hashing. CS Data Structures and Data Management. Reza Dorrigiv, Daniel Roche. School of Computer Science, University of Waterloo

Module 5: Hashing. CS Data Structures and Data Management. Reza Dorrigiv, Daniel Roche. School of Computer Science, University of Waterloo Module 5: Hashing CS 240 - Data Structures and Data Management Reza Dorrigiv, Daniel Roche School of Computer Science, University of Waterloo Winter 2010 Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module

More information

Tables. The Table ADT is used when information needs to be stored and acessed via a key usually, but not always, a string. For example: Dictionaries

Tables. The Table ADT is used when information needs to be stored and acessed via a key usually, but not always, a string. For example: Dictionaries 1: Tables Tables The Table ADT is used when information needs to be stored and acessed via a key usually, but not always, a string. For example: Dictionaries Symbol Tables Associative Arrays (eg in awk,

More information

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

Introduction. two of the most fundamental concepts in computer science are, given an array of values: Searching Class 28 Introduction two of the most fundamental concepts in computer science are, given an array of values: search through the values to see if a specific value is present and, if so, where

More information

Today: Finish up hashing Sorted Dictionary ADT: Binary search, divide-and-conquer Recursive function and recurrence relation

Today: Finish up hashing Sorted Dictionary ADT: Binary search, divide-and-conquer Recursive function and recurrence relation Announcements HW1 PAST DUE HW2 online: 7 questions, 60 points Nat l Inst visit Thu, ok? Last time: Continued PA1 Walk Through Dictionary ADT: Unsorted Hashing Today: Finish up hashing Sorted Dictionary

More information

UNIT 5. Sorting and Hashing

UNIT 5. Sorting and Hashing UNIT 5 Sorting and Hashing SORTING METHODS SYLLABUS: 5.1.Sorting Methods a. Bubble Sort, b. Selection Sort, c. Quick Sort, d. Insertion Sort, e. Merge Sort, f. Radix Sort 5.2.Hashing Concepts CONTINUE

More information

(Refer Slide Time: 00:50)

(Refer Slide Time: 00:50) Programming, Data Structures and Algorithms Prof. N.S. Narayanaswamy Department of Computer Science and Engineering Indian Institute of Technology Madras Module - 03 Lecture 30 Searching Unordered linear

More information

Introduction. hashing performs basic operations, such as insertion, better than other ADTs we ve seen so far

Introduction. hashing performs basic operations, such as insertion, better than other ADTs we ve seen so far Chapter 5 Hashing 2 Introduction hashing performs basic operations, such as insertion, deletion, and finds in average time better than other ADTs we ve seen so far 3 Hashing a hash table is merely an hashing

More information

Hash Tables. Hashing Probing Separate Chaining Hash Function

Hash Tables. Hashing Probing Separate Chaining Hash Function Hash Tables Hashing Probing Separate Chaining Hash Function Introduction In Chapter 4 we saw: linear search O( n ) binary search O( log n ) Can we improve the search operation to achieve better than O(

More information

LECTURE 08 SEARCHING AND SORTING ARRAYS

LECTURE 08 SEARCHING AND SORTING ARRAYS PowerPoint Slides adapted from *Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* Copyright 2012 Pearson Education Inc. COMPUTER PROGRAMMING LECTURE 08 SEARCHING AND

More information

Hashing IV and Course Overview

Hashing IV and Course Overview Date: April 5-6, 2001 CSI 2131 Page: 1 Hashing IV and Course Overview Other Collision Resolution Techniques 1) Double Hashing The first hash function determines the home address If the home address is

More information

Logical File Organisation A file is logically organised as follows:

Logical File Organisation A file is logically organised as follows: File Handling The logical and physical organisation of files. Serial and sequential file handling methods. Direct and index sequential files. Creating, reading, writing and deleting records from a variety

More information

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

Chapter 10. Sorting and Searching Algorithms. Fall 2017 CISC2200 Yanjun Li 1. Sorting. Given a set (container) of n elements Chapter Sorting and Searching Algorithms Fall 2017 CISC2200 Yanjun Li 1 Sorting Given a set (container) of n elements Eg array, set of words, etc Suppose there is an order relation that can be set across

More information

Chapter 5 Hashing. Introduction. Hashing. Hashing Functions. hashing performs basic operations, such as insertion,

Chapter 5 Hashing. Introduction. Hashing. Hashing Functions. hashing performs basic operations, such as insertion, Introduction Chapter 5 Hashing hashing performs basic operations, such as insertion, deletion, and finds in average time 2 Hashing a hash table is merely an of some fixed size hashing converts into locations

More information

DO NOT. UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N.

DO NOT. UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. CS61B Fall 2013 UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division Test #2 Solutions DO NOT P. N. Hilfinger REPRODUCE 1 Test #2 Solution 2 Problems

More information

Hash-Based Indexes. Chapter 11

Hash-Based Indexes. Chapter 11 Hash-Based Indexes Chapter 11 1 Introduction : Hash-based Indexes Best for equality selections. Cannot support range searches. Static and dynamic hashing techniques exist: Trade-offs similar to ISAM vs.

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 10 Recursion and Search MOUNA KACEM Recursion: General Overview 2 Recursion in Algorithms Recursion is the use of recursive algorithms to solve a problem A recursive algorithm

More information

CS 350 Algorithms and Complexity

CS 350 Algorithms and Complexity CS 350 Algorithms and Complexity Winter 2019 Lecture 12: Space & Time Tradeoffs. Part 2: Hashing & B-Trees Andrew P. Black Department of Computer Science Portland State University Space-for-time tradeoffs

More information

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

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 Morteza Noferesti 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 of data, but it is often more useful

More information

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

C Arrays. Group of consecutive memory locations Same name and type. Array name + position number. Array elements are like normal variables 1 6 C Arrays 6.2 Arrays 2 Array Group of consecutive memory locations Same name and type To refer to an element, specify Array name + position number arrayname[ position number ] First element at position

More information

C/C++ Programming Lecture 18 Name:

C/C++ Programming Lecture 18 Name: . The following is the textbook's code for a linear search on an unsorted array. //***************************************************************** // The searchlist function performs a linear search

More information

DATA STRUCTURES AND ALGORITHMS

DATA STRUCTURES AND ALGORITHMS DATA STRUCTURES AND ALGORITHMS Sorting algorithms External sorting, Search Summary of the previous lecture Fast sorting algorithms Quick sort Heap sort Radix sort Running time of these algorithms in average

More information

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

Algorithm for siftdown(int currentposition) while true (infinite loop) do if the currentposition has NO children then return 0. How would we write the BinaryHeap siftdown function recursively? [0] 6 [1] [] 15 10 Name: template class BinaryHeap { private: int maxsize; int numitems; T * heap;... [3] [4] [5] [6] 114 0

More information

Fast Lookup: Hash tables

Fast Lookup: Hash tables CSE 100: HASHING Operations: Find (key based look up) Insert Delete Fast Lookup: Hash tables Consider the 2-sum problem: Given an unsorted array of N integers, find all pairs of elements that sum to a

More information

Database Technology. Topic 7: Data Structures for Databases. Olaf Hartig.

Database Technology. Topic 7: Data Structures for Databases. Olaf Hartig. Topic 7: Data Structures for Databases Olaf Hartig olaf.hartig@liu.se Database System 2 Storage Hierarchy Traditional Storage Hierarchy CPU Cache memory Main memory Primary storage Disk Tape Secondary

More information

File System Interface and Implementation

File System Interface and Implementation Unit 8 Structure 8.1 Introduction Objectives 8.2 Concept of a File Attributes of a File Operations on Files Types of Files Structure of File 8.3 File Access Methods Sequential Access Direct Access Indexed

More information

STACKS 3.1 INTRODUCTION 3.2 DEFINITION

STACKS 3.1 INTRODUCTION 3.2 DEFINITION STACKS 3 3.1 INTRODUCTION A stack is a linear data structure. It is very useful in many applications of computer science. It is a list in which all insertions and deletions are made at one end, called

More information

Functions. Arash Rafiey. September 26, 2017

Functions. Arash Rafiey. September 26, 2017 September 26, 2017 are the basic building blocks of a C program. are the basic building blocks of a C program. A function can be defined as a set of instructions to perform a specific task. are the basic

More information

B+ TREES Examples. B Trees are dynamic. That is, the height of the tree grows and contracts as records are added and deleted.

B+ TREES Examples. B Trees are dynamic. That is, the height of the tree grows and contracts as records are added and deleted. B+ TREES Examples B Trees. B Trees are multi-way trees. That is each node contains a set of keys and pointers. A B Tree with four keys and five pointers represents the minimum size of a B Tree node. A

More information

RECURSION. Data Structures & SWU Rachel Cardell- Oliver

RECURSION. Data Structures & SWU Rachel Cardell- Oliver RECURSION Data Structures & Algorithms @ SWU Rachel Cardell- Oliver Hello. This is Rachel Cardell-Oliver from the University of Western Australia. In this lecture I will give a brief introduction to the

More information

CSE100. Advanced Data Structures. Lecture 21. (Based on Paul Kube course materials)

CSE100. Advanced Data Structures. Lecture 21. (Based on Paul Kube course materials) CSE100 Advanced Data Structures Lecture 21 (Based on Paul Kube course materials) CSE 100 Collision resolution strategies: linear probing, double hashing, random hashing, separate chaining Hash table cost

More information

The physical database. Contents - physical database design DATABASE DESIGN I - 1DL300. Introduction to Physical Database Design

The physical database. Contents - physical database design DATABASE DESIGN I - 1DL300. Introduction to Physical Database Design DATABASE DESIGN I - 1DL300 Fall 2011 Introduction to Physical Database Design Elmasri/Navathe ch 16 and 17 Padron-McCarthy/Risch ch 21 and 22 An introductory course on database systems http://www.it.uu.se/edu/course/homepage/dbastekn/ht11

More information

AE52/AC52/AT52 C & Data Structures JUNE 2014

AE52/AC52/AT52 C & Data Structures JUNE 2014 Q.2 a. Write a program to add two numbers using a temporary variable. #include #include int main () int num1, num2; clrscr (); printf( \n Enter the first number : ); scanf ( %d, &num1);

More information

Use PageUp and PageDown to move from screen to screen. Click on speaker to play sound.

Use PageUp and PageDown to move from screen to screen. Click on speaker to play sound. Algonquin College Hash Tables (Java) Created by Rex Woollard Use PageUp and PageDown to move from screen to screen. Click on speaker to play sound. Linear Search O(n) Suitable for very short lists 1 Binary

More information

Data Structure. IBPS SO (IT- Officer) Exam 2017

Data Structure. IBPS SO (IT- Officer) Exam 2017 Data Structure IBPS SO (IT- Officer) Exam 2017 Data Structure: In computer science, a data structure is a way of storing and organizing data in a computer s memory so that it can be used efficiently. Data

More information

Hashed-Based Indexing

Hashed-Based Indexing Topics Hashed-Based Indexing Linda Wu Static hashing Dynamic hashing Extendible Hashing Linear Hashing (CMPT 54 4-) Chapter CMPT 54 4- Static Hashing An index consists of buckets 0 ~ N-1 A bucket consists

More information

CMSC 341 Lecture 16/17 Hashing, Parts 1 & 2

CMSC 341 Lecture 16/17 Hashing, Parts 1 & 2 CMSC 341 Lecture 16/17 Hashing, Parts 1 & 2 Prof. John Park Based on slides from previous iterations of this course Today s Topics Overview Uses and motivations of hash tables Major concerns with hash

More information

HASH TABLES. Hash Tables Page 1

HASH TABLES. Hash Tables Page 1 HASH TABLES TABLE OF CONTENTS 1. Introduction to Hashing 2. Java Implementation of Linear Probing 3. Maurer s Quadratic Probing 4. Double Hashing 5. Separate Chaining 6. Hash Functions 7. Alphanumeric

More information

Building Java Programs Chapter 13

Building Java Programs Chapter 13 Building Java Programs Chapter 13 Searching and Sorting Copyright (c) Pearson 2013. All rights reserved. Sequential search sequential search: Locates a target value in an array/list by examining each element

More information

CS302 - Data Structures using C++

CS302 - Data Structures using C++ CS302 - Data Structures using C++ Topic: Recursion - Examples Kostas Alexis CS302 - Data Structures using C++ Topic: The Binary Search Kostas Alexis The Binary Search Assumes and exploits that the input

More information

The time and space are the two measure for efficiency of an algorithm.

The time and space are the two measure for efficiency of an algorithm. There are basically six operations: 5. Sorting: Arranging the elements of list in an order (either ascending or descending). 6. Merging: combining the two list into one list. Algorithm: The time and space

More information

6.7 b. Show that a heap of eight elements can be constructed in eight comparisons between heap elements. Tournament of pairwise comparisons

6.7 b. Show that a heap of eight elements can be constructed in eight comparisons between heap elements. Tournament of pairwise comparisons Homework 4 and 5 6.7 b. Show that a heap of eight elements can be constructed in eight comparisons between heap elements. Tournament of pairwise comparisons 6.8 Show the following regarding the maximum

More information