Algorithms
Algorithms
What is Algorithm
An algorithm is a procedure used for solving a problem or performing a
computation.
This algorithm first assigns the initial term of the sequence, a1, to the variable
max. The “for”loop is used to successively examine terms of the sequence. If a
term is greater than the current value of max, it is assigned to be the new value of
max.
PROPERTIES OF ALGORITHMS
To show that the algorithm is correct, we must show that when the
algorithm terminates, the value of the variable max equals the maximum of
the terms of the sequence.
Searching Algorithms
A search algorithm is the step-by-step procedure used to locate specific
data among a collection of data.
when searching for data, the difference between a fast application and
a slower one often lies in the use of the proper search algorithm
Linear or Sequential Search
Consider
arr = [2, 12, 15, 11, 7, 19, 45]
0 1 2 3 4 5 6
Suppose the target element we want to search is 7.
Start with index 0 and compare each element with the target
If the target is found to be equal to the element, return its index
If the target is not found, return -1
The procedures for implementing linear search are as follows:
Step 1: First, read the search element (Target element) in the array.
Step 2: In the second step compare the search element with the first element in the array.
Step 3: If both are matched, display "Target element is found" and terminate the Linear Search function.
Step 4: If both are not matched, compare the search element with the next element in the array.
Step 5: In this step, repeat steps 3 and 4 until the search (Target) element is compared with the last
element of the array.
Step 6 - If the last element in the list does not match, the Linear Search Function will be terminated, and
the message "Element is not found" will be displayed.
Time Complexity Analysis
The Best Case occurs when the target element is the first element of the array.
The number of comparisons, in this case, is 1. So, the time complexity is O(1).
The Average Case: On average, the target element will be somewhere in the
middle of the array. The number of comparisons, in this case, will be N/2. So,
the time complexity will be O(N) (the constant being ignored).
The Worst Case occurs when the target element is the last element in the array
or not in the array. In this case, we have to traverse the entire array, and so the
number of comparisons will be N. So, the time complexity will be O(N)
Binary Search
- is a searching algorithm used in a sorted array by repeatedly dividing
the search interval in half.
- works on the principle of divide and conquer and it is considered the
best searching algorithm because it's faster to run.
-if the terms are numbers, they are listed from smallest to largest;
if they are words, they are listed in lexicographic, or alphabetic, order).
The binary search works in the following manner:
The search process initiates by locating the middle element of the sorted array
of data
If the key value is smaller than the middle element, then searches analyses the
lower values to the middle element for comparison and matching
In case the key value is greater than the middle element then searches analyses
the upper values to the middle element for comparison and matching
Example
You have an array of sorted values ranging from 2 to 20 and need to locate
18.
The average of the lower and upper limits is (l + r) / 2 = 4. The value being
searched is greater than the mid which is 4.
The array values less than the mid are dropped from search and values
greater than the mid-value 4 are searched.
Linear:
Binary:
Sorting
-refers to ordering data in an increasing or decreasing manner
according to some linear relationship among the data items.
First Pass:
Bubble sort starts with very first two elements, comparing them to check which
one is greater.
( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and
swaps since 5 > 1.
( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) –> ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5),
algorithm does not swap them.
Second pass:
Now, the array is already sorted, but our algorithm does not know if it is
completed.
The algorithm needs one whole pass without any swap to know it is sorted.
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
EXAMPLE 4
Use the bubble sort to put 3, 2, 4, 1, 5 into increasing order.