Lecture 2 - Algorithms and Data Structures
Lecture 2 - Algorithms and Data Structures
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
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
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
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
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(logn)