Lab 2 - Search and sort
Lab 2 - Search and sort
Task 2-1 Write a code for linear search without using Builtin functions
Task 2-2 - Write a code for Binary search using recurssion without using Builtin functions
Task 2-3 - Write a code for Merge sort without using Builtin functions
Searching Algorithms:
Algorithm:
1. Start from the first element of the list.
2. Compare the target value with each element of the list sequentially.
3. If the target value matches the current element, return the index of that element.
4. If the target value is not found after examining all elements, return a message indicating that the value is not present.
Linear search is simple to implement and works well for small lists or unsorted data. However, its time complexity is O(n), meaning that as the
size of the list grows, the time taken to search linearly also increases linearly.
Therefore, it may not be efficient for large datasets, especially when compared to more advanced search algorithms like binary search for
sorted lists.
def linear(arr,l,x):
for i in range(0,l):
if (arr[i]==x):
return i
return -1
arr= [1,3,4,5,10,23]
x = 5
l = len(arr)
result = linear(arr,l,x)
print("the number", x,"is on index ",result)
Algorithm
sets up two pointers, left and right, initially pointing to the beginning and end of the list, respectively, representing the search interval.
calculate the midpoint of the search interval, often referred to as mid, using the formula (left + right) // 2.
If the target matches the element at mid, the search is successful, and the algorithm returns the index of the target.
If the target is less than the element at mid, the search continues in the left half of the interval by updating right = mid - 1.
If the target is greater than the element at mid, the search continues in the right half of the interval by updating left = mid + 1.
repeated until the target value is found or until the search interval is empty (left > right).
If the target is not found after examining all elements, the algorithm returns -1 to indicate that the target value is not present in the list.
Binary search has a** time complexity of O(log n)**, making it significantly faster than linear search, especially for large datasets. However, it
requires that the list be sorted beforehand, which may add an initial overhead. Nonetheless, binary search is a fundamental and widely used
algorithm in computer science due to its efficiency.
def binary(array,x,l,r):
print("\nleft",l,"right",r, "\n")
while l<=r:
mid = l+(r-1)//2; # mid = l+r/2 , celling value to get integer
if array[mid]==x: #equal
return mid
arr= [1,2,3,4,5,6,7,8,9,10]
x=9
result = binary(arr, x, 0, len(arr)-1)
print("\nthe number", x,"is on index ", result)
left 0 right 9
left 5 right 9
left 5 right 8
Algorithm
Divide: Divide the unsorted array into two halves.
Conquer: Recursively sort each half of the array using merge sort.
Merge: Merge the sorted halves back together to produce a single sorted array.
or
Base Case: If the input array has zero or one element, it is already sorted, so no further action is needed.
Divide Step: Divide the input array into two roughly equal halves.
Conquer Step: Recursively apply merge sort to each half of the array until each half contains only one element, which is considered
sorted.
Merge Step: Merge the sorted halves back together. This involves comparing elements from each half and placing them in the correct
order in a temporary array. Once all elements are merged, the temporary array becomes the sorted version of the original input array.
Merge sort is known for its stability (maintains the relative order of equal elements) and its consistent performance, with a time complexity of
O(n log n) for average, best, and worst-case scenarios.
However, it requires additional space for the temporary array used during the merge step, which makes it less memory-efficient compared to in-
place sorting algorithms like quicksort.
Nonetheless, merge sort is widely used and serves as the basis for many other sorting algorithms and techniques.