SlideShare a Scribd company logo
Interpolation Search
By: 3Algo
Interpolation ?
@ Interpolation (mathematic) : a type of estimation , a method of
constructing new data points within the range of a discrete set of known
data points.
Interpolation Search ?
@ Interpolation Search : An algorithm for searching for a key in an array that
has been ordered (sorted) by numerical values assigned to the key (key
values). It was first described by W.W Peterson in 1957.
### HAPPY NOTE ### : “Interpolation Search” is very similar to “Binary
Search” but it could be much faster since it could estimate the closest or
might be the right index of the “KEY”, that we want to search for.
Story Begin
To completely understand about the “Interpolation Search”, here are the
topics to learn.
+ Binary Search
+ Linear Interpolation
+ Interpolation Search
+ Binary Search
@ Both “Binary Search” and “Interpolation Search” only work with sorted
array of values because it does not know where to search if the array is not
sorted.
@ The expected result from the search algorithm is FOUND or NOT FOUND
@ Time Complexity of Binary Search : log2 (n)
How it work?
Example: I have a sorted array (ascending) of integer like this
-13, 0, 10, 17, 27, 100
Let find index of number “17”
Solution:
Integer -13 0 10 17 27 100
Index 0 1 2 3 4 5
Step to find it:
1. Find middle index of the array if the lowest index is less than or equal to
highest index otherwise => Ops! Not found
2. Compare the value of the middle index
| if it is equal to the number I am searching for => Yeah! I found it
| if it is less than the number I am searching for => It might be in the
higher index so set my smallest index = the middle index + 1; then redo it
from step one
| if it is higher than the number I am searching for => set highest index =
the middle index -1; then redo
Integer -13 0 10 17 27 100
Index 0 1 2 3 4 5
@ mid_index =
𝐡𝐢𝐠𝐡𝐞𝐬𝐭+𝒍𝒐𝒘𝒆𝒔𝒕
𝟐
mid_index =
5 +0
2
= 2.5 but we will get mid_index = 2 since index cannot be
floating point value
Integer value at index mid_index is 10 < 17 (the number I am searching for)
 Set the lowest index equal to mid_index + 1 = 3
So find new mid_index =
5 +3
2
= 4
Integer -13 0 10 17 27 100
Index 0 1 2 3 4 5
Integer value at index mid_index is 27 > 17 (the number I am searching for)
 Set the highest index equal to mid_index - 1 = 3
So find new mid_index =
3 + 3
2
= 3
Integer -13 0 10 17 27 100
Index 0 1 2 3 4 5
Integer value at index mid_index is 17 = 17 (the number I am searching for)
YEAH! The value is at index 3
@ If I do not found the value here the lowest index might be larger than
highest index, which is not logical. If so the value is not found.
@ The whole concept of binary search is adapted by Interpolation Search
except that Interpolation Search modify the mid_index to a better index
(closer to the index of the value). That means Interpolation Search could be
faster than Binary Search.
+ Linear Interpolation
@ Linear interpolation is used as the improvement on mid_index of binary
search.
@ Linear Interpolation : a method of curve fitting using linear polynomials
to construct new data points within the range of a discrete set of known
data points.
How it work?
(x, y)
x0 x1
y0
y1
@ The graph means that it has 2 known points and I want to estimate
another point by assume the value is linear. So the value I want to find is
somewhere between the two known points.
@ From the point that I want to find to point 0 (x0, y0) and to point 1 (x1, y1)
is a the same line so the slope of the two line is the same or equal.
y − y0
x −x0
=
y1− y0
x1− x0
Let say I want to find the “x” when I know “y”
 x = x0 + (x1 – x0) ×
y− y0
y1− y0
@ This interpolation formula will be used in the interpolation search to find
a better mid_index.
+ Interpolation Search
@ The expected result from the search algorithm is FOUND or NOT FOUND
@ Time Complexity of Binary Search : O (log log n))
Since the improvement in on mid_index, so the better mid_index is
mid_index =
lowest + (highest – lowest) ×
integer I am searching −lowest interger
highest integer −lowest interger
How it work?
Example: I have a sorted array (ascending) of integer like this
-13, 0, 10, 17, 27, 100
Let find index of number “17” now using interpolation search instead of
binary search
Solution:
Integer -13 0 10 17 27 100
Index 0 1 2 3 4 5
mid_index = 0 + (5 – 0)
17 −(−13)
100 −(−13)
= 1.32743363 but we will get
mid_index = 1 since index cannot be floating point value
Integer value at index mid_index is 0 < 17 Set the lowest index equal to
mid_index + 1 = 2
So find new mid_index = 2 + (5 – 2)
17 − 0
100 − 0
= 2.51 so take mid_index = 2
Integer -13 0 10 17 27 100
Index 0 1 2 3 4 5
Integer value at index mid_index is 10 < 17 Set the lowest index equal to
mid_index + 1 = 3
So find new mid_index = 3 + (5 – 3)
17 −10
100 −10
= 3.15151515 so take
mid_index = 3
Integer -13 0 10 17 27 100
Index 0 1 2 3 4 5
Integer value at index mid_index is 17 = 17 (the number I am searching for)
YEAH! The value is at index 3
@ If I do not found the value here the value of the lowest index might be
larger than the value I want to search for, which is not logical. If so the value
is not found.
Reading Materials
• https://en.wikipedia.org/wiki/Interpolation
• https://en.wikipedia.org/wiki/Linear_interpolation
• https://en.wikipedia.org/wiki/Binary_search_algorithm

More Related Content

What's hot

Graph representation
Graph representationGraph representation
Graph representation
Tech_MX
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
Anandhasilambarasan D
 
Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)
PyData
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
sagar yadav
 
Presentation on array
Presentation on array Presentation on array
Presentation on array
topu93
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
Adam Mukharil Bachtiar
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
Pranay Neema
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - Graph
Madhu Bala
 
Binary search
Binary searchBinary search
Binary search
AparnaKumari31
 
Single linked list
Single linked listSingle linked list
Single linked list
jasbirsingh chauhan
 
Graph data structure and algorithms
Graph data structure and algorithmsGraph data structure and algorithms
Graph data structure and algorithms
Anandhasilambarasan D
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
Dharita Chokshi
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
eShikshak
 
Binary Search
Binary SearchBinary Search
Binary Search
kunj desai
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data Structure
Meghaj Mallick
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
Mohd Arif
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
moazamali28
 
Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
Masud Parvaze
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
Ninad Mankar
 
Floyd Warshall Algorithm
Floyd Warshall AlgorithmFloyd Warshall Algorithm
Floyd Warshall Algorithm
InteX Research Lab
 

What's hot (20)

Graph representation
Graph representationGraph representation
Graph representation
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
 
Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Presentation on array
Presentation on array Presentation on array
Presentation on array
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - Graph
 
Binary search
Binary searchBinary search
Binary search
 
Single linked list
Single linked listSingle linked list
Single linked list
 
Graph data structure and algorithms
Graph data structure and algorithmsGraph data structure and algorithms
Graph data structure and algorithms
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
Binary Search
Binary SearchBinary Search
Binary Search
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data Structure
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Floyd Warshall Algorithm
Floyd Warshall AlgorithmFloyd Warshall Algorithm
Floyd Warshall Algorithm
 

Similar to Interpolation search

Linear and Binary Search
Linear and Binary SearchLinear and Binary Search
Linear and Binary Search
WinNie Sjr
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
haramaya university
 
1 D Arrays in C++
1 D Arrays in C++1 D Arrays in C++
1 D Arrays in C++
poonam.rwalia
 
Unit iv(dsc++)
Unit iv(dsc++)Unit iv(dsc++)
Unit iv(dsc++)
Durga Devi
 
Lecture_Oct26.pptx
Lecture_Oct26.pptxLecture_Oct26.pptx
Lecture_Oct26.pptx
SylrizcinMarieManzo3
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Umesh Kumar
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sorting
guest2cb109
 
Data Structures Design Notes.pdf
Data Structures Design Notes.pdfData Structures Design Notes.pdf
Data Structures Design Notes.pdf
AmuthachenthiruK
 
Binary search algorithm
Binary search algorithmBinary search algorithm
Binary search algorithm
maamir farooq
 
search_sort.ppt
search_sort.pptsearch_sort.ppt
search_sort.ppt
SwatiHans10
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searching
sajinis3
 
Binary search2
Binary search2Binary search2
Binary search2
tahanihassan
 
UNIT IV -Data Structures.pdf
UNIT IV -Data Structures.pdfUNIT IV -Data Structures.pdf
UNIT IV -Data Structures.pdf
KPRevathiAsstprofITD
 
Searching and Sorting Algorithms in Data Structures
Searching and Sorting Algorithms  in Data StructuresSearching and Sorting Algorithms  in Data Structures
Searching and Sorting Algorithms in Data Structures
poongothai11
 
binary_search
binary_searchbinary_search
binary_search
Mohamed Elsayed
 
Searching
Searching Searching
Search techniques and Hashing
Search techniques and HashingSearch techniques and Hashing
Search techniques and Hashing
Thirunavukarasu Mani
 
Chapter3.pptx
Chapter3.pptxChapter3.pptx
Chapter3.pptx
ASMAALWADEE2
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
Eduardo Bergavera
 

Similar to Interpolation search (20)

Linear and Binary Search
Linear and Binary SearchLinear and Binary Search
Linear and Binary Search
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
1 D Arrays in C++
1 D Arrays in C++1 D Arrays in C++
1 D Arrays in C++
 
Unit iv(dsc++)
Unit iv(dsc++)Unit iv(dsc++)
Unit iv(dsc++)
 
Lecture_Oct26.pptx
Lecture_Oct26.pptxLecture_Oct26.pptx
Lecture_Oct26.pptx
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sorting
 
Data Structures Design Notes.pdf
Data Structures Design Notes.pdfData Structures Design Notes.pdf
Data Structures Design Notes.pdf
 
Binary search algorithm
Binary search algorithmBinary search algorithm
Binary search algorithm
 
search_sort.ppt
search_sort.pptsearch_sort.ppt
search_sort.ppt
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searching
 
Binary search2
Binary search2Binary search2
Binary search2
 
UNIT IV -Data Structures.pdf
UNIT IV -Data Structures.pdfUNIT IV -Data Structures.pdf
UNIT IV -Data Structures.pdf
 
Searching and Sorting Algorithms in Data Structures
Searching and Sorting Algorithms  in Data StructuresSearching and Sorting Algorithms  in Data Structures
Searching and Sorting Algorithms in Data Structures
 
binary_search
binary_searchbinary_search
binary_search
 
Searching
Searching Searching
Searching
 
Search techniques and Hashing
Search techniques and HashingSearch techniques and Hashing
Search techniques and Hashing
 
Chapter3.pptx
Chapter3.pptxChapter3.pptx
Chapter3.pptx
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
 

Recently uploaded

How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
Wahiba Chair Training & Consulting
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
สมใจ จันสุกสี
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
Jyoti Chand
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
Himanshu Rai
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 

Recently uploaded (20)

How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 

Interpolation search

  • 2. Interpolation ? @ Interpolation (mathematic) : a type of estimation , a method of constructing new data points within the range of a discrete set of known data points. Interpolation Search ? @ Interpolation Search : An algorithm for searching for a key in an array that has been ordered (sorted) by numerical values assigned to the key (key values). It was first described by W.W Peterson in 1957. ### HAPPY NOTE ### : “Interpolation Search” is very similar to “Binary Search” but it could be much faster since it could estimate the closest or might be the right index of the “KEY”, that we want to search for. Story Begin To completely understand about the “Interpolation Search”, here are the topics to learn. + Binary Search + Linear Interpolation + Interpolation Search + Binary Search @ Both “Binary Search” and “Interpolation Search” only work with sorted array of values because it does not know where to search if the array is not sorted. @ The expected result from the search algorithm is FOUND or NOT FOUND @ Time Complexity of Binary Search : log2 (n) How it work? Example: I have a sorted array (ascending) of integer like this -13, 0, 10, 17, 27, 100 Let find index of number “17” Solution: Integer -13 0 10 17 27 100 Index 0 1 2 3 4 5 Step to find it: 1. Find middle index of the array if the lowest index is less than or equal to highest index otherwise => Ops! Not found 2. Compare the value of the middle index | if it is equal to the number I am searching for => Yeah! I found it | if it is less than the number I am searching for => It might be in the higher index so set my smallest index = the middle index + 1; then redo it from step one | if it is higher than the number I am searching for => set highest index = the middle index -1; then redo
  • 3. Integer -13 0 10 17 27 100 Index 0 1 2 3 4 5 @ mid_index = 𝐡𝐢𝐠𝐡𝐞𝐬𝐭+𝒍𝒐𝒘𝒆𝒔𝒕 𝟐 mid_index = 5 +0 2 = 2.5 but we will get mid_index = 2 since index cannot be floating point value Integer value at index mid_index is 10 < 17 (the number I am searching for)  Set the lowest index equal to mid_index + 1 = 3 So find new mid_index = 5 +3 2 = 4 Integer -13 0 10 17 27 100 Index 0 1 2 3 4 5 Integer value at index mid_index is 27 > 17 (the number I am searching for)  Set the highest index equal to mid_index - 1 = 3 So find new mid_index = 3 + 3 2 = 3 Integer -13 0 10 17 27 100 Index 0 1 2 3 4 5 Integer value at index mid_index is 17 = 17 (the number I am searching for) YEAH! The value is at index 3 @ If I do not found the value here the lowest index might be larger than highest index, which is not logical. If so the value is not found. @ The whole concept of binary search is adapted by Interpolation Search except that Interpolation Search modify the mid_index to a better index (closer to the index of the value). That means Interpolation Search could be faster than Binary Search. + Linear Interpolation @ Linear interpolation is used as the improvement on mid_index of binary search. @ Linear Interpolation : a method of curve fitting using linear polynomials to construct new data points within the range of a discrete set of known data points. How it work? (x, y) x0 x1 y0 y1
  • 4. @ The graph means that it has 2 known points and I want to estimate another point by assume the value is linear. So the value I want to find is somewhere between the two known points. @ From the point that I want to find to point 0 (x0, y0) and to point 1 (x1, y1) is a the same line so the slope of the two line is the same or equal. y − y0 x −x0 = y1− y0 x1− x0 Let say I want to find the “x” when I know “y”  x = x0 + (x1 – x0) × y− y0 y1− y0 @ This interpolation formula will be used in the interpolation search to find a better mid_index. + Interpolation Search @ The expected result from the search algorithm is FOUND or NOT FOUND @ Time Complexity of Binary Search : O (log log n)) Since the improvement in on mid_index, so the better mid_index is mid_index = lowest + (highest – lowest) × integer I am searching −lowest interger highest integer −lowest interger How it work? Example: I have a sorted array (ascending) of integer like this -13, 0, 10, 17, 27, 100 Let find index of number “17” now using interpolation search instead of binary search Solution: Integer -13 0 10 17 27 100 Index 0 1 2 3 4 5 mid_index = 0 + (5 – 0) 17 −(−13) 100 −(−13) = 1.32743363 but we will get mid_index = 1 since index cannot be floating point value Integer value at index mid_index is 0 < 17 Set the lowest index equal to mid_index + 1 = 2 So find new mid_index = 2 + (5 – 2) 17 − 0 100 − 0 = 2.51 so take mid_index = 2 Integer -13 0 10 17 27 100 Index 0 1 2 3 4 5 Integer value at index mid_index is 10 < 17 Set the lowest index equal to mid_index + 1 = 3 So find new mid_index = 3 + (5 – 3) 17 −10 100 −10 = 3.15151515 so take mid_index = 3
  • 5. Integer -13 0 10 17 27 100 Index 0 1 2 3 4 5 Integer value at index mid_index is 17 = 17 (the number I am searching for) YEAH! The value is at index 3 @ If I do not found the value here the value of the lowest index might be larger than the value I want to search for, which is not logical. If so the value is not found.
  • 6. Reading Materials • https://en.wikipedia.org/wiki/Interpolation • https://en.wikipedia.org/wiki/Linear_interpolation • https://en.wikipedia.org/wiki/Binary_search_algorithm