Lab 4
Lab 4
LAB 4
Searching Techniques:
Searching is the process of finding the location of a target element among a list of elements.
Here, we are going to discuss about the following two searching techniques:
Procedure:
2
Advantages:
It is a simple technique.
This technique works well for lists with smaller size.
The elements in the list need not be in any sorted order.
Disadvantage:
This method is inefficient when large number of elements are placed in list, because time taken for
search is more.
3
In [44]:
4
In [45]:
Alternative Way:
In [2]:
5
In [4]:
Procedure:
Binary search algorithm works on the principle of divide and conquer. For this algorithm to work properly,
the data collection should be in the sorted form.
Binary search looks for a particular item by comparing the middle most item of the collection.
If a match occurs, then the index of item is returned.
If the middle item is greater than the item, then the item is searched in the sub-array to the left of the
middle item. Otherwise, the item is searched for in the sub-array to the right of the middle item.
This process continues on the sub-array as well until the size of the sub-array reduces to zero.
For a binary search to work, it is mandatory for the target array to be sorted. We shall learn the process of
binary search with a pictorial example. The following is our sorted array and let us assume that we need to
search the location of value 31 using binary search.
First, we shall determine half of the array by using the below formula −
6
mid = low + (high - low) / 2
Here it is, 0 + (9 - 0 ) / 2 = 4 (integer value of 4.5). So, 4 is the mid of the array.
Now we compare the value stored at location 4, with the value being searched, i.e. 31. We find that the value
at location 4 is 27, which is not a match. As the value is greater than 27 and we have a sorted array, so we
also know that the target value must be in the upper portion of the array.
We change our low to mid + 1 and find the new mid value again.
low = mid + 1
Our new mid is 7 now. We compare the value stored at location 7 with our target value 31.
The value stored at location 7 is not a match, rather it is less than what we are looking for. So, the value must
be in the lower part from this location.
7
We compare the value stored at location 5 with our target value. We find that it is a match.
Advantage:
Binary search halves the searchable items and thus reduces the count of comparisons to be made to
very less numbers.
Disadvantage:
This algorithm requires the list to be sorted,then only this method is applicable.
8
In [2]:
def binary_search(a,n,key):
low=0
high=n-1
while(low<=high):
mid=int((low+high)/2)
if(key==a[mid]):
return mid
elif(key<a[mid]):
high=mid-1
else:
low=mid+1
return -1
9
In [1]:
def binary_search(a,n,key):
low=0
high=n-1
while(low<=high):
mid=int((low+high)/2)
if(key==a[mid]):
return mid
elif(key<a[mid]):
high=mid-1
else:
low=mid+1
return -1
Linear search starts at the beginning of a list of values, and checks 1 by 1 in order for the result you are
looking for.
A binary search starts in the middle of a sorted array, and determines which side (if any) the value you
are looking for is on.
10
Sorting Techniques:
Sorting refers to arranging data in a particular format. Sorting algorithm specifies the way to arrange data in a
particular order. Most common orders are numerical or lexicographical orders.
The importance of sorting lies in the fact that data searching can be optimized to a very high level, if data is
stored in a sorted manner.
1. Selection Sort
2. Bubble Sort
1.Selection Sort:
Selection sort is also known as push-down sorting. As the name suggests the first element of the list is
selected. It is compared with all the elements. If any element is found to be lesser than the selected element,
these two are interchanged. This procedure is repeated till all the elements in the list are sorted.
11
12
Advantages:
The main advantage of the selection sort is that it performs well on a small list.
Because it is an in-place sorting algorithm, no additional temporary storage is required beyond whatis
needed to hold the original list.
Its performance is easily influenced by the initial ordering of the items before the sorting process.
Disadvantages:
The primary disadvantage of the selection sort is its poor efficiency when dealing with a huge list of
items.
The selection sort requires n-squared number of steps for sorting 'n' elements.
In [3]:
def selectionSort(a):
for i in range(len(a)):
least=i
for k in range(i+1,len(a)):
if a[k]<a[least]:
least=k
temp=a[least]
a[least]=a[i]
a[i]=temp
a=[50,30,10,20,40,70,60]
print ("Original list",a)
selectionSort(a) # Calling to the function
print("Selection Sort:",a)
Alternative Way:
13
In [6]:
In [7]:
2. Bubble Sort:
This is the simplest and oldest sorting technique when compared with all the other sorting techniques.
It is also called as exchange sort.
In this sorting technique the adjacent elements are compared and interchanged if necessary.
Procedure:
14
1. Compare first and second elements. If the first element is greaterthan the second element, then
interchange these two elements.
2. Compare second and third elements. If the second element is greater than third element then make an
interchange.
3. The process is repeated till the last and second last element is compared and swapped if necessary.
This completes the first pass. At the end of the first pass, the largest element will get its exact final position
i.e., it occupies the last position.
The step-1 to step-3 are repeated n-1 times for the elements between 1 to n-1 because the nth element is
already sorted.
Advantages:
Disadvantages:
It runs slowly and hence it is not efficient, because Even if the elements are sorted, n-1 iterations are
required to sort.
It is not used for sorting the list of larger size.
It is insufficient algorithm because the number of iterations increases with increase in number of
elements to be sorted.
15
Python program to implement Bubble Sort technique:
In [1]:
In [2]:
16
Tasks
24