SlideShare a Scribd company logo
1 of 18
‫الرحيم‬‫حمن‬‫الر‬‫هللا‬ ‫بسم‬
Bayan College
For
Science & Technology
Computer Science 6
Binary Search
binary search
Prepared by
:
Mohamed siddig
Tahani hassan
Rana seid
introduction
Problem:
Given a list of n numbers and q queries, find out whet
her each query is among the list of n numbers.
Analysis:
The solution is to use a linear search, where, for each
query k , we check each of then n numbers to see if k
is one of them. this o(n) algorithm will often be too slow.
we could use complicated data structures such as a set
or a hash table, but a much easier solution is to
implement a binary search
1
A binary search
is a divide-and-conquer .we algorithm divide the list in
half and search from only half of the list for future
iterations.
Objective: -
Find the value of the key to the matrix x .
The idea: -
This algorithm is based on binary search in the matrix
x. where the search begins from the element, which is
located in the center of the matrix, and every time I
compare it with the key so that, if the two values ​​are
equal,
this
indicates that the key is to find values ​​in the matrix, either if
the key value is less of the value of the item east settec
the search in the left part of the matrix and in the case of
adverse will be searched in the right part of the matrix,
and so on ... So we get a matrix consisting of one box a
value equal to the key or different about him.
3
The steps of an algorithm:
1. sort array descending or ascending
2. Set low=0 and high=length-1
3. If low not smaller or not equal high go to 8:
4. Set middle =(low + high)/2
5. If target equal array [middle] go to 9
6. If target large then array[mid] set low =mid+1 t and go to 3.
7. If target smaller than array[mid] set high=mid-1 and go to 3
8. Print target not found and go 10
9. return middle
10. End function
4
The first way
5
Code :
int binary_search(int a[], int low, int high, int target)
{
while (low <= high)
{
int middle = (low + high)/2;
If(target==a[middle] )
return middle;
else if (target < a[middle])
high = middle - 1;
else if (target > a[middle])
low = middle + 1;
}
return -1;
}
6
example: -
Look for value 33 in the following matrix:
7
Solution
1) define the middle of the matrix:
8
2) matrix divided into two parts according to
Mead site: -
9
3) Since 33 is smaller than 53, the search
process will be carried out on the first half:
10
4) we work on the first half division and
become the med value is equal to: -
11
5) Since 33 is greater than 25, the first half
will be excluded and the search continues
in the second half
1
2
6) again, we re-partitioning process where
values ​​become med equal to 5
7) Akhiranjd the value of 33 found in the site
if and which equals 4.
1
3
Best case and worst case :
·The best case for a binary search is finding the
target item on the first look
.The worst case for a binary search is searching for
an item which is not in the data or in last look .
1
4
Application :
binary search is now used in 99% of 3D games
and applications
15
Reference:
https://www.princeton.edu
http://www.algolist.net
http://stackoverflow.com
1
6

More Related Content

What's hot

Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureBalwant Gorad
 
Binary search in ds
Binary search in dsBinary search in ds
Binary search in dschauhankapil
 
Using Word Embedding for Automatic Query Expansion
Using Word Embedding for Automatic Query ExpansionUsing Word Embedding for Automatic Query Expansion
Using Word Embedding for Automatic Query ExpansionDwaipayan Roy
 
Advance algorithm hashing lec II
Advance algorithm hashing lec IIAdvance algorithm hashing lec II
Advance algorithm hashing lec IISajid Marwat
 
Coalesced hashing / Hash Coalescido
Coalesced hashing / Hash CoalescidoCoalesced hashing / Hash Coalescido
Coalesced hashing / Hash CoalescidoCriatividadeZeroDocs
 
Data Structures : hashing (1)
Data Structures : hashing (1)Data Structures : hashing (1)
Data Structures : hashing (1)Home
 
Advance algorithm hashing lec I
Advance algorithm hashing lec IAdvance algorithm hashing lec I
Advance algorithm hashing lec ISajid Marwat
 

What's hot (12)

Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data Structure
 
Searching
SearchingSearching
Searching
 
Binary search
Binary searchBinary search
Binary search
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
 
Binary search in ds
Binary search in dsBinary search in ds
Binary search in ds
 
Using Word Embedding for Automatic Query Expansion
Using Word Embedding for Automatic Query ExpansionUsing Word Embedding for Automatic Query Expansion
Using Word Embedding for Automatic Query Expansion
 
Advance algorithm hashing lec II
Advance algorithm hashing lec IIAdvance algorithm hashing lec II
Advance algorithm hashing lec II
 
SPSF01 - Algorithm
SPSF01 - AlgorithmSPSF01 - Algorithm
SPSF01 - Algorithm
 
Coalesced hashing / Hash Coalescido
Coalesced hashing / Hash CoalescidoCoalesced hashing / Hash Coalescido
Coalesced hashing / Hash Coalescido
 
Data Structures : hashing (1)
Data Structures : hashing (1)Data Structures : hashing (1)
Data Structures : hashing (1)
 
Advance algorithm hashing lec I
Advance algorithm hashing lec IAdvance algorithm hashing lec I
Advance algorithm hashing lec I
 
Data structure Unit-I Part-C
Data structure Unit-I Part-CData structure Unit-I Part-C
Data structure Unit-I Part-C
 

Similar to Binary search2

Similar to Binary search2 (20)

Lecture_Oct26.pptx
Lecture_Oct26.pptxLecture_Oct26.pptx
Lecture_Oct26.pptx
 
Unit 8 searching and hashing
Unit   8 searching and hashingUnit   8 searching and hashing
Unit 8 searching and hashing
 
Rahat &amp; juhith
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhith
 
Linear and Binary Search
Linear and Binary SearchLinear and Binary Search
Linear and Binary Search
 
Unit viii searching and hashing
Unit   viii searching and hashing Unit   viii searching and hashing
Unit viii searching and hashing
 
UNIT V.docx
UNIT V.docxUNIT V.docx
UNIT V.docx
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
 
Chapter 4: basic search algorithms data structure
Chapter 4: basic search algorithms data structureChapter 4: basic search algorithms data structure
Chapter 4: basic search algorithms data structure
 
CSA 2001 (Module-2).pptx
CSA 2001 (Module-2).pptxCSA 2001 (Module-2).pptx
CSA 2001 (Module-2).pptx
 
Chapter 11 ds
Chapter 11 dsChapter 11 ds
Chapter 11 ds
 
searching techniques.pptx
searching techniques.pptxsearching techniques.pptx
searching techniques.pptx
 
Artificial Intelligence_Searching.pptx
Artificial Intelligence_Searching.pptxArtificial Intelligence_Searching.pptx
Artificial Intelligence_Searching.pptx
 
Unit iv(dsc++)
Unit iv(dsc++)Unit iv(dsc++)
Unit iv(dsc++)
 
UNIT IV -Data Structures.pdf
UNIT IV -Data Structures.pdfUNIT IV -Data Structures.pdf
UNIT IV -Data Structures.pdf
 
Data Structures Design Notes.pdf
Data Structures Design Notes.pdfData Structures Design Notes.pdf
Data Structures Design Notes.pdf
 
algorithm Unit 2
algorithm Unit 2 algorithm Unit 2
algorithm Unit 2
 
Unit 2 in daa
Unit 2 in daaUnit 2 in daa
Unit 2 in daa
 
Binary search Algorithm
Binary search AlgorithmBinary search Algorithm
Binary search Algorithm
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
 
(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting
 

Binary search2

  • 2. binary search Prepared by : Mohamed siddig Tahani hassan Rana seid
  • 3. introduction Problem: Given a list of n numbers and q queries, find out whet her each query is among the list of n numbers. Analysis: The solution is to use a linear search, where, for each query k , we check each of then n numbers to see if k is one of them. this o(n) algorithm will often be too slow. we could use complicated data structures such as a set or a hash table, but a much easier solution is to implement a binary search 1
  • 4. A binary search is a divide-and-conquer .we algorithm divide the list in half and search from only half of the list for future iterations. Objective: - Find the value of the key to the matrix x . The idea: - This algorithm is based on binary search in the matrix x. where the search begins from the element, which is located in the center of the matrix, and every time I compare it with the key so that, if the two values ​​are equal, this
  • 5. indicates that the key is to find values ​​in the matrix, either if the key value is less of the value of the item east settec the search in the left part of the matrix and in the case of adverse will be searched in the right part of the matrix, and so on ... So we get a matrix consisting of one box a value equal to the key or different about him. 3
  • 6. The steps of an algorithm: 1. sort array descending or ascending 2. Set low=0 and high=length-1 3. If low not smaller or not equal high go to 8: 4. Set middle =(low + high)/2 5. If target equal array [middle] go to 9 6. If target large then array[mid] set low =mid+1 t and go to 3. 7. If target smaller than array[mid] set high=mid-1 and go to 3 8. Print target not found and go 10 9. return middle 10. End function 4
  • 8. Code : int binary_search(int a[], int low, int high, int target) { while (low <= high) { int middle = (low + high)/2; If(target==a[middle] ) return middle; else if (target < a[middle]) high = middle - 1; else if (target > a[middle]) low = middle + 1; } return -1; } 6
  • 9. example: - Look for value 33 in the following matrix: 7
  • 10. Solution 1) define the middle of the matrix: 8
  • 11. 2) matrix divided into two parts according to Mead site: - 9
  • 12. 3) Since 33 is smaller than 53, the search process will be carried out on the first half: 10
  • 13. 4) we work on the first half division and become the med value is equal to: - 11
  • 14. 5) Since 33 is greater than 25, the first half will be excluded and the search continues in the second half 1 2
  • 15. 6) again, we re-partitioning process where values ​​become med equal to 5 7) Akhiranjd the value of 33 found in the site if and which equals 4. 1 3
  • 16. Best case and worst case : ·The best case for a binary search is finding the target item on the first look .The worst case for a binary search is searching for an item which is not in the data or in last look . 1 4
  • 17. Application : binary search is now used in 99% of 3D games and applications 15