0% found this document useful (0 votes)
0 views22 pages

Lecture 2 - Algorithms and Data Structures

The document discusses common algorithms for sorting and searching, focusing on linear and binary search methods. Linear search is simple but inefficient for large lists, while binary search is more efficient as it divides the list into halves. The time complexity for binary search is O(1) in the best case and O(log n) in the worst and average cases.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views22 pages

Lecture 2 - Algorithms and Data Structures

The document discusses common algorithms for sorting and searching, focusing on linear and binary search methods. Linear search is simple but inefficient for large lists, while binary search is more efficient as it divides the list into halves. The time complexity for binary search is O(1) in the best case and O(log n) in the worst and average cases.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Sorting and Searching

Algorithms
Common Problems

 There are some very common problems that we use computers to solve:
 Searching: Looking for specific data item/record from list of data items
or set of records.
 Sorting : placing records/items in order
Linear/Sequential Search on an
Unordered List
Basic algorithm:
 Get the search criterion (key)
 Get the first record from the file
While ( (record != key) and (still more records) )
Get the next record
End_while
Implementation of Linear Search
(Sequential Search)
int linear_search(int list[], int n, int key){
for (int i=0;i<n; i++){
if(list[i]==key)
return i;
}
return -1;
}
Linear Search (Sequential Search)
Time Complexity
Binary Search

 Sequential search is not efficient for large lists because, on average, the
sequential search searches half the list.
 If we have an ordered list and we know how many things are in the list,
we can use a different strategy.
 The binary search gets its name because the algorithm continually
divides the list into two parts.
 Uses the divide-and-conquer technique to search the list
Step by Step Explanation

 Start with a sorted list: To perform binary search, the input list must be
sorted in ascending order.
 Define the search range: Identify the leftmost and rightmost indices of
the list that represent the current search range.
 Calculate the mid: Find the middle index of the current range using the
formula mid = (left + right) / 2.
 Compare with the target: Check if the middle element of the list is equal
to, greater than, or less than the target element.
Step by Step Explanation

 Adjust the search range: Based on the comparison, update the left and
right indices to narrow down the search range. If the middle element is
equal to the target, the search is complete. Otherwise, if the middle
element is greater, the target must be in the left half of the current
range; otherwise, it must be in the right half.
 Repeat the process: Continue the process of dividing the search space
in half until the target element is found or until the search range
becomes empty.
Binary Search Visualization

Suppose you have an array like the following:


Binary Search Visualization

 Suppose you have an array like the one below


 And what you want is to create a function that is responsible for
searching for a specific number within the array.
 For this example, suppose the function is tasked with finding the number
5. How would you do it using binary search?
Binary Search Visualization

 The first step is to divide the array into two equal parts, which is
achieved by finding the middle element, which in our case is the number
4, as you can see in the image below.
Binary Search Visualization

 The second step is to compare whether the middle element — In this


case, the number 4 — is greater than or equal to the element we are
searching for.
 So for our example, we would make the following comparison:
4≥5
 Clearly, the answer is NO, indicating that all numbers less than 4 should
be discarded because the number we are searching for is greater than
the middle number.
Binary Search Visualization
Binary Search Visualization

 As a third step, once we’ve identified that the number we’re looking for
is greater than the middle number — remember, the middle number is 4

 we now need to consider only the numbers greater than 4 and again
divide the array into two, as follows:
Binary Search Visualization

Now, again, we must make the following condition:

is 6 ≥ 5
In this case, the answer is YES. The middle number is
greater than the number we are searching for, which means
we should discard all numbers greater than 6.
Binary Search Visualization

 As shown in the image above, the result of the condition leaves us only
with the number 5 as the result, as there are no other numbers smaller
than 6 apart from the number 5.
 And that’s how the binary search algorithm works. It divides a sorted
array into two equal parts using a middle element, then applies a
condition to determine whether the element we are looking for is to the
right or left of the array, and repeats this process until it finds the
desired element.
How Fast is a Binary Search?
Time Complexity of Binary Search

 Binary search is one of the fastest Algorithms


 1. Best Case: O(1)
 The best case occurs when the target element is found at the middle
index on the first comparison.
 Example: If the array is [1, 2, 3, 4, 5] and the target is 3, it is at the
middle index.
Worst Case: O(log⁡n)

 The worst case happens when the search space is repeatedly halved
until a single element is left.
 For an array of size n, the maximum number of halving steps required is:
Average Case: O(log⁡n)

 On average, the target might be found after a few halving steps.


 However, the expected number of steps is still proportional to log n
making the average case time complexity also O(logn).
Binary Search Implementation
Binary Search Implementation

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy