SlideShare ist ein Scribd-Unternehmen logo
1 von 19
SEARCHING
Introduction 
 Process of finding an element within the list of 
elements in order or randomly. 
 Retrieval: Successful Search. 
 A table of records in which a key is used for retriev 
al is often called a SEARCH TABLE or 
DICTIONARY. 
 Internal Searching – Whole data in main memory 
 External Searching – Most data is kept in auxiliary 
memory. 
2
Searching Methods 
 Sequential or Linear Searching. 
 Binary Search. 
 Hashing. 
3
Sequential Search 
 Searches on unordered and ordered tables in 
sequential manner until the desired record is not 
found or table end is not reached. 
 It is simple and good for small arrays. 
 Mostly used when data is not sorted. 
 Efficiency: 
 Best – O(1) 
 Average – O(n/2) 
 Worst – O(n) 
 Less efficient if array size is large. 
 Not efficient on sorted arrays. 
4
Binary Search 
 This technique works better on sorted arrays and 
can be applied only on sorted arrays. 
 Not applied on Linked Lists. 
 Requires less number of comparisons than linear 
search. 
 Efficiency: O(log2n). 
 Logic behind the technique: 
5 
First Half Second Half 
First Value Mid Value Last Value 
low=0 mid=(low+high)/2 high=n-1
Hashing
Introduction 
 The search operation on a sorted array using the 
7 
binary search method takes O(log2n) 
 We can improve the search time by using an 
approach called Hashing. 
 Usually implemented on Dictionaries. 
 Well, there are lots of applications out there that 
need to support ONLY the operations INSERT, 
SEARCH, and DELETE. These are known as 
“dictionary” operations. 
 Hashing can make this happen in O(1) and is quite 
fast in practice.
Dictionary 
 A dictionary is a collection of elements 
 Each element has a field called key 
8 
– (key, value) 
 Every key is usually distinct. 
 Typical dictionary operations are: 
– Insert a pair into the dictionary 
– Search the pair with a specified key 
– Delete the pair with a specified key 
 Collection of student records in a class 
– (key, value) =(student-number, a list of assignment and 
exam marks) 
– All keys are distinct
Dictionary as an Ordered Linear List 
 L = (e1, e2, e3, …, en) 
 Each ei is a pair (key, value) 
 Array or chain representation 
9 
– unsorted array: O(n) search time 
– sorted array: O(logn) search time 
– unsorted chain: O(n) search time 
– sorted chain: O(n) search time
Hash Table 
 A hash table is a data structure that stores elements and 
10 
allows insertions, lookups, and deletions to be performed in 
O(1) time. 
 A hash table is an alternative method for representing a 
dictionary 
 In a hash table, a hash function is used to map keys into 
positions in a table. This act is called hashing 
 Hash Table Operations 
– Search: compute f(k) and see if a pair exists 
– Insert: compute f(k) and place it in that position 
– Delete: compute f(k) and delete the pair in that position 
 In ideal situation, hash table search, insert or delete takes 
(1)
Why we need Hash Tables 
 Internet routers is a good example of why hash 
tables are required. 
 A router table (especially in those routers in the 
backbone networks of internet operators) may 
contain hundreds of thousands or millions of 
entries. 
When a packet has to be routed to a specific IP ad 
dress, the router has to determine the best route 
by querying the router table in an efficient manner. 
Hash Tables are used as an efficient lookup struct 
ure having as key the IP address and as value the 
path that should be follow for that address. 
11
Why we need Hash Tables 
12
How Does it Work 
 The table part is just an ordinary array, it is the Hash that 
we are interested in. 
 The Hash is a function that transforms a key into address 
or index of array(table) where the record will be stored. If 
the size of the table is N, then the integer will be in the 
range 0 to N-1. The integer is used as an index into the arr 
ay. Thus, in essence, the key itself indexes the array. 
 If h is a hash function and k is key then h(k) is called the 
hash of the key and is the index at which a record with the 
key k should be placed. 
 The hash function generates this address by performing 
some simple arithmetic or logical operations on the key. 
13
Ideal Hashing Example 
 Pairs are: (22,a),(33,c),(3,d),(72,e),(85,f)- 
14 
-(key, value) pairs 
 Hash table is ht[0:7], m = 8 (where m is the 
number of positions in the hash table) 
 Hash function h is k % m = k % 8 
 Where are the pairs stored? 
[0] [1] [2] [3] [4] [5] [6] [7] 
(72,e) (33,c) (3,d) (85,f) (22,a) 
[0] [1] [2] [3] [4] [5] [6] [7]
Hashing Function Methods (Hashing Methods) 
 Division Hash Method 
 The key K is divided by some number m and the 
remainder is used as the hash address of K. 
 h(k)=k mod m 
 This gives the indexes in the range 0 to m-1 so the 
hash table should be of size m 
 This is an example of uniform hash function if value 
of m will be chosen carefully. 
 Generally a prime number is a best choice which 
will spread keys evenly. 
 A uniform hash function is designed to distribute 
the keys roughly evenly into the available positions 
within the array (or hash table). 
15
Hashing Function Methods 
 The Folding Method 
 The key K is partitioned into a number of parts ,each 
of which has the same length as the required 
address with the possible exception of the last part . 
 The parts are then added together , ignoring the 
final carry, to form an address. 
 Example: If key=356942781 is to be transformed into 
a three digit address. 
P1=356, P2=942, P3=781 are added to yield 079. 
16
Hashing Function Methods 
 The Mid- Square Method 
 The key K is multiplied by itself and the address is 
obtained by selecting an appropriate number of 
digits from the middle of the square. 
 The number of digits selected depends on the size 
of the table. 
 Example: If key=123456 is to be transformed. 
 (123456)2=15241383936 
 If a three-digit address is required, positions 5 to 7 
could be chosen giving address 138. 
17
Hashing a string key 
 Table size [0..99] 
 A..Z ---> 1,2, ...26 
 0..9 ----> 27,...36 
 Key: CS1 --->3+19+28 (concat) = 31,928 
 (31,928)2 = 1,019,397,184 - 10 digits 
 Extract middle 2 digits (5th and 6th) as table 
size is 0..99. 
 Get 39, so: H(CS1) = 39. 
18
Characteristics of a Good Hash Function 
 The hash value is fully determined by the data 
being hashed. 
 The hash function uses all the input data. 
 The hash function "uniformly" distributes the data 
across the entire set of possible hash values. 
 The hash function generates very different hash 
values for similar strings. 
19

Weitere ähnliche Inhalte

Was ist angesagt?

Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]Muhammad Hammad Waseem
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure shameen khan
 
Data Structures : hashing (1)
Data Structures : hashing (1)Data Structures : hashing (1)
Data Structures : hashing (1)Home
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihmSajid Marwat
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithmsGanesh Solanke
 
Selection sort
Selection sortSelection sort
Selection sortstella D
 
Hashing In Data Structure
Hashing In Data Structure Hashing In Data Structure
Hashing In Data Structure Meghaj Mallick
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort AlgorithmLemia Algmri
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IMohamed Loey
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)Elavarasi K
 
UNIT I LINEAR DATA STRUCTURES – LIST
UNIT I 	LINEAR DATA STRUCTURES – LIST 	UNIT I 	LINEAR DATA STRUCTURES – LIST
UNIT I LINEAR DATA STRUCTURES – LIST Kathirvel Ayyaswamy
 

Was ist angesagt? (20)

Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 
Red black tree
Red black treeRed black tree
Red black tree
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Hash table
Hash tableHash table
Hash table
 
Hashing
HashingHashing
Hashing
 
Data Structures : hashing (1)
Data Structures : hashing (1)Data Structures : hashing (1)
Data Structures : hashing (1)
 
Hashing
HashingHashing
Hashing
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Merge Sort
Merge SortMerge Sort
Merge Sort
 
Selection sort
Selection sortSelection sort
Selection sort
 
Hashing In Data Structure
Hashing In Data Structure Hashing In Data Structure
Hashing In Data Structure
 
Linked list
Linked listLinked list
Linked list
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms I
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
 
Quick sort
Quick sortQuick sort
Quick sort
 
UNIT I LINEAR DATA STRUCTURES – LIST
UNIT I 	LINEAR DATA STRUCTURES – LIST 	UNIT I 	LINEAR DATA STRUCTURES – LIST
UNIT I LINEAR DATA STRUCTURES – LIST
 

Ähnlich wie Hashing Technique In Data Structures

Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptxAgonySingh
 
presentation on important DAG,TRIE,Hashing.pptx
presentation on important DAG,TRIE,Hashing.pptxpresentation on important DAG,TRIE,Hashing.pptx
presentation on important DAG,TRIE,Hashing.pptxjainaaru59
 
Algorithms notes tutorials duniya
Algorithms notes   tutorials duniyaAlgorithms notes   tutorials duniya
Algorithms notes tutorials duniyaTutorialsDuniya.com
 
DS Unit 1.pptx
DS Unit 1.pptxDS Unit 1.pptx
DS Unit 1.pptxchin463670
 
Sienna 9 hashing
Sienna 9 hashingSienna 9 hashing
Sienna 9 hashingchidabdu
 
Data Structures Design Notes.pdf
Data Structures Design Notes.pdfData Structures Design Notes.pdf
Data Structures Design Notes.pdfAmuthachenthiruK
 
Hashing and File Structures in Data Structure.pdf
Hashing and File Structures in Data Structure.pdfHashing and File Structures in Data Structure.pdf
Hashing and File Structures in Data Structure.pdfJaithoonBibi
 
Hashing.pptx
Hashing.pptxHashing.pptx
Hashing.pptxkratika64
 
11_hashtable-1.ppt. Data structure algorithm
11_hashtable-1.ppt. Data structure algorithm11_hashtable-1.ppt. Data structure algorithm
11_hashtable-1.ppt. Data structure algorithmfarhankhan89766
 
DSA UNIT II ARRAY AND LIST - notes
DSA UNIT II ARRAY AND LIST - notesDSA UNIT II ARRAY AND LIST - notes
DSA UNIT II ARRAY AND LIST - notesswathirajstar
 

Ähnlich wie Hashing Technique In Data Structures (20)

hashing.pdf
hashing.pdfhashing.pdf
hashing.pdf
 
Unit viii searching and hashing
Unit   viii searching and hashing Unit   viii searching and hashing
Unit viii searching and hashing
 
Lec4
Lec4Lec4
Lec4
 
Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptx
 
presentation on important DAG,TRIE,Hashing.pptx
presentation on important DAG,TRIE,Hashing.pptxpresentation on important DAG,TRIE,Hashing.pptx
presentation on important DAG,TRIE,Hashing.pptx
 
Algorithms notes tutorials duniya
Algorithms notes   tutorials duniyaAlgorithms notes   tutorials duniya
Algorithms notes tutorials duniya
 
Unit 8 searching and hashing
Unit   8 searching and hashingUnit   8 searching and hashing
Unit 8 searching and hashing
 
Hashing .pptx
Hashing .pptxHashing .pptx
Hashing .pptx
 
DS Unit 1.pptx
DS Unit 1.pptxDS Unit 1.pptx
DS Unit 1.pptx
 
Sienna 9 hashing
Sienna 9 hashingSienna 9 hashing
Sienna 9 hashing
 
Data Structures Design Notes.pdf
Data Structures Design Notes.pdfData Structures Design Notes.pdf
Data Structures Design Notes.pdf
 
Hashing
HashingHashing
Hashing
 
Hashing and File Structures in Data Structure.pdf
Hashing and File Structures in Data Structure.pdfHashing and File Structures in Data Structure.pdf
Hashing and File Structures in Data Structure.pdf
 
Hashing.pptx
Hashing.pptxHashing.pptx
Hashing.pptx
 
Hashing
HashingHashing
Hashing
 
Chapter 12 ds
Chapter 12 dsChapter 12 ds
Chapter 12 ds
 
Hashing.pptx
Hashing.pptxHashing.pptx
Hashing.pptx
 
Hashing 1
Hashing 1Hashing 1
Hashing 1
 
11_hashtable-1.ppt. Data structure algorithm
11_hashtable-1.ppt. Data structure algorithm11_hashtable-1.ppt. Data structure algorithm
11_hashtable-1.ppt. Data structure algorithm
 
DSA UNIT II ARRAY AND LIST - notes
DSA UNIT II ARRAY AND LIST - notesDSA UNIT II ARRAY AND LIST - notes
DSA UNIT II ARRAY AND LIST - notes
 

Kürzlich hochgeladen

GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 

Kürzlich hochgeladen (20)

GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 

Hashing Technique In Data Structures

  • 2. Introduction  Process of finding an element within the list of elements in order or randomly.  Retrieval: Successful Search.  A table of records in which a key is used for retriev al is often called a SEARCH TABLE or DICTIONARY.  Internal Searching – Whole data in main memory  External Searching – Most data is kept in auxiliary memory. 2
  • 3. Searching Methods  Sequential or Linear Searching.  Binary Search.  Hashing. 3
  • 4. Sequential Search  Searches on unordered and ordered tables in sequential manner until the desired record is not found or table end is not reached.  It is simple and good for small arrays.  Mostly used when data is not sorted.  Efficiency:  Best – O(1)  Average – O(n/2)  Worst – O(n)  Less efficient if array size is large.  Not efficient on sorted arrays. 4
  • 5. Binary Search  This technique works better on sorted arrays and can be applied only on sorted arrays.  Not applied on Linked Lists.  Requires less number of comparisons than linear search.  Efficiency: O(log2n).  Logic behind the technique: 5 First Half Second Half First Value Mid Value Last Value low=0 mid=(low+high)/2 high=n-1
  • 7. Introduction  The search operation on a sorted array using the 7 binary search method takes O(log2n)  We can improve the search time by using an approach called Hashing.  Usually implemented on Dictionaries.  Well, there are lots of applications out there that need to support ONLY the operations INSERT, SEARCH, and DELETE. These are known as “dictionary” operations.  Hashing can make this happen in O(1) and is quite fast in practice.
  • 8. Dictionary  A dictionary is a collection of elements  Each element has a field called key 8 – (key, value)  Every key is usually distinct.  Typical dictionary operations are: – Insert a pair into the dictionary – Search the pair with a specified key – Delete the pair with a specified key  Collection of student records in a class – (key, value) =(student-number, a list of assignment and exam marks) – All keys are distinct
  • 9. Dictionary as an Ordered Linear List  L = (e1, e2, e3, …, en)  Each ei is a pair (key, value)  Array or chain representation 9 – unsorted array: O(n) search time – sorted array: O(logn) search time – unsorted chain: O(n) search time – sorted chain: O(n) search time
  • 10. Hash Table  A hash table is a data structure that stores elements and 10 allows insertions, lookups, and deletions to be performed in O(1) time.  A hash table is an alternative method for representing a dictionary  In a hash table, a hash function is used to map keys into positions in a table. This act is called hashing  Hash Table Operations – Search: compute f(k) and see if a pair exists – Insert: compute f(k) and place it in that position – Delete: compute f(k) and delete the pair in that position  In ideal situation, hash table search, insert or delete takes (1)
  • 11. Why we need Hash Tables  Internet routers is a good example of why hash tables are required.  A router table (especially in those routers in the backbone networks of internet operators) may contain hundreds of thousands or millions of entries. When a packet has to be routed to a specific IP ad dress, the router has to determine the best route by querying the router table in an efficient manner. Hash Tables are used as an efficient lookup struct ure having as key the IP address and as value the path that should be follow for that address. 11
  • 12. Why we need Hash Tables 12
  • 13. How Does it Work  The table part is just an ordinary array, it is the Hash that we are interested in.  The Hash is a function that transforms a key into address or index of array(table) where the record will be stored. If the size of the table is N, then the integer will be in the range 0 to N-1. The integer is used as an index into the arr ay. Thus, in essence, the key itself indexes the array.  If h is a hash function and k is key then h(k) is called the hash of the key and is the index at which a record with the key k should be placed.  The hash function generates this address by performing some simple arithmetic or logical operations on the key. 13
  • 14. Ideal Hashing Example  Pairs are: (22,a),(33,c),(3,d),(72,e),(85,f)- 14 -(key, value) pairs  Hash table is ht[0:7], m = 8 (where m is the number of positions in the hash table)  Hash function h is k % m = k % 8  Where are the pairs stored? [0] [1] [2] [3] [4] [5] [6] [7] (72,e) (33,c) (3,d) (85,f) (22,a) [0] [1] [2] [3] [4] [5] [6] [7]
  • 15. Hashing Function Methods (Hashing Methods)  Division Hash Method  The key K is divided by some number m and the remainder is used as the hash address of K.  h(k)=k mod m  This gives the indexes in the range 0 to m-1 so the hash table should be of size m  This is an example of uniform hash function if value of m will be chosen carefully.  Generally a prime number is a best choice which will spread keys evenly.  A uniform hash function is designed to distribute the keys roughly evenly into the available positions within the array (or hash table). 15
  • 16. Hashing Function Methods  The Folding Method  The key K is partitioned into a number of parts ,each of which has the same length as the required address with the possible exception of the last part .  The parts are then added together , ignoring the final carry, to form an address.  Example: If key=356942781 is to be transformed into a three digit address. P1=356, P2=942, P3=781 are added to yield 079. 16
  • 17. Hashing Function Methods  The Mid- Square Method  The key K is multiplied by itself and the address is obtained by selecting an appropriate number of digits from the middle of the square.  The number of digits selected depends on the size of the table.  Example: If key=123456 is to be transformed.  (123456)2=15241383936  If a three-digit address is required, positions 5 to 7 could be chosen giving address 138. 17
  • 18. Hashing a string key  Table size [0..99]  A..Z ---> 1,2, ...26  0..9 ----> 27,...36  Key: CS1 --->3+19+28 (concat) = 31,928  (31,928)2 = 1,019,397,184 - 10 digits  Extract middle 2 digits (5th and 6th) as table size is 0..99.  Get 39, so: H(CS1) = 39. 18
  • 19. Characteristics of a Good Hash Function  The hash value is fully determined by the data being hashed.  The hash function uses all the input data.  The hash function "uniformly" distributes the data across the entire set of possible hash values.  The hash function generates very different hash values for similar strings. 19