SlideShare une entreprise Scribd logo
1  sur  31
Linear Search
&
Binary Search
Abdul Kahir
&
Abdul Mohaimin Rahat
What is algorithm and Algorithm design?
• An Algorithm is a Step by Step solution of a specific
mathematical or computer related problem.
• Algorithm design is a specific method to create a
mathematical process in solving problems.
Sorted Array
• Sorted array is an array where each element is sorted in
numerical, alphabetical, or some other order, and placed at
equally spaced addresses in computer memory.
1 2 3 4
0.2 0.3 1 1.5
Unsorted Array
• Unsorted array is an array where each element is
not sorted in numerical, alphabetical, or some other order,
and placed at equally spaced addresses in computer memory.
1 2 3 4
0.2 0.3 1.5 1
What is searching?
• In computer science, searching is the process of finding an item with
specified properties from a collection of items.The items may be stored as
records in a database, simple data elements in arrays, text in files, nodes in
trees, vertices and edges in graphs, or maybe be elements in other search
place.
• The definition of a search is the process of looking for something or
someone
• Example : An example of a search is a quest to find a missing person
Why do we need searching?
Searching is one of the core computer science algorithms. We know that today’s
computers store a lot of information.To retrieve this information proficiently we need
very efficient searching algorithms.
Types of Searching
• Linear search
• Binary search
Linear Search
• The linear search is a sequential search, which uses a loop to step
through an array, starting with the first element.
• It compares each element with the value being searched for, and
stops when either the value is found or the end of the array is
encountered.
• If the value being searched is not in the array, the algorithm will
unsuccessfully search to the end of the array.
Linear Search
• Since the array elements are stored in linear order searching the
element in the linear order make it easy and efficient.
• The search may be successful or unsuccessfully.That is, if the
required element is found them the search is successful other wise
it is unsuccessfully.
Unordered linear/ Sequential search
int unorderedlinearsearch (int A[], int n, int data)
{
for (int i=0; i<n; i++)
{
if(A[i] == data)
return i;
}
return -1;
}
Advantages of Linear search
• If the first number in the directory is the number you
were searching for ,then lucky you!!.
• ●Since you have found it on the very first page,now its
not important for you that how many pages are there in
the directory.
• The linear search is simple - It is very easy to
understand and implement
• It does not require the data in the array to be stored in
any particular order
• ●So it does not depends on no. on elements in the
directory.Hence constant time .
Disadvantages of Linear search
• It may happen that the number you are searching for is the last number of
directory or if it is not in the directory at all.
• In that case you have to search the whole directory.
• Now number of elements will matter to you.if there are 500 pages ,you have to
search 500;if it has 1000 you have to search 1000.
• Your search time is proportional to number of elements in the directory.
• It has very poor efficiency because it takes lots of comparisons to find a
particular record in big files
• The performance of the algorithm scales linearly with the size of the input
• Linear search is slower then other searching algorithms
Analysis of Linear Search
How long will our search take?
In the best case, the target value is in the first element of the array.
So the search takes some tiny, and constant, amount of time.
In the worst case, the target value is in the last element of the array.
So the search takes an amount of time proportional to the length of
the array.
Analysis of Linear Search
In the average case, the target value is somewhere in the array.
In fact, since the target value can be anywhere in the array, any element
of the array is equally likely.
So on average, the target value will be in the middle of the array.
So the search takes an amount of time proportional to half the length
of the array
The worst case complexity is O(n), sometimes known an O(n) search
Time taken to search elements keep increasing as the number of
elements are increased.
Abdul Kahir
161-15-7173
Binary Search
The general term for a smart search through sorted data is a binary search.
1. The initial search region is the whole array.
2. Look at the data value in the middle of the search region.
3. If you’ve found your target, stop.
4. If your target is less than the middle data value, the new search region
is the lower half of the data.
5. If your target is greater than the middle data value, the new search
region is the higher half of the data.
6. Continue from Step 2.
17
Binary Search
© Reem Al-Attas
Binary Search
2. Calculate middle = (low + high) / 2.
= (0 + 8) / 2 = 4.
If 37 == array[middle]  return middle
Else if 37 < array[middle]  high = middle -1
Else if 37 > array[middle]  low = middle +1
7/25/2017 © Reem Al-Attas 19
Binary Search
Repeat 2. Calculate middle = (low + high) / 2.
= (0 + 3) / 2 = 1.
If 37 == array[middle]  return middle
Else if 37 < array[middle]  high = middle -1
Else if 37 > array[middle]  low = middle +1
7/25/2017 © Reem Al-Attas 20
Binary Search
Repeat 2. Calculate middle = (low + high) / 2.
= (2 + 3) / 2 = 2.
If 37 == array[middle]  return middle
Else if 37 < array[middle]  high = middle -1
Else if 37 > array[middle]  low = middle +1
©The McGraw-Hill Companies, Inc. Permission required for reproduction or
display.
Chapter 11 - 21
Binary Search Routine
public int binarySearch (int[] number, int searchValue) {
int low = 0,
high = number.length - 1,
mid = (low + high) / 2;
while (low <= high && number[mid] != searchValue) {
if (number[mid] < searchValue) {
low = mid + 1;
} else { //number[mid] > searchValue
high = mid - 1;
}
mid = (low + high) / 2; //integer division will
truncate
}
if (low > high) {
mid = NOT_FOUND;
}
return mid;
}
Binary Search Performance
• Successful Search
– Best Case – 1 comparison
– Worst Case – log2N comparisons
• Unsuccessful Search
– Best Case =
Worst Case – log2N comparisons
• Since the portion of an array to search is cut into half after every
comparison, we compute how many times the array can be
divided into halves.
• After K comparisons, there will be N/2K elements in the list. We
solve for K when N/2K = 1, deriving K = log2N.
Comparing N and log2N Performance
Array Size Linear – N Binary – log2N
10 10 4
50 50 6
100 100 7
500 500 9
1000 1000 10
2000 2000 11
3000 3000 12
4000 4000 12
5000 5000 13
6000 6000 13
7000 7000 13
8000 8000 13
9000 9000 14
10000 10000 14
Important Differences:
Input data needs to be sorted in Binary Search and not in Linear
Search
Linear search does the sequential access whereas Binary search
access data randomly.
Time complexity of linear search -O(n) , Binary search has time
complexity O(log n).
Linear search performs equality comparisons and Binary search
performs ordering comparisons
Greedy Algorithm
• A greedy algorithm is an algorithmic paradigm that follows
the problem solving heuristic of making the locally optimal
choice at each stage with the hope of finding a global
optimum.
Usage of greedy algorithm
• The greedy choice property: A globally optimal solution can
be arrived at by making a locally optimal (greedy) choice.
• The optimal substructure property:The optimal solution
contains within its optimal solutions to subproblems.
Method
• Casting the optimization problem as one for which:
we make a choice and are left with only one sub problem to solve
• Proving the greedy choice:
there is always an optimal solution to the original problem that
makes the greedy choice
• Proving the optimal structure:
the greedy choice + an optimal solution to the resulting Sub
problem leads to an optimal solution.
Simple Example on greedy Algorithm
method
• $6.64 = $2 +$2 + $1 + $1
.25 + .25 + .10 +
.01 + .01 + .01 +.01
• 10$= $5 + $5
When Greedy Algorithm Fails
The summation of two 5$ is 10$ but the summation of three 5$
cannot be 10 if its fixed.
For example,
$5 + $5 = $10
But,
$5 + $5 + $5 ≠ $10
Importance of Algorithm Design
• It is used to store and access large quantities of data
efficiently.
• It is used to solve complex computational problems and to
design of good programs
• It is important to justify an algorithm correctness
mathematically
• It provides clear , simple and unambiguous description
Conclusion
• At last it may said that , the so called program name
Algorithm design is one of the most enforceable solution
program that can be used to solve any complicated , complex
, and hard program.
• Under considering the above case the concerned authority
included the jubilant students should prefer this program with
great care.

Contenu connexe

Tendances

Tendances (20)

Searching linear &amp; binary search
Searching linear &amp; binary searchSearching linear &amp; binary search
Searching linear &amp; binary search
 
Binary search
Binary searchBinary search
Binary search
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
 
Binary Search
Binary SearchBinary Search
Binary Search
 
Searching Techniques and Analysis
Searching Techniques and AnalysisSearching Techniques and Analysis
Searching Techniques and Analysis
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searching
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Linear Search Data Structure
Linear Search Data StructureLinear Search Data Structure
Linear Search Data Structure
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary search
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
 
Searching algorithm
Searching algorithmSearching algorithm
Searching algorithm
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptx
 
Searching in c language
Searching in c languageSearching in c language
Searching in c language
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
Linear Search Presentation
Linear Search PresentationLinear Search Presentation
Linear Search Presentation
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structures
 

Similaire à Rahat &amp; juhith

(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting
Fadhil Ismail
 

Similaire à Rahat &amp; juhith (20)

data_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptxdata_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptx
 
Binary search Algorithm
Binary search AlgorithmBinary search Algorithm
Binary search Algorithm
 
Lecture_Oct26.pptx
Lecture_Oct26.pptxLecture_Oct26.pptx
Lecture_Oct26.pptx
 
Data Structures 8
Data Structures 8Data Structures 8
Data Structures 8
 
CPP12 - Algorithms
CPP12 - AlgorithmsCPP12 - Algorithms
CPP12 - Algorithms
 
Binary search algorithm.pptx
Binary search  algorithm.pptxBinary search  algorithm.pptx
Binary search algorithm.pptx
 
Data operatons & searching and sorting algorithms
Data operatons & searching and sorting algorithmsData operatons & searching and sorting algorithms
Data operatons & searching and sorting algorithms
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)
 
Unit 8 searching and hashing
Unit   8 searching and hashingUnit   8 searching and hashing
Unit 8 searching and hashing
 
searching techniques.pptx
searching techniques.pptxsearching techniques.pptx
searching techniques.pptx
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
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 Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
Searching Algorithms for students of CS and IT using C++
Searching Algorithms for students of CS and IT using C++Searching Algorithms for students of CS and IT using C++
Searching Algorithms for students of CS and IT using C++
 
Unit 7 sorting
Unit   7 sortingUnit   7 sorting
Unit 7 sorting
 
Data structure and algorithms
Data structure and algorithmsData structure and algorithms
Data structure and algorithms
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
 
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
 
(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting
 
Searching
SearchingSearching
Searching
 

Dernier

Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
shivangimorya083
 
Determinants of health, dimensions of health, positive health and spectrum of...
Determinants of health, dimensions of health, positive health and spectrum of...Determinants of health, dimensions of health, positive health and spectrum of...
Determinants of health, dimensions of health, positive health and spectrum of...
shambhavirathore45
 
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
amitlee9823
 
Log Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxLog Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptx
JohnnyPlasten
 
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Vip Model Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
Vip Model  Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...Vip Model  Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
Vip Model Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
shivangimorya083
 
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in  KishangarhDelhi 99530 vip 56974 Genuine Escort Service Call Girls in  Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Dernier (20)

April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysis
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx
 
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
VidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxVidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptx
 
Determinants of health, dimensions of health, positive health and spectrum of...
Determinants of health, dimensions of health, positive health and spectrum of...Determinants of health, dimensions of health, positive health and spectrum of...
Determinants of health, dimensions of health, positive health and spectrum of...
 
BabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxBabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptx
 
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightCheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
 
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
 
Edukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxEdukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFx
 
Zuja dropshipping via API with DroFx.pptx
Zuja dropshipping via API with DroFx.pptxZuja dropshipping via API with DroFx.pptx
Zuja dropshipping via API with DroFx.pptx
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
 
Smarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptxSmarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptx
 
Log Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxLog Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptx
 
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
BigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxBigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptx
 
Invezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signals
 
Generative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusGenerative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and Milvus
 
Vip Model Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
Vip Model  Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...Vip Model  Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
Vip Model Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
 
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in  KishangarhDelhi 99530 vip 56974 Genuine Escort Service Call Girls in  Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
 

Rahat &amp; juhith

  • 3. What is algorithm and Algorithm design? • An Algorithm is a Step by Step solution of a specific mathematical or computer related problem. • Algorithm design is a specific method to create a mathematical process in solving problems.
  • 4. Sorted Array • Sorted array is an array where each element is sorted in numerical, alphabetical, or some other order, and placed at equally spaced addresses in computer memory. 1 2 3 4 0.2 0.3 1 1.5
  • 5. Unsorted Array • Unsorted array is an array where each element is not sorted in numerical, alphabetical, or some other order, and placed at equally spaced addresses in computer memory. 1 2 3 4 0.2 0.3 1.5 1
  • 6. What is searching? • In computer science, searching is the process of finding an item with specified properties from a collection of items.The items may be stored as records in a database, simple data elements in arrays, text in files, nodes in trees, vertices and edges in graphs, or maybe be elements in other search place. • The definition of a search is the process of looking for something or someone • Example : An example of a search is a quest to find a missing person
  • 7. Why do we need searching? Searching is one of the core computer science algorithms. We know that today’s computers store a lot of information.To retrieve this information proficiently we need very efficient searching algorithms. Types of Searching • Linear search • Binary search
  • 8. Linear Search • The linear search is a sequential search, which uses a loop to step through an array, starting with the first element. • It compares each element with the value being searched for, and stops when either the value is found or the end of the array is encountered. • If the value being searched is not in the array, the algorithm will unsuccessfully search to the end of the array.
  • 9. Linear Search • Since the array elements are stored in linear order searching the element in the linear order make it easy and efficient. • The search may be successful or unsuccessfully.That is, if the required element is found them the search is successful other wise it is unsuccessfully.
  • 10. Unordered linear/ Sequential search int unorderedlinearsearch (int A[], int n, int data) { for (int i=0; i<n; i++) { if(A[i] == data) return i; } return -1; }
  • 11. Advantages of Linear search • If the first number in the directory is the number you were searching for ,then lucky you!!. • ●Since you have found it on the very first page,now its not important for you that how many pages are there in the directory. • The linear search is simple - It is very easy to understand and implement • It does not require the data in the array to be stored in any particular order • ●So it does not depends on no. on elements in the directory.Hence constant time .
  • 12. Disadvantages of Linear search • It may happen that the number you are searching for is the last number of directory or if it is not in the directory at all. • In that case you have to search the whole directory. • Now number of elements will matter to you.if there are 500 pages ,you have to search 500;if it has 1000 you have to search 1000. • Your search time is proportional to number of elements in the directory. • It has very poor efficiency because it takes lots of comparisons to find a particular record in big files • The performance of the algorithm scales linearly with the size of the input • Linear search is slower then other searching algorithms
  • 13. Analysis of Linear Search How long will our search take? In the best case, the target value is in the first element of the array. So the search takes some tiny, and constant, amount of time. In the worst case, the target value is in the last element of the array. So the search takes an amount of time proportional to the length of the array.
  • 14. Analysis of Linear Search In the average case, the target value is somewhere in the array. In fact, since the target value can be anywhere in the array, any element of the array is equally likely. So on average, the target value will be in the middle of the array. So the search takes an amount of time proportional to half the length of the array The worst case complexity is O(n), sometimes known an O(n) search Time taken to search elements keep increasing as the number of elements are increased.
  • 16. Binary Search The general term for a smart search through sorted data is a binary search. 1. The initial search region is the whole array. 2. Look at the data value in the middle of the search region. 3. If you’ve found your target, stop. 4. If your target is less than the middle data value, the new search region is the lower half of the data. 5. If your target is greater than the middle data value, the new search region is the higher half of the data. 6. Continue from Step 2.
  • 18. © Reem Al-Attas Binary Search 2. Calculate middle = (low + high) / 2. = (0 + 8) / 2 = 4. If 37 == array[middle]  return middle Else if 37 < array[middle]  high = middle -1 Else if 37 > array[middle]  low = middle +1
  • 19. 7/25/2017 © Reem Al-Attas 19 Binary Search Repeat 2. Calculate middle = (low + high) / 2. = (0 + 3) / 2 = 1. If 37 == array[middle]  return middle Else if 37 < array[middle]  high = middle -1 Else if 37 > array[middle]  low = middle +1
  • 20. 7/25/2017 © Reem Al-Attas 20 Binary Search Repeat 2. Calculate middle = (low + high) / 2. = (2 + 3) / 2 = 2. If 37 == array[middle]  return middle Else if 37 < array[middle]  high = middle -1 Else if 37 > array[middle]  low = middle +1
  • 21. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 11 - 21 Binary Search Routine public int binarySearch (int[] number, int searchValue) { int low = 0, high = number.length - 1, mid = (low + high) / 2; while (low <= high && number[mid] != searchValue) { if (number[mid] < searchValue) { low = mid + 1; } else { //number[mid] > searchValue high = mid - 1; } mid = (low + high) / 2; //integer division will truncate } if (low > high) { mid = NOT_FOUND; } return mid; }
  • 22. Binary Search Performance • Successful Search – Best Case – 1 comparison – Worst Case – log2N comparisons • Unsuccessful Search – Best Case = Worst Case – log2N comparisons • Since the portion of an array to search is cut into half after every comparison, we compute how many times the array can be divided into halves. • After K comparisons, there will be N/2K elements in the list. We solve for K when N/2K = 1, deriving K = log2N.
  • 23. Comparing N and log2N Performance Array Size Linear – N Binary – log2N 10 10 4 50 50 6 100 100 7 500 500 9 1000 1000 10 2000 2000 11 3000 3000 12 4000 4000 12 5000 5000 13 6000 6000 13 7000 7000 13 8000 8000 13 9000 9000 14 10000 10000 14
  • 24. Important Differences: Input data needs to be sorted in Binary Search and not in Linear Search Linear search does the sequential access whereas Binary search access data randomly. Time complexity of linear search -O(n) , Binary search has time complexity O(log n). Linear search performs equality comparisons and Binary search performs ordering comparisons
  • 25. Greedy Algorithm • A greedy algorithm is an algorithmic paradigm that follows the problem solving heuristic of making the locally optimal choice at each stage with the hope of finding a global optimum.
  • 26. Usage of greedy algorithm • The greedy choice property: A globally optimal solution can be arrived at by making a locally optimal (greedy) choice. • The optimal substructure property:The optimal solution contains within its optimal solutions to subproblems.
  • 27. Method • Casting the optimization problem as one for which: we make a choice and are left with only one sub problem to solve • Proving the greedy choice: there is always an optimal solution to the original problem that makes the greedy choice • Proving the optimal structure: the greedy choice + an optimal solution to the resulting Sub problem leads to an optimal solution.
  • 28. Simple Example on greedy Algorithm method • $6.64 = $2 +$2 + $1 + $1 .25 + .25 + .10 + .01 + .01 + .01 +.01 • 10$= $5 + $5
  • 29. When Greedy Algorithm Fails The summation of two 5$ is 10$ but the summation of three 5$ cannot be 10 if its fixed. For example, $5 + $5 = $10 But, $5 + $5 + $5 ≠ $10
  • 30. Importance of Algorithm Design • It is used to store and access large quantities of data efficiently. • It is used to solve complex computational problems and to design of good programs • It is important to justify an algorithm correctness mathematically • It provides clear , simple and unambiguous description
  • 31. Conclusion • At last it may said that , the so called program name Algorithm design is one of the most enforceable solution program that can be used to solve any complicated , complex , and hard program. • Under considering the above case the concerned authority included the jubilant students should prefer this program with great care.