SlideShare a Scribd company logo
3
Most read
4
Most read
11
Most read
using Java
2015
Data Structure
Prepared by: Mahmoud Rafeek Al-farra
in Java
3. Searching Algorithms
mfarra.cst.ps www.fb.com/MahmoudRFarra
Contents
Which is best ?
Binary Search approach
Linear search approach
Introduction
Introduction
mfarra.cst.ps www.fb.com/MahmoudRFarra
 Searching for data is a fundamental computer
programming task and one that has been studied for
many years.
There are two fundamental ways to search for data in
a list:
The sequential (linear) search approach.
The binary search approuach.
Linear search approach
mfarra.cst.ps www.fb.com/MahmoudRFarra
The linear search approach compares the key element
sequentially with each element in the data structure.
 It continues to do so until the key matches an
element in the DS or the DS is exhausted without a
match being found.
If a match is made, the linear search returns the
index of the element in the DS that matches the key.
 If no match is found, the search returns -1.
Linear search approach
mfarra.cst.ps www.fb.com/MahmoudRFarra
2 5 81 88 90 997 9 34 55 56 66 79 80
1 2 3 4 5 6 7 8 9 10 11 12 13
Wanted = 80
array
0
==‫؟‬
.... ....
The wanted value at comparison number 10 and position 9
You can see an animated example from the following link:
http://www.cs.armstrong.edu/liang/animation/web/LinearSearch.html
Linear search approach
mfarra.cst.ps www.fb.com/MahmoudRFarra
1. public class LinearSearch {
2. /** The method for finding a key in the list */
3. public static int linearSearch(int[] list, int key) {
4. for (int i = 0; i < list.length; i++) {
5. if (key == list[i])
6. return i;
7. }
8. return -1;
9. }
10. }
Binary search approach
mfarra.cst.ps www.fb.com/MahmoudRFarra
When the records you are searching through are
sorted into order, you can perform a more efficient
search than the sequential search to find a value. This
search is called a binary search.
To use this algorithm, we first need our data stored
in order (ascending, preferably) in an array.
You can see an animated example from the following link:
http://www.cs.armstrong.edu/liang/animation/web/BinarySearch.html
Binary Search approach
mfarra.cst.ps www.fb.com/MahmoudRFarra
Binary Search approach
2 5 81 88 90 997 9 34 55 56 66 79 80
1 2 3 4 5 6 7 8 9 10 11 12 13
(0+13) / 2 = 6
If (array[6] == 80)
return 6
Elseif (array[6] > 80)
High = 6 -1
Else
Low = 6+1
Middle = (low + high)/2
Wanted = 80
array
0
(7+13) / 2 = 10
If (array[10] == 80)
return 10
Elseif (array[10] > 80)
High = 10 -1
Else
Low = 10+1
(7+9) / 2 = 8
If (array[8] == 80)
return 8
Elseif (array[8] > 80)
High = 8-1
Else
Low = 8+1
1 2 3
mfarra.cst.ps www.fb.com/MahmoudRFarra
Binary Search approach
2 5 81 88 90 997 9 34 55 56 66 79 80
1 2 3 4 5 6 7 8 9 10 11 12 13
Middle = (low + high)/2
Wanted = 80
array
0
(7+9) / 2 = 8
If (array[8] == 80)
return 8
Elseif (array[8] > 80)
High = 8-1
Else
Low = 8+1
3
(9+9) / 2 = 9
If (array[9] == 80)
return 8
Elseif (array[9] > 80)
High = 9-1
Else
Low = 9+1
4
return 8
mfarra.cst.ps www.fb.com/MahmoudRFarra
Binary Search approach
mfarra.cst.ps www.fb.com/MahmoudRFarra
1. public class BinarySearch {
2. public static int binarySearch(int[] list, int key) {
3. int low = 0;
4. int high = list.length - 1;
5. while (high >= low) {
6. int mid = (low + high) / 2;
7. if (key < list[mid])
8. high = mid - 1;
9. else if (key == list[mid])
10. return mid;
11. else
12. low = mid + 1;
13. }
14. return –low - 1; // Now high < low, key not found
15. }
16. }
Which is best ?
mfarra.cst.ps www.fb.com/MahmoudRFarra
The
best
Complexity
Data
structure
Suitable
data
Size of
data
Which is best ?
mfarra.cst.ps www.fb.com/MahmoudRFarra
 If an array is sorted, binary search is more efficient
than linear search for finding an element in the array.
 Linear search is useful for finding an element in a
small array or an unsorted array, but it is inefficient
for large arrays.
 Binary search is more efficient, but it requires that
the array be presorted.
Which is best ?
mfarra.cst.ps www.fb.com/MahmoudRFarra
Complexity time of linear approach is: O(n)
Complexity time of binary approach is: O( log(n))
 Supported data structure of linear: ?
 Supported data structure of Binary: ?
using Java
2015
FB: M a h m o u d R F a r r a
YouTube: M a h m o u d R F a r
SlidesShare: mralfarra
Thank you

More Related Content

PPT
Queue Data Structure
PDF
Strings in java
PPT
Data Structures - Searching & sorting
PPTX
List interface in collections framework
PPT
SEARCHING AND SORTING ALGORITHMS
PDF
Java Thread Synchronization
PPT
PPTX
Queue Data Structure
Strings in java
Data Structures - Searching & sorting
List interface in collections framework
SEARCHING AND SORTING ALGORITHMS
Java Thread Synchronization

What's hot (20)

PPTX
Quick sort
PDF
Java Linked List Tutorial | Edureka
PPT
Collection Framework in java
PPTX
Java Stack Data Structure.pptx
PDF
Java threads
PPT
Queue implementation
PPT
Algorithm: Quick-Sort
PPTX
Binary search in data structure
PPT
Selection sort
PPT
3.3 shell sort
PPTX
Stacks in DATA STRUCTURE
PPTX
Data structure & its types
PPTX
Presentation on Elementary data structures
PPTX
Selection sort
PPTX
Complexity analysis in Algorithms
PPTX
Stack and Queue
PPT
Data Structure and Algorithms Binary Search Tree
PPTX
Graph traversals in Data Structures
PPTX
Insertion sort
Quick sort
Java Linked List Tutorial | Edureka
Collection Framework in java
Java Stack Data Structure.pptx
Java threads
Queue implementation
Algorithm: Quick-Sort
Binary search in data structure
Selection sort
3.3 shell sort
Stacks in DATA STRUCTURE
Data structure & its types
Presentation on Elementary data structures
Selection sort
Complexity analysis in Algorithms
Stack and Queue
Data Structure and Algorithms Binary Search Tree
Graph traversals in Data Structures
Insertion sort
Ad

Viewers also liked (20)

DOCX
25 java tough interview questions
PPTX
Java Sorting Algorithms
DOCX
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
PDF
Columna_bernik_Dic 2009pdf
PDF
私のちびっとタスク管理 - RxTstudy #6
PDF
Boas práticas para manipulação de alimentos
PPTX
UbD Sept 2016
PDF
Cuando las-mujeres-matan
PDF
ゆるキャラから学んだもの
PDF
Disneyから学んだこと
PDF
企業研究〜コカ・コーラ株式会社〜
PDF
R.D Nº 0520 2011-ED Procedimientos para el Desarrollo de Actividades de Capac...
PDF
Social Design for Urban Change
PPTX
Lecture 9 data structures and algorithms
PPTX
Большинство собственников не умеют управлять своими компаниями. Михаил Молоканов
PPTX
historia de madonna
ODP
Madonna Open Office
PPT
Fisiologia - Sistema Respiratório 3
PPT
3.4 selection sort
PPTX
Nutritional Rehabilitation
25 java tough interview questions
Java Sorting Algorithms
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
Columna_bernik_Dic 2009pdf
私のちびっとタスク管理 - RxTstudy #6
Boas práticas para manipulação de alimentos
UbD Sept 2016
Cuando las-mujeres-matan
ゆるキャラから学んだもの
Disneyから学んだこと
企業研究〜コカ・コーラ株式会社〜
R.D Nº 0520 2011-ED Procedimientos para el Desarrollo de Actividades de Capac...
Social Design for Urban Change
Lecture 9 data structures and algorithms
Большинство собственников не умеют управлять своими компаниями. Михаил Молоканов
historia de madonna
Madonna Open Office
Fisiologia - Sistema Respiratório 3
3.4 selection sort
Nutritional Rehabilitation
Ad

Similar to 3 searching algorithms in Java (20)

PDF
Searching and Sorting Techniques in Data Structure
PPTX
Presentation on Searching and Sorting in C language.pptx
PPTX
Introduction to Searching Algorithm for beginners
DOCX
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
PPT
Searching in c language
PPT
Searching algorithms
PPTX
Searching and Sorting Unit II Part I.pptx
PPTX
3.Problem Solving Techniques and Data Structures.pptx
PPTX
Rahat &amp; juhith
PPTX
Unit 2 Searching and Sorting Technique.pptx
PPTX
27631722026_T._TAJESWAR_RAO_PCC-CS301_CSE(CS).pptx
PPTX
Searching & Algorithms IN DATA STRUCTURES
PDF
searching
PPTX
unit 2 First.pptx Searching - Linear and Binary Search
PPTX
Presentation
DOCX
MODULE 5-Searching and-sorting
PPTX
Searching linear &amp; binary search
PPTX
data_structure_Chapter two_computer.pptx
PPTX
Searching in Data Structure
PPTX
Searching and Sorting Algorithms in Data Structures
Searching and Sorting Techniques in Data Structure
Presentation on Searching and Sorting in C language.pptx
Introduction to Searching Algorithm for beginners
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
Searching in c language
Searching algorithms
Searching and Sorting Unit II Part I.pptx
3.Problem Solving Techniques and Data Structures.pptx
Rahat &amp; juhith
Unit 2 Searching and Sorting Technique.pptx
27631722026_T._TAJESWAR_RAO_PCC-CS301_CSE(CS).pptx
Searching & Algorithms IN DATA STRUCTURES
searching
unit 2 First.pptx Searching - Linear and Binary Search
Presentation
MODULE 5-Searching and-sorting
Searching linear &amp; binary search
data_structure_Chapter two_computer.pptx
Searching in Data Structure
Searching and Sorting Algorithms in Data Structures

More from Mahmoud Alfarra (20)

PPT
Computer Programming, Loops using Java - part 2
PPT
Computer Programming, Loops using Java
PPT
Chapter 10: hashing data structure
PPT
Chapter9 graph data structure
PPT
Chapter 8: tree data structure
PPT
Chapter 7: Queue data structure
PPT
Chapter 6: stack data structure
PPT
Chapter 5: linked list data structure
PPT
Chapter 4: basic search algorithms data structure
PPT
Chapter 3: basic sorting algorithms data structure
PPT
Chapter 2: array and array list data structure
PPT
Chapter1 intro toprincipleofc#_datastructure_b_cs
PPT
Chapter 0: introduction to data structure
PPTX
3 classification
PPT
8 programming-using-java decision-making practices 20102011
PPT
7 programming-using-java decision-making220102011
PPT
6 programming-using-java decision-making20102011-
PPT
5 programming-using-java intro-tooop20102011
PPT
4 programming-using-java intro-tojava20102011
PPT
3 programming-using-java introduction-to computer
Computer Programming, Loops using Java - part 2
Computer Programming, Loops using Java
Chapter 10: hashing data structure
Chapter9 graph data structure
Chapter 8: tree data structure
Chapter 7: Queue data structure
Chapter 6: stack data structure
Chapter 5: linked list data structure
Chapter 4: basic search algorithms data structure
Chapter 3: basic sorting algorithms data structure
Chapter 2: array and array list data structure
Chapter1 intro toprincipleofc#_datastructure_b_cs
Chapter 0: introduction to data structure
3 classification
8 programming-using-java decision-making practices 20102011
7 programming-using-java decision-making220102011
6 programming-using-java decision-making20102011-
5 programming-using-java intro-tooop20102011
4 programming-using-java intro-tojava20102011
3 programming-using-java introduction-to computer

Recently uploaded (20)

PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
master seminar digital applications in india
PPTX
Pharma ospi slides which help in ospi learning
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
01-Introduction-to-Information-Management.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
Open Quiz Monsoon Mind Game Prelims.pptx
human mycosis Human fungal infections are called human mycosis..pptx
102 student loan defaulters named and shamed – Is someone you know on the list?
Microbial diseases, their pathogenesis and prophylaxis
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
master seminar digital applications in india
Pharma ospi slides which help in ospi learning
The Final Stretch: How to Release a Game and Not Die in the Process.
Open Quiz Monsoon Mind Game Final Set.pptx
Microbial disease of the cardiovascular and lymphatic systems
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
01-Introduction-to-Information-Management.pdf
Anesthesia in Laparoscopic Surgery in India
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Pharmacology of Heart Failure /Pharmacotherapy of CHF
TR - Agricultural Crops Production NC III.pdf
Open Quiz Monsoon Mind Game Prelims.pptx

3 searching algorithms in Java

  • 1. using Java 2015 Data Structure Prepared by: Mahmoud Rafeek Al-farra in Java 3. Searching Algorithms
  • 2. mfarra.cst.ps www.fb.com/MahmoudRFarra Contents Which is best ? Binary Search approach Linear search approach Introduction
  • 3. Introduction mfarra.cst.ps www.fb.com/MahmoudRFarra  Searching for data is a fundamental computer programming task and one that has been studied for many years. There are two fundamental ways to search for data in a list: The sequential (linear) search approach. The binary search approuach.
  • 4. Linear search approach mfarra.cst.ps www.fb.com/MahmoudRFarra The linear search approach compares the key element sequentially with each element in the data structure.  It continues to do so until the key matches an element in the DS or the DS is exhausted without a match being found. If a match is made, the linear search returns the index of the element in the DS that matches the key.  If no match is found, the search returns -1.
  • 5. Linear search approach mfarra.cst.ps www.fb.com/MahmoudRFarra 2 5 81 88 90 997 9 34 55 56 66 79 80 1 2 3 4 5 6 7 8 9 10 11 12 13 Wanted = 80 array 0 ==‫؟‬ .... .... The wanted value at comparison number 10 and position 9 You can see an animated example from the following link: http://www.cs.armstrong.edu/liang/animation/web/LinearSearch.html
  • 6. Linear search approach mfarra.cst.ps www.fb.com/MahmoudRFarra 1. public class LinearSearch { 2. /** The method for finding a key in the list */ 3. public static int linearSearch(int[] list, int key) { 4. for (int i = 0; i < list.length; i++) { 5. if (key == list[i]) 6. return i; 7. } 8. return -1; 9. } 10. }
  • 7. Binary search approach mfarra.cst.ps www.fb.com/MahmoudRFarra When the records you are searching through are sorted into order, you can perform a more efficient search than the sequential search to find a value. This search is called a binary search. To use this algorithm, we first need our data stored in order (ascending, preferably) in an array. You can see an animated example from the following link: http://www.cs.armstrong.edu/liang/animation/web/BinarySearch.html
  • 8. Binary Search approach mfarra.cst.ps www.fb.com/MahmoudRFarra
  • 9. Binary Search approach 2 5 81 88 90 997 9 34 55 56 66 79 80 1 2 3 4 5 6 7 8 9 10 11 12 13 (0+13) / 2 = 6 If (array[6] == 80) return 6 Elseif (array[6] > 80) High = 6 -1 Else Low = 6+1 Middle = (low + high)/2 Wanted = 80 array 0 (7+13) / 2 = 10 If (array[10] == 80) return 10 Elseif (array[10] > 80) High = 10 -1 Else Low = 10+1 (7+9) / 2 = 8 If (array[8] == 80) return 8 Elseif (array[8] > 80) High = 8-1 Else Low = 8+1 1 2 3 mfarra.cst.ps www.fb.com/MahmoudRFarra
  • 10. Binary Search approach 2 5 81 88 90 997 9 34 55 56 66 79 80 1 2 3 4 5 6 7 8 9 10 11 12 13 Middle = (low + high)/2 Wanted = 80 array 0 (7+9) / 2 = 8 If (array[8] == 80) return 8 Elseif (array[8] > 80) High = 8-1 Else Low = 8+1 3 (9+9) / 2 = 9 If (array[9] == 80) return 8 Elseif (array[9] > 80) High = 9-1 Else Low = 9+1 4 return 8 mfarra.cst.ps www.fb.com/MahmoudRFarra
  • 11. Binary Search approach mfarra.cst.ps www.fb.com/MahmoudRFarra 1. public class BinarySearch { 2. public static int binarySearch(int[] list, int key) { 3. int low = 0; 4. int high = list.length - 1; 5. while (high >= low) { 6. int mid = (low + high) / 2; 7. if (key < list[mid]) 8. high = mid - 1; 9. else if (key == list[mid]) 10. return mid; 11. else 12. low = mid + 1; 13. } 14. return –low - 1; // Now high < low, key not found 15. } 16. }
  • 12. Which is best ? mfarra.cst.ps www.fb.com/MahmoudRFarra The best Complexity Data structure Suitable data Size of data
  • 13. Which is best ? mfarra.cst.ps www.fb.com/MahmoudRFarra  If an array is sorted, binary search is more efficient than linear search for finding an element in the array.  Linear search is useful for finding an element in a small array or an unsorted array, but it is inefficient for large arrays.  Binary search is more efficient, but it requires that the array be presorted.
  • 14. Which is best ? mfarra.cst.ps www.fb.com/MahmoudRFarra Complexity time of linear approach is: O(n) Complexity time of binary approach is: O( log(n))  Supported data structure of linear: ?  Supported data structure of Binary: ?
  • 15. using Java 2015 FB: M a h m o u d R F a r r a YouTube: M a h m o u d R F a r SlidesShare: mralfarra Thank you