SlideShare a Scribd company logo
DATA STRUCTURE
Chapter 4: Basic Search
Algorithms
Prepared & Presented by
Mr. Mahmoud R. Alfarra
2010-2011
College of Science & Technology
Dep. Of Computer Science & IT
BCs of Information Technology
http://mfarra.cst.ps
Out Line
 Introduction
 Sequential Search Algorithm
 Binary Search Algorithm
 Which is the best ?
 Search and sorting in Array List
2
Introduction
 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 search and the
binary search.
3
Sequential Search
Algorithm
4
 The most obvious type of search is to begin at
the beginning of a set of records and move
through each record until you find the record
you are looking for or you come to the end of
the records. This is called a sequential search.
 A sequential search (also called a linear
search) is very easy to implement.
Sequential Search
Algorithm
5
2 5 81 88 90 99
7 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
Sequential Search Algorithm
6
1. static void Main(string[] args)
2. { int[] a = { 10, 2, 34, 4, 3, 1, 100 };
3. int wanted = 1;
4. int target_index=-1;
5. for (int i = 0; i < a.Length; i++)
6. if (a[i] == wanted) {
7. target_index = i;
8. break; }
9. if (target_index >= 0)
10. Console.WriteLine(" The wanted value exist at
position: "+(target_index+1)+"'th");
11. Console.Read(); }
Practice: Reprogram
this problem using
string data.
Binary Search Algorithm
7
 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.
Binary Search Algorithm
8
Binary Search Algorithm
9
2 5 81 88 90 99
7 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
Binary Search Algorithm
10
2 5 81 88 90 99
7 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
Binary Search Algorithm
11
‫البيانات‬ ‫تراكيب‬ ‫مساق‬
‫إعداد‬ ‫العلمية‬ ‫المادة‬
/
‫أ‬
.
‫ا‬ َّ‫الفــر‬ ‫رفيق‬ ‫محمود‬
1. static void Main(string[] args) {
2. int[] a = { 2, 3, 5, 8, 10, 23, 30};
3. int wanted = 23;
4. int start = 0;
5. int last = a.Length - 1;
6. int middle = (last + start) / 2;
7. int result =0;
8. while (start < last) {
9. middle = (last + start) / 2;
10. if (a[middle] == wanted) {
11. result = 1;
12. break; }
13. else if (a[middle] < wanted)
14. start = middle + 1;
15. else
16. last = middle - 1; }
17. if (result == 1)
18. Console.WriteLine("The wanted element is exist at: " +
middle);
19. else
20. Console.WriteLine("The wanted element does not exist ");
To understand:
trace step by step
To practice: try with
string data
More practice: try with
2-D array
Which is the best ?
12
 Sequential search is used when the items in
the list are in random order.
 Binary search is used when the items are
sorted in the list.
Search and sorting in Array List
13
1. static void Main(string[] args) {
2. ArrayList a = new ArrayList();
3. a.Add(20);
4. a.Add(4);
5. a.Add(3);
6. a.Sort();
7. int result = a.BinarySearch(3);
8. Console.WriteLine(" position of 3 at : " + result);
9. result = a.BinarySearch(7);
10. Console.WriteLine(" position of 7 at : " + result);
11. Console.WriteLine();
12. foreach (object x in a)
13. Console.WriteLine(" " + x);
14. Console.ReadLine(); }
Thank You …
14
Remember that: question is the key of knowledge
Ahl Eljanna 

َ‫ق‬َ
‫د‬َ
‫ص‬ ‫ي‬ّ
‫ذ‬‫ه‬‫ل‬‫ا‬ ّ‫ه‬ّ
‫َلِل‬ ُ
‫د‬‫ح‬
‫م‬َ‫ح‬
‫ْل‬‫ا‬ ‫وا‬ُ‫ل‬‫ا‬َ‫ق‬َ
‫و‬
‫ا‬ ‫ا‬َ‫ن‬َ‫ث‬َ
‫ر‬‫ح‬
‫َو‬‫أ‬َ
‫و‬ ُ‫ه‬َ
‫د‬‫ح‬‫ع‬َ
‫و‬ ‫ا‬َ‫ن‬
ُ‫أ‬‫ه‬
‫و‬َ‫ب‬َ‫ت‬َ‫ن‬ َ
‫ض‬‫ح‬
‫ر‬َ‫ح‬
‫ْل‬
َ
‫م‬‫ح‬‫ع‬ّ‫ن‬َ‫ف‬ ُ‫اء‬َ
‫ش‬َ‫ن‬ ُ
‫ث‬‫ح‬‫ي‬َ
‫ح‬ ّ
‫هة‬‫ن‬َ‫ح‬
‫ْل‬‫ا‬ ‫ح‬
‫ن‬ّ
‫م‬
َ
‫ي‬ّ‫ل‬ّ
‫ام‬َ
‫حع‬‫ل‬‫ا‬ ُ
‫ر‬‫ح‬
‫َج‬‫أ‬
15
Ad

More Related Content

What's hot (20)

3 searching algorithms in Java
3 searching algorithms in Java3 searching algorithms in Java
3 searching algorithms in Java
Mahmoud Alfarra
 
3 Array operations
3   Array operations3   Array operations
3 Array operations
Mahmoud Alfarra
 
Stack Implementation
Stack ImplementationStack Implementation
Stack Implementation
Zidny Nafan
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked List
PTCL
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Lovely Professional University
 
Data structure stack&queue basics
Data structure stack&queue   basicsData structure stack&queue   basics
Data structure stack&queue basics
Selvin Josy Bai Somu
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
Apurbo Datta
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Zidny Nafan
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
nikshaikh786
 
Control statements
Control statementsControl statements
Control statements
Pramod Rathore
 
Priority Queue
Priority QueuePriority Queue
Priority Queue
Joyjit Choudhury
 
Stacks
StacksStacks
Stacks
Sadaf Ismail
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
pinakspatel
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
somendra kumar
 
Queue
QueueQueue
Queue
Nabeel Ahsen
 
Stacks, Queues, Deques
Stacks, Queues, DequesStacks, Queues, Deques
Stacks, Queues, Deques
A-Tech and Software Development
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queue
Senthil Kumar
 
Presentation on queue
Presentation on queuePresentation on queue
Presentation on queue
Rojan Pariyar
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queue
Rai University
 
stacks in algorithems and data structure
stacks in algorithems and data structurestacks in algorithems and data structure
stacks in algorithems and data structure
faran nawaz
 
3 searching algorithms in Java
3 searching algorithms in Java3 searching algorithms in Java
3 searching algorithms in Java
Mahmoud Alfarra
 
Stack Implementation
Stack ImplementationStack Implementation
Stack Implementation
Zidny Nafan
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked List
PTCL
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Zidny Nafan
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
nikshaikh786
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
pinakspatel
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
somendra kumar
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queue
Senthil Kumar
 
Presentation on queue
Presentation on queuePresentation on queue
Presentation on queue
Rojan Pariyar
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queue
Rai University
 
stacks in algorithems and data structure
stacks in algorithems and data structurestacks in algorithems and data structure
stacks in algorithems and data structure
faran nawaz
 

Similar to Chapter 4: basic search algorithms data structure (20)

Chapter 11 ds
Chapter 11 dsChapter 11 ds
Chapter 11 ds
Hanif Durad
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
UNIT V.docx
UNIT V.docxUNIT V.docx
UNIT V.docx
Revathiparamanathan
 
Algorithm and Programming (Searching)
Algorithm and Programming (Searching)Algorithm and Programming (Searching)
Algorithm and Programming (Searching)
Adam Mukharil Bachtiar
 
UNIT IV -Data Structures.pdf
UNIT IV -Data Structures.pdfUNIT IV -Data Structures.pdf
UNIT IV -Data Structures.pdf
KPRevathiAsstprofITD
 
Searching
SearchingSearching
Searching
A. S. M. Shafi
 
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
 
object oriented programming lab manual .docx
object oriented programming  lab manual .docxobject oriented programming  lab manual .docx
object oriented programming lab manual .docx
Kirubaburi R
 
data stracyturwe waaure semeer gorbe eidd fata sahttacuyeiwi
data stracyturwe waaure  semeer gorbe eidd  fata  sahttacuyeiwidata stracyturwe waaure  semeer gorbe eidd  fata  sahttacuyeiwi
data stracyturwe waaure semeer gorbe eidd fata sahttacuyeiwi
husseindabdi1
 
16-sorting.ppt
16-sorting.ppt16-sorting.ppt
16-sorting.ppt
18Gunaalanpg
 
9 Arrays
9 Arrays9 Arrays
9 Arrays
Praveen M Jigajinni
 
Chapter #3 (Searchinmmmg ALgorithm).pptx
Chapter #3 (Searchinmmmg ALgorithm).pptxChapter #3 (Searchinmmmg ALgorithm).pptx
Chapter #3 (Searchinmmmg ALgorithm).pptx
hekmatyarzahir44
 
21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx
reddy19841
 
IRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching AlgorithmsIRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching Algorithms
IRJET Journal
 
array lecture engineeringinformatin_technology.pptx
array lecture engineeringinformatin_technology.pptxarray lecture engineeringinformatin_technology.pptx
array lecture engineeringinformatin_technology.pptx
aihamjassar
 
Array ADT(Abstract Data Type)|Data Structure
Array ADT(Abstract Data Type)|Data StructureArray ADT(Abstract Data Type)|Data Structure
Array ADT(Abstract Data Type)|Data Structure
Akash Gaur
 
DSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdfDSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdf
MustafaJutt4
 
Data Structure Searching.pptx
Data Structure Searching.pptxData Structure Searching.pptx
Data Structure Searching.pptx
SourabhTaneja4
 
Chapter 14
Chapter 14Chapter 14
Chapter 14
Terry Yoast
 
07+08slide.pptx
07+08slide.pptx07+08slide.pptx
07+08slide.pptx
MURADSANJOUM
 
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
 
object oriented programming lab manual .docx
object oriented programming  lab manual .docxobject oriented programming  lab manual .docx
object oriented programming lab manual .docx
Kirubaburi R
 
data stracyturwe waaure semeer gorbe eidd fata sahttacuyeiwi
data stracyturwe waaure  semeer gorbe eidd  fata  sahttacuyeiwidata stracyturwe waaure  semeer gorbe eidd  fata  sahttacuyeiwi
data stracyturwe waaure semeer gorbe eidd fata sahttacuyeiwi
husseindabdi1
 
Chapter #3 (Searchinmmmg ALgorithm).pptx
Chapter #3 (Searchinmmmg ALgorithm).pptxChapter #3 (Searchinmmmg ALgorithm).pptx
Chapter #3 (Searchinmmmg ALgorithm).pptx
hekmatyarzahir44
 
21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx
reddy19841
 
IRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching AlgorithmsIRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching Algorithms
IRJET Journal
 
array lecture engineeringinformatin_technology.pptx
array lecture engineeringinformatin_technology.pptxarray lecture engineeringinformatin_technology.pptx
array lecture engineeringinformatin_technology.pptx
aihamjassar
 
Array ADT(Abstract Data Type)|Data Structure
Array ADT(Abstract Data Type)|Data StructureArray ADT(Abstract Data Type)|Data Structure
Array ADT(Abstract Data Type)|Data Structure
Akash Gaur
 
DSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdfDSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdf
MustafaJutt4
 
Data Structure Searching.pptx
Data Structure Searching.pptxData Structure Searching.pptx
Data Structure Searching.pptx
SourabhTaneja4
 
Ad

More from Mahmoud Alfarra (20)

Computer Programming, Loops using Java - part 2
Computer Programming, Loops using Java - part 2Computer Programming, Loops using Java - part 2
Computer Programming, Loops using Java - part 2
Mahmoud Alfarra
 
Computer Programming, Loops using Java
Computer Programming, Loops using JavaComputer Programming, Loops using Java
Computer Programming, Loops using Java
Mahmoud Alfarra
 
Chapter9 graph data structure
Chapter9  graph data structureChapter9  graph data structure
Chapter9 graph data structure
Mahmoud Alfarra
 
Chapter 8: tree data structure
Chapter 8:  tree data structureChapter 8:  tree data structure
Chapter 8: tree data structure
Mahmoud Alfarra
 
Chapter 2: array and array list data structure
Chapter 2: array and array list  data structureChapter 2: array and array list  data structure
Chapter 2: array and array list data structure
Mahmoud Alfarra
 
Chapter 0: introduction to data structure
Chapter 0: introduction to data structureChapter 0: introduction to data structure
Chapter 0: introduction to data structure
Mahmoud Alfarra
 
3 classification
3  classification3  classification
3 classification
Mahmoud Alfarra
 
8 programming-using-java decision-making practices 20102011
8 programming-using-java decision-making practices 201020118 programming-using-java decision-making practices 20102011
8 programming-using-java decision-making practices 20102011
Mahmoud Alfarra
 
7 programming-using-java decision-making220102011
7 programming-using-java decision-making2201020117 programming-using-java decision-making220102011
7 programming-using-java decision-making220102011
Mahmoud Alfarra
 
6 programming-using-java decision-making20102011-
6 programming-using-java decision-making20102011-6 programming-using-java decision-making20102011-
6 programming-using-java decision-making20102011-
Mahmoud Alfarra
 
5 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop201020115 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop20102011
Mahmoud Alfarra
 
4 programming-using-java intro-tojava20102011
4 programming-using-java intro-tojava201020114 programming-using-java intro-tojava20102011
4 programming-using-java intro-tojava20102011
Mahmoud Alfarra
 
3 programming-using-java introduction-to computer
3 programming-using-java introduction-to computer3 programming-using-java introduction-to computer
3 programming-using-java introduction-to computer
Mahmoud Alfarra
 
2 programming-using-java how to built application
2 programming-using-java how to built application2 programming-using-java how to built application
2 programming-using-java how to built application
Mahmoud Alfarra
 
1 programming-using-java -introduction
1 programming-using-java -introduction1 programming-using-java -introduction
1 programming-using-java -introduction
Mahmoud Alfarra
 
تلخيص النصوص تلقائيا
تلخيص النصوص تلقائياتلخيص النصوص تلقائيا
تلخيص النصوص تلقائيا
Mahmoud Alfarra
 
4×4×4 لتحصيل التميز
4×4×4 لتحصيل التميز4×4×4 لتحصيل التميز
4×4×4 لتحصيل التميز
Mahmoud Alfarra
 
Data preparation and processing chapter 2
Data preparation and processing chapter  2Data preparation and processing chapter  2
Data preparation and processing chapter 2
Mahmoud Alfarra
 
Introduction to-data-mining chapter 1
Introduction to-data-mining  chapter 1Introduction to-data-mining  chapter 1
Introduction to-data-mining chapter 1
Mahmoud Alfarra
 
Graph-Based Technique for Extracting Keyphrases In a Single-Document (GTEK)
Graph-Based Technique for Extracting Keyphrases In a Single-Document (GTEK)Graph-Based Technique for Extracting Keyphrases In a Single-Document (GTEK)
Graph-Based Technique for Extracting Keyphrases In a Single-Document (GTEK)
Mahmoud Alfarra
 
Computer Programming, Loops using Java - part 2
Computer Programming, Loops using Java - part 2Computer Programming, Loops using Java - part 2
Computer Programming, Loops using Java - part 2
Mahmoud Alfarra
 
Computer Programming, Loops using Java
Computer Programming, Loops using JavaComputer Programming, Loops using Java
Computer Programming, Loops using Java
Mahmoud Alfarra
 
Chapter9 graph data structure
Chapter9  graph data structureChapter9  graph data structure
Chapter9 graph data structure
Mahmoud Alfarra
 
Chapter 8: tree data structure
Chapter 8:  tree data structureChapter 8:  tree data structure
Chapter 8: tree data structure
Mahmoud Alfarra
 
Chapter 2: array and array list data structure
Chapter 2: array and array list  data structureChapter 2: array and array list  data structure
Chapter 2: array and array list data structure
Mahmoud Alfarra
 
Chapter 0: introduction to data structure
Chapter 0: introduction to data structureChapter 0: introduction to data structure
Chapter 0: introduction to data structure
Mahmoud Alfarra
 
8 programming-using-java decision-making practices 20102011
8 programming-using-java decision-making practices 201020118 programming-using-java decision-making practices 20102011
8 programming-using-java decision-making practices 20102011
Mahmoud Alfarra
 
7 programming-using-java decision-making220102011
7 programming-using-java decision-making2201020117 programming-using-java decision-making220102011
7 programming-using-java decision-making220102011
Mahmoud Alfarra
 
6 programming-using-java decision-making20102011-
6 programming-using-java decision-making20102011-6 programming-using-java decision-making20102011-
6 programming-using-java decision-making20102011-
Mahmoud Alfarra
 
5 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop201020115 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop20102011
Mahmoud Alfarra
 
4 programming-using-java intro-tojava20102011
4 programming-using-java intro-tojava201020114 programming-using-java intro-tojava20102011
4 programming-using-java intro-tojava20102011
Mahmoud Alfarra
 
3 programming-using-java introduction-to computer
3 programming-using-java introduction-to computer3 programming-using-java introduction-to computer
3 programming-using-java introduction-to computer
Mahmoud Alfarra
 
2 programming-using-java how to built application
2 programming-using-java how to built application2 programming-using-java how to built application
2 programming-using-java how to built application
Mahmoud Alfarra
 
1 programming-using-java -introduction
1 programming-using-java -introduction1 programming-using-java -introduction
1 programming-using-java -introduction
Mahmoud Alfarra
 
تلخيص النصوص تلقائيا
تلخيص النصوص تلقائياتلخيص النصوص تلقائيا
تلخيص النصوص تلقائيا
Mahmoud Alfarra
 
4×4×4 لتحصيل التميز
4×4×4 لتحصيل التميز4×4×4 لتحصيل التميز
4×4×4 لتحصيل التميز
Mahmoud Alfarra
 
Data preparation and processing chapter 2
Data preparation and processing chapter  2Data preparation and processing chapter  2
Data preparation and processing chapter 2
Mahmoud Alfarra
 
Introduction to-data-mining chapter 1
Introduction to-data-mining  chapter 1Introduction to-data-mining  chapter 1
Introduction to-data-mining chapter 1
Mahmoud Alfarra
 
Graph-Based Technique for Extracting Keyphrases In a Single-Document (GTEK)
Graph-Based Technique for Extracting Keyphrases In a Single-Document (GTEK)Graph-Based Technique for Extracting Keyphrases In a Single-Document (GTEK)
Graph-Based Technique for Extracting Keyphrases In a Single-Document (GTEK)
Mahmoud Alfarra
 
Ad

Recently uploaded (20)

Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
Handling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptxHandling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptx
AuthorAIDNationalRes
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdfBiophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
PKLI-Institute of Nursing and Allied Health Sciences Lahore , Pakistan.
 
Operations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdfOperations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdf
Arab Academy for Science, Technology and Maritime Transport
 
New Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptxNew Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptx
milanasargsyan5
 
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
Handling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptxHandling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptx
AuthorAIDNationalRes
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
New Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptxNew Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptx
milanasargsyan5
 

Chapter 4: basic search algorithms data structure

  • 1. DATA STRUCTURE Chapter 4: Basic Search Algorithms Prepared & Presented by Mr. Mahmoud R. Alfarra 2010-2011 College of Science & Technology Dep. Of Computer Science & IT BCs of Information Technology http://mfarra.cst.ps
  • 2. Out Line  Introduction  Sequential Search Algorithm  Binary Search Algorithm  Which is the best ?  Search and sorting in Array List 2
  • 3. Introduction  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 search and the binary search. 3
  • 4. Sequential Search Algorithm 4  The most obvious type of search is to begin at the beginning of a set of records and move through each record until you find the record you are looking for or you come to the end of the records. This is called a sequential search.  A sequential search (also called a linear search) is very easy to implement.
  • 5. Sequential Search Algorithm 5 2 5 81 88 90 99 7 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
  • 6. Sequential Search Algorithm 6 1. static void Main(string[] args) 2. { int[] a = { 10, 2, 34, 4, 3, 1, 100 }; 3. int wanted = 1; 4. int target_index=-1; 5. for (int i = 0; i < a.Length; i++) 6. if (a[i] == wanted) { 7. target_index = i; 8. break; } 9. if (target_index >= 0) 10. Console.WriteLine(" The wanted value exist at position: "+(target_index+1)+"'th"); 11. Console.Read(); } Practice: Reprogram this problem using string data.
  • 7. Binary Search Algorithm 7  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.
  • 9. Binary Search Algorithm 9 2 5 81 88 90 99 7 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
  • 10. Binary Search Algorithm 10 2 5 81 88 90 99 7 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
  • 11. Binary Search Algorithm 11 ‫البيانات‬ ‫تراكيب‬ ‫مساق‬ ‫إعداد‬ ‫العلمية‬ ‫المادة‬ / ‫أ‬ . ‫ا‬ َّ‫الفــر‬ ‫رفيق‬ ‫محمود‬ 1. static void Main(string[] args) { 2. int[] a = { 2, 3, 5, 8, 10, 23, 30}; 3. int wanted = 23; 4. int start = 0; 5. int last = a.Length - 1; 6. int middle = (last + start) / 2; 7. int result =0; 8. while (start < last) { 9. middle = (last + start) / 2; 10. if (a[middle] == wanted) { 11. result = 1; 12. break; } 13. else if (a[middle] < wanted) 14. start = middle + 1; 15. else 16. last = middle - 1; } 17. if (result == 1) 18. Console.WriteLine("The wanted element is exist at: " + middle); 19. else 20. Console.WriteLine("The wanted element does not exist "); To understand: trace step by step To practice: try with string data More practice: try with 2-D array
  • 12. Which is the best ? 12  Sequential search is used when the items in the list are in random order.  Binary search is used when the items are sorted in the list.
  • 13. Search and sorting in Array List 13 1. static void Main(string[] args) { 2. ArrayList a = new ArrayList(); 3. a.Add(20); 4. a.Add(4); 5. a.Add(3); 6. a.Sort(); 7. int result = a.BinarySearch(3); 8. Console.WriteLine(" position of 3 at : " + result); 9. result = a.BinarySearch(7); 10. Console.WriteLine(" position of 7 at : " + result); 11. Console.WriteLine(); 12. foreach (object x in a) 13. Console.WriteLine(" " + x); 14. Console.ReadLine(); }
  • 14. Thank You … 14 Remember that: question is the key of knowledge
  • 15. Ahl Eljanna   َ‫ق‬َ ‫د‬َ ‫ص‬ ‫ي‬ّ ‫ذ‬‫ه‬‫ل‬‫ا‬ ّ‫ه‬ّ ‫َلِل‬ ُ ‫د‬‫ح‬ ‫م‬َ‫ح‬ ‫ْل‬‫ا‬ ‫وا‬ُ‫ل‬‫ا‬َ‫ق‬َ ‫و‬ ‫ا‬ ‫ا‬َ‫ن‬َ‫ث‬َ ‫ر‬‫ح‬ ‫َو‬‫أ‬َ ‫و‬ ُ‫ه‬َ ‫د‬‫ح‬‫ع‬َ ‫و‬ ‫ا‬َ‫ن‬ ُ‫أ‬‫ه‬ ‫و‬َ‫ب‬َ‫ت‬َ‫ن‬ َ ‫ض‬‫ح‬ ‫ر‬َ‫ح‬ ‫ْل‬ َ ‫م‬‫ح‬‫ع‬ّ‫ن‬َ‫ف‬ ُ‫اء‬َ ‫ش‬َ‫ن‬ ُ ‫ث‬‫ح‬‫ي‬َ ‫ح‬ ّ ‫هة‬‫ن‬َ‫ح‬ ‫ْل‬‫ا‬ ‫ح‬ ‫ن‬ّ ‫م‬ َ ‫ي‬ّ‫ل‬ّ ‫ام‬َ ‫حع‬‫ل‬‫ا‬ ُ ‫ر‬‫ح‬ ‫َج‬‫أ‬ 15