0% found this document useful (0 votes)
0 views

Lab 2 - Search and sort

The document outlines three programming tasks: implementing linear search, binary search using recursion, and merge sort without using built-in functions. It provides algorithms and example code for each search method, highlighting their time complexities and operational details. Linear search operates in O(n) time, binary search in O(log n) time, and merge sort in O(n log n) time, with merge sort requiring additional memory for temporary arrays.

Uploaded by

hamidraza
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Lab 2 - Search and sort

The document outlines three programming tasks: implementing linear search, binary search using recursion, and merge sort without using built-in functions. It provides algorithms and example code for each search method, highlighting their time complexities and operational details. Linear search operates in O(n) time, binary search in O(log n) time, and merge sort in O(n log n) time, with merge sort requiring additional memory for temporary arrays.

Uploaded by

hamidraza
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

keyboard_arrow_down Lab Task 2

Task 2-1 Write a code for linear search without using Builtin functions

take size of list from user not more than 20


take values of list from user

Task 2-2 - Write a code for Binary search using recurssion without using Builtin functions

take size of list from user not more than 20


take values of list from user

Task 2-3 - Write a code for Merge sort without using Builtin functions

Searching Algorithms:

keyboard_arrow_down Linear Search


Linear search, also known as sequential search,for finding a target value within a list or array. It involves checking each element of the list one
by one, starting from the beginning, until the target value is found or until all elements have been examined.

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)

the number 5 is on index 3

keyboard_arrow_down Binary Search


Binary search is an efficient algorithm for finding a target value within a sorted list or array. It works by repeatedly dividing the search interval in
half, narrowing down the possible locations of the target element.

Algorithm

Binary search begins with a sorted list or array of elements.

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.

Checks if the target value matches the element at the midpoint.

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

print("\nmid", mid, "\tvalue at mid:", arr[mid])

if array[mid]==x: #equal
return mid

elif array[mid]<x: # value is greater


return binary(array,x,mid+1,r) # left = mid +1

else: #value is smaller


return binary(array , x , l , mid-1) # right = mid-1
return -1

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

mid 4 value at mid: 5

left 5 right 9

mid 9 value at mid: 10

left 5 right 8

mid 8 value at mid: 9

the number 9 is on index 8

keyboard_arrow_down Merge sort


Merge sort is a sorting algorithm that follows the divide-and-conquer rule. It works by recursively dividing the input array into smaller
subarrays, sorting each subarray, and then merging the sorted subarrays back together to produce a sorted output array. The main steps of the
merge sort algorithm are as follows:

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.

You might also like