0% found this document useful (0 votes)
16 views58 pages

Linear and Binary Search Explained

Uploaded by

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

Linear and Binary Search Explained

Uploaded by

kgf6748
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

3.

6 Searching:

• Searching is the process of finding a given value position in a list of values.


• It decides whether a search key is present in the data or not.
• It is the algorithmic process of finding a particular item in a collection of items.
• It can be done on internal data structure or on external data structure.

Types of Searching:
There are two types of searching. They are
i) Linear or Sequential Searching
ii) Binary Searching
i) Linear Search:
• Linear Search is also called as Sequential search. Sequential search starts at
the beginning of the list and checks every element of the list.
• It is a basic and simple search algorithm.
• Sequential search compares the element with all the other elements given in
the list.
• If the element is matched, it returns the value index, else it returns -1.
• It is mostly used to search an unordered list of elements.

Concept of Linear Search

Let's understand the following steps to find the element key = 7 in the given list.

Step - 1: Start the search from the first element and Check key = 7 with each element
of list x.
Step - 2: If element is found, return the index position of the key.

Step - 3: If element is not found, return element is not present.

Program:
def linear(lst,n, key):
n = len(lst)
for i in range(0, n):
if lst[i] == key:
return i
return -1
n = int(input("Enter the total number of elements in the list: "))
lst = []
for i in range(n):
[Link](int(input("Enter the value: ")))
print("List:", lst)
key = int(input("Enter the value to search: "))
result = linear(lst,n, key)
if result == -1:
print("Element not found")
else:
print("Element found at index:", result)

OUTPUT:
Enter the total number of elements in the list: 4
Enter the value: 5
Enter the value: 4
Enter the value: 3
Enter the value: 8
List: [5, 4, 3, 8]
Enter the value to search: 4
Element found at index: 1
Complexity Analysis of Linear Search:
Time Complexity:
• Best Case: In the best case, the key might be present at the first index. So the best
case complexity is O(1)
• Worst Case: In the worst case, the key might be present at the last index i.e.,
opposite to the end from which the search has started in the list. So the worst-case
complexity is O(N) where N is the size of the list.
• Average Case: O(N)
Auxiliary Space:
O(1) as except for the variable to iterate through the list, no other variable is used.
Advantages of Linear Search:
• Linear search can be used irrespective of whether the array is sorted or not. It can be
used on arrays of any data type.
• Does not require any additional memory.
• It is a well-suited algorithm for small datasets.
Drawbacks of Linear Search:
• Linear search has a time complexity of O(N), which in turn makes it slow for large
datasets.
• Not suitable for large arrays.
3.7 Binary Search:
Definition:
Binary search is a searching algorithm that works efficiently with a sorted list.
Binary search used in a sorted array by repeatedly dividing the search interval in
half. The idea of binary search is to use the information that the array is sorted and
reduce the time complexity to O(log N).

Conditions:
To apply Binary Search algorithm:
• The data structure must be sorted.
• Access to any element of the data structure takes constant time.
Algorithm:
In this algorithm,
• Divide the search space into two halves by finding the middle index
“mid”.
• Compare the middle element of the search space with the key.
• If the key is found at middle element, the process is terminated.
• If the key is not found at middle element, choose which half will be
used as the next search space.
• If the key is smaller than the middle element, then the left
side is used for next search.
• If the key is larger than the middle element, then the right
side is used for next search.
• This process is continued until the key is found or the total search
space is exhausted.
Program:
def binary_search(list1, n):
low = 0
high = len(list1) - 1
mid = 0
while low <= high:
mid = (high + low) // 2
if list1[mid] < n:
low = mid + 1
elif list1[mid] > n:
high = mid - 1
else:
return mid
return -1
list1 = [12, 24, 32, 39, 45, 50, 54]
n = 45
result = binary_search(list1, n)
if result != -1:

print("Element is present at index", str(result))

else:

print("Element is not present in list1")


Complexity Analysis of Binary Search:
• Time Complexity:
• Best Case: O(1)
• Average Case: O(log N)
• Worst Case: O(log N)
• Auxiliary Space: O(1), If the recursive call stack is considered then
the auxiliary space will be O(logN).
Advantages of Binary Search:
• Binary search is faster than linear search, especially for large arrays.
• More efficient than other searching algorithms with a similar time
complexity, such as interpolation search or exponential search.
• Binary search is well-suited for searching large datasets that are
stored in external memory, such as on a hard drive or in the cloud.
Drawbacks of Binary Search:
• The array should be sorted.
• Binary search requires that the data structure being searched be
stored in contiguous memory locations.
• Binary search requires that the elements of the array be comparable,
meaning that they must be able to be ordered.
Applications of Binary Search:
• Binary search can be used as a building block for more complex
algorithms used in machine learning, such as algorithms for training
neural networks or finding the optimal hyperparameters for a model.
• It can be used for searching in computer graphics such as algorithms
for ray tracing or texture mapping.
• It can be used for searching a database.
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Experiment 8
Implementation of hash tables
To implement hash table to perform the following hashing techniques
1. Separate chaining
2. Open addressing with linear probing

Program:
class HashTable:
def __init__(self, n, v):
self.hash_value = [Link](" ")
self.n = n
[Link] = []
for i in range(n):
[Link]([])
def separate_chain(self):
for z in self.hash_value:
i = int(z)
x = i % self.n
tem = [Link][x]
[Link](i)
return [Link]
def linear(self):
def recurs(k, y):
x = k % self.n
if len([Link][x]) != 0:
recurs(k + 1, y)
else:
[Link][x].append(y)
for i in self.hash_value:
recurs(int(i), int(i))
return [Link]
while True:
print("1. Separate Chaining method \n2. Open Addressing with Linear
probing method")
x = int(input("Enter the Option : "))
if x == 1:
# ... (code for Separate Chaining)
elif x == 2:
# ... (code for Linear Probing)
else:
break

Output:
1. Separate Chaining method
2. Open Addressing with Linear probing method
Enter the Option : 1
Enter Table Size : 10
Enter Values Separate By Space : 23 22 25 15 12 13
[[], [], [22, 12], [23, 13], [], [25, 15], [], [], [], []]
Enter the Option : 2
Enter Table Size : 10
Enter Values Separate By Space : 45 11 13 17 15
[[], [11], [], [13], [], [45], [15], [17], [], []]
1. Define sorting
Sorting arranges the numerical and alphabetical data present in a list in a specific order
or sequence. There are a number of sorting techniques available. The algorithms can be
chosen based on the following factors-Size of the data structure-Algorithm efficiency-
Programmer's knowledge of the technique.
2. What are the types of Sorting?
Sorting can be divided in to two categories.
i) Internal Sorting: If all the data that is to be sorted can be adjusted at a time
in the main memory, the internal sorting is being performed.
ii) External Sorting: When the data that is to be sorted cannot be
accommodated in the memory at the same time and some has to be kept in
auxillary memory such as hard disk, floppy disk etc
3. Difference between Internal and External Sorting.
Internal Sorting External Sorting
• The sorting process that takes • If the data being sorted do not fit
place entirely within main into the main memory and they
memory of the computer. reside in slower external memory
external sorting is done.
• Data to be sorted is small enough • Used to handle massive amount
to be held in main memory of data in external memory
• Does not make use of any • Makes use of external resource
external resource
• Eg: Insertion sort, Bubble sort, • External Merge Sort
Selection sort.
4. Define bubble sort
Bubble sort is a simple sorting algorithm that works by repeatedly stepping through the
list to be sorted, comparing each pair of adjacent items and swapping them if they are in
the wrong order. The pass through the list is repeated until no swaps are needed, which
indicates that the list is sorted. The algorithm gets its name from the way smaller
elements "bubble" to the top of the list.
5. What are the steps in quicksort?
The steps are:
i) Pick an element, called a pivot, from the list.
ii) Reorder the list so that all elements with values less than the pivot come
before the pivot, while all elements with values greater than the pivot come
after it(equal values can go either way). After this partitioning, the pivot is in
its final position. This is called the partition operation.
iii) Recursively apply the above steps to the sub-list of elements with smaller
values and separately to the sub-list of elements with greater values.
6 What are the advantages of insertion sort
Advantages:
i) Simplest sorting technique and easy to implement
ii) It performs well in the case of smaller lists.
iii) It leverages the presence of any existing sort pattern in the list
Disadvantages:
i) Efficiency of O(n) is not well suited for large sized lists
ii) POGA requires large number of elements to be shifted
7. Mention the different ways to select a pivot element.
The different ways to select a pivot element are
i) Pick the first element as pivot
ii) Pick the last element as pivot
iii) Pick the Middle element as pivot
iv) Median-of-three elements
v) Pick three elements, and find the median x of these elements
vi) Use that median as the pivot.
vii) Randomly pick an element as pivot.
8. Define Searching
Searching refers to determining whether an element is present in a given list elements or
not. If the element is present, the search is considered as successful otherwise it is
considered as an unsuccessful search. The choice of a searching technique is based on
the following factors.
i) Order of elements in the list i.e.,
ii) random or sorted
iii) Size of the list
9. Mention the types of searching
The types are
i) Linear search
ii) Binary search
10. What is meant by linear search?
Linear search or sequential search is a method for finding a particular value in a list that
consists of checking every one of its elements, one at a time and in sequence, until the
desired one is found.
11. What is binary search?
For binary search, the array should be arranged in ascending or descending order. In
each step, the algorithm compares the search key value with the middle element of the
array. If the key match, then a matching element has been found and its index, or
position, is returned. Otherwise, if the search key is less than the middle element, then
the algorithm repeats its action on the sub-array to the left of the middle element or, if
the search key is greater, on the sub-array to the right.
12 Define Hashing
Hashing is the transformation of string of characters into a usually shorter fixed length
value or key that represents the original string Hashing is used to index and retrieve
items in a database because it is faster to find the item using the short hashed key than
to find it using the original value.
13 What do you mean by hash table?
The hash table data structure is merely an array of some fixed size, containing the keys.
A key is a string with an associated value. Each key is mapped into some number in a
range 0 to Hash Size-1 and placed in the appropriate cell.
14 What do you mean by hash function?
A hash function is a key to address transformation which acts upon a given key to
compute the relative position of the key in an array. The choice of hash function should
be simple it must distribute the data evenly. A simple hash function is
Hash(key)=key mod H_Size

15 Write the importance of hashing.


1. Maps key with the corresponding value using hash function.
2. Hash tables support the efficient addition of new entries and the time
spent on searching for the required data is independent of the number of
items stored.
16 What is open addressing?
Open addressing is also called closed hashing, which is an alternative to resolve the
collisions with linked lists. In this hashing system, if a collision occurs, alternative cells
are tired until an empty cell is found.
17 List the strategies in open addressing
There are three strategies in open addressing:
i) Linear probing
ii) Quadratic probing
iii) Double hashing
18 What do you mean by collision in hashing?
When an element is inserted, it hashes to the same value as an already insert element,
and then it produces collision.
19 What do you mean by collision in hashing?
When an element is inserted, it hashes to the same value as an already insert element,
and then it produces collision.
20 What is rehashing?
If the table gets too full, the running time for the operations will start taking long and
inserts might fail for open addressing. A solution is table that is twice as to build
another for with the associated new wind down the entire original hash table, computing
the new hash value for each element and inserting it in the new table. This entire
operation is called rehashing.
21 List the advantages of using hash tables
i) Hash tables have high performance when looking up data, inserting &
deleting existing values.
ii) The time complexity for hash tables is constant regardless of the number of
items in the table.
iii) They perform very well even when working with large datasets.
22 List the disadvantages of using hash tables
i) Cannot use a null value as a key.
ii) Collisions cannot be avoided when generating keys using. Hash functions
Collisions occur when a key that is already in use is generated.
iii) If the hashing function has many collisions, this can lead to performance
decrease.
23 What are the collision resolution methods?
The following are the collision resolution methods,
i) Separate chaining
ii) Open addressing
iii) Multiple hashing
24 What do you mean by separate chaining?
Separate is a collision resolution technique to keep the list of all elements that hash to
the same value. This is called separate chaining because each hash table element is a
separate chain (linked list). Each linked list contains all the elements whose keys hash
to the same index.
25 Write the advantage of separate chaining:
More number of elements can be inserted as it uses linked lists
26 Write the disadvantages of separate chaining.
i) The elements are evenly distributed. Some elements may have more
elements and some may not have anything.
ii) It requires pointers. This leads to slow the algorithm down a bit because of
the time required to allocate new cells, and also essentially requires the
implementation of a second data structure.
27 What do you mean by Probing?
Probing is the process of getting next available hash table array cell.=
28 What do you mean by linear probing?
Linear probing is an open addressing collision resolution strategy in which F is a linear
function of i, F(i)=i. This amounts to trying sequentially in search of an empty cell. If
the table is big enough, a free cell can always be found, but the time to do so can get
quite large.
29 What do you mean by quadratic probing?
Quadratic probing is an open addressing collision resolution strategy in which F(i)-12.
There is no guarantee of finding an empty cell once the table gets half full if the table
size is not prime. This is because at most half of the table can be used as alternative
locations to resolve collisions.
30 List the limitations of linear probing.
i) Time taken for finding the next available cell is large.
ii) In linear probing, we come across a problem known as clustering.
31 Mention one advantage and disadvantage of using quadratic probing.
Advantage: The problem of primary clustering is eliminated.
Disadvantage: There is no guarantee of finding an unoccupied cell on the table is nearly
half full
32 Define Extendible Hashing
Extendible Hashing is a dynamic hashing method wherein directories, and buckets are
used to hash data. It is an aggressively flexible method in which the hash function also
experiences dynamic changes.

You might also like