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

More Related Content

What's hot

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 ListPTCL
 
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
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 queueSenthil 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
 

What's hot (20)

3 searching algorithms in Java
3 searching algorithms in Java3 searching algorithms in Java
3 searching algorithms in Java
 
3 Array operations
3   Array operations3   Array operations
3 Array operations
 
Stack Implementation
Stack ImplementationStack Implementation
Stack Implementation
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked List
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Data structure stack&queue basics
Data structure stack&queue   basicsData structure stack&queue   basics
Data structure stack&queue basics
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
Control statements
Control statementsControl statements
Control statements
 
Priority Queue
Priority QueuePriority Queue
Priority Queue
 
Stacks
StacksStacks
Stacks
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
Queue
QueueQueue
Queue
 
Stacks, Queues, Deques
Stacks, Queues, DequesStacks, Queues, Deques
Stacks, Queues, Deques
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queue
 
Presentation on queue
Presentation on queuePresentation on queue
Presentation on queue
 
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
 
stacks in algorithems and data structure
stacks in algorithems and data structurestacks in algorithems and data structure
stacks in algorithems and data structure
 

Similar to Chapter 4: basic search algorithms data structure

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 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
 
16-sorting.ppt
16-sorting.ppt16-sorting.ppt
16-sorting.ppt
18Gunaalanpg
 
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 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
 
07+08slide.pptx
07+08slide.pptx07+08slide.pptx
07+08slide.pptx
MURADSANJOUM
 
Unit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTUREUnit 2 dsa LINEAR DATA STRUCTURE
Unit 8 searching and hashing
Unit   8 searching and hashingUnit   8 searching and hashing
Unit 8 searching and hashing
Dabbal Singh Mahara
 
Searching.ppt
Searching.pptSearching.ppt
Searching.ppt
SpammemeLmao
 
CP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdfCP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdf
saneshgamerz
 

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

Chapter 11 ds
Chapter 11 dsChapter 11 ds
Chapter 11 ds
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
 
UNIT V.docx
UNIT V.docxUNIT V.docx
UNIT V.docx
 
Algorithm and Programming (Searching)
Algorithm and Programming (Searching)Algorithm and Programming (Searching)
Algorithm and Programming (Searching)
 
UNIT IV -Data Structures.pdf
UNIT IV -Data Structures.pdfUNIT IV -Data Structures.pdf
UNIT IV -Data Structures.pdf
 
Searching
SearchingSearching
Searching
 
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
 
16-sorting.ppt
16-sorting.ppt16-sorting.ppt
16-sorting.ppt
 
9 Arrays
9 Arrays9 Arrays
9 Arrays
 
21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx
 
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
 
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
 
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
 
Data Structure Searching.pptx
Data Structure Searching.pptxData Structure Searching.pptx
Data Structure Searching.pptx
 
Chapter 14
Chapter 14Chapter 14
Chapter 14
 
07+08slide.pptx
07+08slide.pptx07+08slide.pptx
07+08slide.pptx
 
Unit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTUREUnit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTURE
 
Unit 8 searching and hashing
Unit   8 searching and hashingUnit   8 searching and hashing
Unit 8 searching and hashing
 
Searching.ppt
Searching.pptSearching.ppt
Searching.ppt
 
CP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdfCP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdf
 

More from 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
 
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
 

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
 
Computer Programming, Loops using Java
Computer Programming, Loops using JavaComputer Programming, Loops using Java
Computer Programming, Loops using Java
 
Chapter9 graph data structure
Chapter9  graph data structureChapter9  graph data structure
Chapter9 graph data structure
 
Chapter 8: tree data structure
Chapter 8:  tree data structureChapter 8:  tree data structure
Chapter 8: tree data structure
 
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
 
Chapter 0: introduction to data structure
Chapter 0: introduction to data structureChapter 0: introduction to data structure
Chapter 0: introduction to data structure
 
3 classification
3  classification3  classification
3 classification
 
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
 
7 programming-using-java decision-making220102011
7 programming-using-java decision-making2201020117 programming-using-java decision-making220102011
7 programming-using-java decision-making220102011
 
6 programming-using-java decision-making20102011-
6 programming-using-java decision-making20102011-6 programming-using-java decision-making20102011-
6 programming-using-java decision-making20102011-
 
5 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop201020115 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop20102011
 
4 programming-using-java intro-tojava20102011
4 programming-using-java intro-tojava201020114 programming-using-java intro-tojava20102011
4 programming-using-java intro-tojava20102011
 
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
 
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
 
1 programming-using-java -introduction
1 programming-using-java -introduction1 programming-using-java -introduction
1 programming-using-java -introduction
 
تلخيص النصوص تلقائيا
تلخيص النصوص تلقائياتلخيص النصوص تلقائيا
تلخيص النصوص تلقائيا
 
4×4×4 لتحصيل التميز
4×4×4 لتحصيل التميز4×4×4 لتحصيل التميز
4×4×4 لتحصيل التميز
 
Data preparation and processing chapter 2
Data preparation and processing chapter  2Data preparation and processing chapter  2
Data preparation and processing chapter 2
 
Introduction to-data-mining chapter 1
Introduction to-data-mining  chapter 1Introduction to-data-mining  chapter 1
Introduction to-data-mining chapter 1
 
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)
 

Recently uploaded

The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 

Recently uploaded (20)

The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 

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