0% found this document useful (0 votes)
31 views11 pages

LabManual 23967 Content Document 20240909125727PM

do it now

Uploaded by

dtrudra72
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views11 pages

LabManual 23967 Content Document 20240909125727PM

do it now

Uploaded by

dtrudra72
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

1.

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.

Linear Search Steps:

Step 1 - Does the item match the value I’m looking for?

Step 2 - If it does match return, you’ve found your item!

Step 3 - If it does not match advance and repeat the process.

Step 4 - Reached the end of the list and still no value found?

Well obviously the item is not in the list!

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.

Imagine you have a random assortment of integers for this list:

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

int linearSearch(int data[], int length, int val)

for (int i = 0; i <= length; i++)

if (val == data[i])

return i;

}//end if

}//end for return -1; //Value was not in the list }//

end

linear Search Function Analysis & Conclusion

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).

2. BINARY Search Algorithm

 Searching is a process of finding a particular element among several given elements.


 The search is successful if the required element is found.
 Otherwise, the search is unsuccessful.
Binary Search-

 Binary Search is one of the fastest searching algorithms.


 It is used for finding the location of an element in a linear array.
 It works on the principle of divide and conquer technique.
Binary Search Algorithm can be applied only on Sorted arrays.
So, the elements must be arranged in-
 Either ascending order if the elements are numbers.
 Or dictionary order if the elements are strings.

To apply binary search on an unsorted array,


 First, sort the array using some sorting technique.
 Then, use binary search algorithm.
Binary Search Algorithm-
Consider-
 There is a linear array ‘a’ of size ‘n’.
 Binary search algorithm is being used to search an element ‘item’ in this linear array.
 If search ends in success, it sets loc to the index of the element otherwise it sets loc to -1.
 Variables beg and end keeps track of the index of the first and last element of the array or sub
array in which the element is being searched at that instant.
 Variable mid keeps track of the index of the middle element of that array or sub array in which
the element is being searched at that instant.

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.

Time Complexity Analysis-


Binary Search time complexity analysis is done below-
 In each iteration or in each recursive call, the search gets reduced to half of the array.
 So for n elements in the array, there are log2n iterations or recursive calls.

Thus, we have-

Time Complexity of Binary Search Algorithm is O(log2n).


Here, n is the number of elements in the sorted linear array.

This time complexity of binary search remains unchanged irrespective of the element position
even if it is not present in the array.

Binary Search Example-


Consider-
 We are given the following sorted linear array.
 Element 15 has to be searched in it using Binary Search Algorithm.

Binary Search Algorithm works in the following steps-


Step-01:
To begin with, we take beg=0 and end=6.
 We compute location of the middle element as-
mid
= (beg + end) / 2
= (0 + 6) / 2
=3
 Here, a[mid] = a[3] = 20 ≠ 15 and beg < end.
 So, we start next iteration.
Step-02:
Since a[mid] = 20 > 15, so we take end = mid – 1 = 3 – 1 = 2 whereas beg remains unchanged.
 We compute location of the middle element as-
mid
= (beg + end) / 2
= (0 + 2) / 2
=1
 Here, a[mid] = a[1] = 10 ≠ 15 and beg < end.
 So, we start next iteration.
Step-03:
Since a[mid] = 10 < 15, so we take beg = mid + 1 = 1 + 1 = 2 whereas end remains unchanged.
 We compute location of the middle element as-
mid
= (beg + end) / 2
= (2 + 2) / 2
=2
 Here, a[mid] = a[2] = 15 which matches to the element being searched.
 So, our search terminates in success and index 2 is returned.
Binary Search Algorithm Advantages-
The advantages of binary search algorithm are-
 It eliminates half of the list from further searching by using the result of each comparison.
 It indicates whether the element being searched is before or after the current position in the
list.
 This information is used to narrow the search.
 For large lists of data, it works significantly better than linear search.
Binary Search Algorithm Disadvantages-
The disadvantages of binary search algorithm are-
 It employs recursive approach which requires more stack space.
 Programming binary search algorithm is error prone and difficult.
 The interaction of binary search with memory hierarchy i.e. caching is poor.
(because of its random access nature)
3. Insertion sort

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.

A similar approach is used by insertion sort.

Insertion sort is a sorting algorithm that places an unsorted element at its suitable place in each
iteration.

How Insertion Sort Works?

Suppose we need to sort the following array.

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.

2. Now, the first two elements are sorted.


Take the third element and compare it with the elements on the left of it. Placed it just
behind the element smaller than it. If there is no element smaller than it, then place it at
the beginning of the array.

Place 1 at the beginning


1. Similarly, place every unsorted element at its correct position.

Place 3 behind 1 and the array is sorted


Factorial of a number
Factorial of a non-negative integer, is multiplication of all integers smaller than or equal
to n. For example factorial of 6 is 6*5*4*3*2*1 which is 720.

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);

 Factorial can be calculated using following recursive formula.


n! = 1 if n = 0
n! = n * (n-1)! Otherwise
Or

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)

You might also like