LabManual 23967 Content Document 20240909125727PM
LabManual 23967 Content Document 20240909125727PM
Linear Search
Linear Search as the name implies is a searching algorithm which obtains its result by traversing
a list of data items in a linear fashion. It will start at the beginning of a list, and mosey on
through until the desired element is found, or in some cases is not found. The aspect of Linear
Search which makes it inefficient in this respect is that if the element is not in the list it will have
to go through the entire list. As you can imagine this can be quite cumbersome for lists of very
large magnitude, keep this in mind as you contemplate how and where to implement this
algorithm. Of course conversely the best case for this would be that the element one is searching
for is the first element of the list, this will be elaborated more so in the Analysis & Conclusion
section of this tutorial.
Step 1 - Does the item match the value I’m looking for?
Step 4 - Reached the end of the list and still no value found?
Return -1 to signify you have not found your value. As always, visual representations are a bit
more clear and concise so let me present one for you now.
Ok so here is our number set, my lucky number happens to be 7 so let‘s put this value as the key,
or the value in which we hope Linear Search can find. Notice the indexes of the array above each
of the elements, meaning this has a size or length of 5. I digress let us look at the first term at
position 0. The value held here 3, which is not equal to 7. We move on.
--0 1 2 3 4 5
[325170]
So we hit position 0, on to position 1. The value 2 is held here. Hmm still not equal to 7. We
march on.
--0 1 2 3 4 5
[325170]
Position 2 is next on the list, and sadly holds a 5, still not the number we‘re looking for. Again
we move up one.
--0 1 2 3 4 5
[325170]
Now at index 3 we have value 1. Nice try but no cigar let‘s move forward yet again. --0 1 2 3 4
5 [ 3 2 5 1 7 0 ] Ah Ha! Position 4 is the one that has been harboring 7, we return the position in
the array which holds 7 and exit.
--0 1 2 3 4 5
[325170]
//linearSearch Function
if (val == data[i])
return i;
}//end if
}//end for return -1; //Value was not in the list }//
end
As we have seen throughout this tutorial that Linear Search is certainly not the absolute best
method for searching but do not let this taint your view on the algorithm itself. People are always
attempting to better versions of current algorithms in an effort to make existing ones more
efficient. Not to mention that Linear Search as shown has its place and at the very least is a great
beginner‘s introduction into the world of searching algorithms. With this is mind we progress to
the asymptotic analysis of the Linear Search:
Worst Case: The worse case for Linear Search is achieved if the element to be found is not in
the list at all. This would entail the algorithm to traverse the entire list and return nothing. Thus
the worst case running time is: O(N).
Average Case: The average case is in short revealed by insinuating that the average element
would be somewhere in the middle of the list or N/2. This does not change since we are dividing
by a constant factor here, so again the average case would be: O(N).
Best Case: The best case can be a reached if the element to be found is the first one in the list.
This would not have to do any traversing spare the first one giving this a constant time
complexity or: O(1).
Algorithm Binary_Search(key,A,N)
Low=0 , High=n-1
While(low<=high)
Mid=(low + high)/2
If (key==a[mid]) return mid+1
Else if(key<A[Mid])
high=mid-1
Else
Low=mid+1
Endif
End while
Return -1
Explanation
Binary Search Algorithm searches an element by comparing it with the middle most element of the
array.
Then, following three cases are possible-
Case-01
If the element being searched is found to be the middle most element, its index is returned.
Case-02
If the element being searched is found to be greater than the middle most element, then its search is
further continued in the right sub array of the middle most element.
Case-03
If the element being searched is found to be smaller than the middle most element, then its search is
further continued in the left sub array of the middle most element.
This iteration keeps on repeating on the sub arrays until the desired element is found
or size of the sub array reduces to zero.
Thus, we have-
This time complexity of binary search remains unchanged irrespective of the element position
even if it is not present in the array.
Insertion sort works similarly as we sort cards in our hand in a card game.
We assume that the first card is already sorted then, we select an unsorted card. If the unsorted
card is greater than the card in hand, it is placed on the right otherwise, to the left. In the same
way, other unsorted cards are taken and put at their right place.
Insertion sort is a sorting algorithm that places an unsorted element at its suitable place in each
iteration.
Initial array
1. The first element in the array is assumed to be sorted. Take the second element and store
it separately in key.
Compare key with the first element. If the first element is greater than key, then key is
placed in front of the first element.
If the first element is greater than key, then key is placed in front of the first element.
Recursive Solution:
Factorial can be calculated using following recursive formula.
n! = n * (n-1)!
n! = 1 if n = 0 or n = 1
Algorithm fact(n)
//Purpose: to find factorial of given number
// Input: n represent a positive integer
//Output: factorial of n
if (n == 0)
return 1;
return n * factorial(n - 1);
T(N)=0 IF N=0
1+T(N-1) OTHERWISE
1+T(N-1)
1+ 1+T(N-2)
2+T(N-2)
2+ 1+T(N-3)
3+T(n-3)
3+ 1+T(N-4)
4+t(n-4)
.
.
.
i + T(N-i)
Substitute i=N
N+T(N-N)
N+t(0)
N+0
N
Theta(N)