SlideShare una empresa de Scribd logo
1 de 15
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

Más contenido relacionado

La actualidad más candente

3 searching algorithms in Java
3 searching algorithms in Java3 searching algorithms in Java
3 searching algorithms in JavaMahmoud Alfarra
 
Stack Implementation
Stack ImplementationStack Implementation
Stack ImplementationZidny 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 StructureZidny Nafan
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manualnikshaikh786
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-listpinakspatel
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applicationssomendra kumar
 
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 queueRojan 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,queueRai University
 
stacks in algorithems and data structure
stacks in algorithems and data structurestacks in algorithems and data structure
stacks in algorithems and data structurefaran nawaz
 

La actualidad más candente (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 a Chapter 4: basic search algorithms data structure

Similar a 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
 
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
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
 

Más de 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 2Mahmoud Alfarra
 
Computer Programming, Loops using Java
Computer Programming, Loops using JavaComputer Programming, Loops using Java
Computer Programming, Loops using JavaMahmoud Alfarra
 
Chapter9 graph data structure
Chapter9  graph data structureChapter9  graph data structure
Chapter9 graph data structureMahmoud Alfarra
 
Chapter 8: tree data structure
Chapter 8:  tree data structureChapter 8:  tree data structure
Chapter 8: tree data structureMahmoud 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 structureMahmoud Alfarra
 
Chapter 0: introduction to data structure
Chapter 0: introduction to data structureChapter 0: introduction to data structure
Chapter 0: introduction to data structureMahmoud 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 20102011Mahmoud Alfarra
 
7 programming-using-java decision-making220102011
7 programming-using-java decision-making2201020117 programming-using-java decision-making220102011
7 programming-using-java decision-making220102011Mahmoud 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-tooop20102011Mahmoud Alfarra
 
4 programming-using-java intro-tojava20102011
4 programming-using-java intro-tojava201020114 programming-using-java intro-tojava20102011
4 programming-using-java intro-tojava20102011Mahmoud 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 computerMahmoud 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 applicationMahmoud Alfarra
 
1 programming-using-java -introduction
1 programming-using-java -introduction1 programming-using-java -introduction
1 programming-using-java -introductionMahmoud 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 2Mahmoud Alfarra
 
Introduction to-data-mining chapter 1
Introduction to-data-mining  chapter 1Introduction to-data-mining  chapter 1
Introduction to-data-mining chapter 1Mahmoud 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
 

Más de 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)
 

Último

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 

Último (20)

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 

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