DAA Assignment 1 Solution by ST
DAA Assignment 1 Solution by ST
Q1.what is binary search ? write a algorithm , time complexity and pseudo code with diagram
Ans:-
Binary search is a classic algorithm used to ef ciently locate a target value within a sorted array or
list. It works by repeatedly dividing the search interval in half.
Algorithm Explanation:
1. Start: De ne the boundaries of the search interval (left and right) to cover the entire
array initially.
2. Middle Calculation: Calculate the middle index of the current interval.
3. Comparison: Compare the middle element of the array with the target value.
4. Adjust Search Range: Depending on the comparison result, adjust the boundaries (left
and right) of the search interval to either the left half or the right half of the current
interval.
5. Repeat: Continue the process until the target value is found or the search interval is empty
(left > right).
Pseudo-code:
BinarySearch(array, target):
left = 0
right = length of array - 1
Target: 13
Search Process:
fi
fi
Assignment 1 ST Daa
[2, 4, 7, 10, 13, 18, 22, 27]
L R
[2, 4, 7, 10, 13, 18, 22, 27]
L R
[2, 4, 7, 10, 13, 18, 22, 27]
L R
[2, 4, 7, 10, 13, 18, 22, 27]
M
In each step, L and R represent the current boundaries of the search interval (left and right in
the pseudo-code), and M represents the middle index (mid). The search narrows down the interval
until it nds the target (13 in this case).
Key Points:
Q2.what is linear search ? write a algorithm , time complexity and pseudo code with diagram
Ans:-
Linear search is a simple searching algorithm that sequentially checks each element in a list until
the target element is found or all elements have been checked.
Algorithm Explanation:
LinearSearch(array, target):
for i from 0 to length of array - 1:
if array[i] == target:
return i
return -1
fi
fi
Assignment 1 ST Daa
Diagram:
Target: 7
Search Process:
[5, 9, 3, 7, 2, 8, 4]
^
[5, 9, 3, 7, 2, 8, 4]
^
[5, 9, 3, 7, 2, 8, 4]
^
[5, 9, 3, 7, 2, 8, 4]
^
[5, 9, 3, 7, 2, 8, 4]
^
[5, 9, 3, 7, 2, 8, 4]
^
In each step, ^ represents the current element being checked. The linear search algorithm continues
checking each element until it nds the target (7 in this case) or reaches the end of the list without
nding the target.
Key Points:
• Linear search has a time complexity of O(n),where n is the number of elements in the array.
• It does not require the array to be sorted.
• It is straightforward but may be inef cient for large lists compared to more advanced search
algorithms like binary search (for sorted lists) or hash tables (for unordered data).
Linear search is useful when:
Q3.what is selection sort ? write a algorithm ,time complexity and pseudo code with diagram
Selection Sort is a simple comparison-based sorting algorithm. It works by dividing the input array
into two parts: sorted and unsorted. The algorithm repeatedly nds the smallest (or largest,
depending on sorting order) element from the unsorted part and swaps it with the rst element of
the unsorted part. This process continues until the entire array is sorted.
Algorithm:
1. Selection Sort Algorithm:
fi
fi
fi
fi
fi
fi
Assignment 1 ST Daa
◦ Find the smallest element in the unsorted array.
◦ Swap it with the element at the beginning of the unsorted array.
◦ Move the boundary between the sorted and unsorted subarrays one element to the
right.
Pseudo Code:
Below is a diagrammatic representation of the Selection Sort algorithm. This diagram illustrates
how the smallest element from the unsorted part of the array is selected and swapped with the rst
element of the unsorted part in each iteration.
Pass 1:
[1, 4, 5, 2, 7] // Swap 7 with 1 (smallest element)
Pass 2:
[1, 2, 5, 4, 7] // Swap 4 with 2 (smallest element)
Pass 3:
[1, 2, 4, 5, 7] // No swap as 5 is already in its correct
position
Pass 4:
[1, 2, 4, 5, 7] // No swap as 7 is already in its correct
position
• Each pass of the algorithm selects the smallest element from the unsorted portion of the
array and swaps it with the rst element of the unsorted portion.
• The sorted portion of the array (left side) grows with each pass, while the unsorted portion
(right side) shrinks until the entire array is sorted.
Selection Sort has a time complexity of O(n2), where n is the number of elements in the array. It is
not very ef cient for large lists compared to more advanced sorting algorithms like Quick Sort or
Merge Sort, but it is simple and easy to implement.
fi
fi
fi
Assignment 1 ST Daa
Q4.what is merge sort ? write a algorithm ,TIME complexity and pseudo code with diagraM
Ans:-
Merge Sort is a divide-and-conquer sorting algorithm. It works by recursively dividing the input
array into smaller subarrays until each subarray contains only one element. Then, it merges these
subarrays back together in a sorted manner.
1. Algorithm:
◦ Divide the unsorted array into two halves.
◦ Recursively sort each half.
◦ Merge the two sorted halves into a single sorted array.
2. Pseudo code:
3. if length(A) <= 1
4. return A
5. else
6. mid = length(A) / 2
7. left = mergeSort( A[0...mid-1] )
8. right = mergeSort( A[mid...length(A)-1] )
9. return merge( left, right )
10. end if
11. end procedure
12.
13. procedure merge( left : array, right : array )
14. result = []
15. while left is not empty and right is not empty do
16. if left[0] <= right[0]
17. append left[0] to result
18. remove first element from left
19. else
20. append right[0] to result
21. remove first element from right
22. end if
23. end while
24.
25. // Append remaining elements
26. append remaining elements of left to result
Assignment 1 ST Daa
27. append remaining elements of right to result
28.
29. return result
30. end procedure
31.
◦ Step 1: The array is recursively divided into halves until each subarray contains one
element.
◦ Step 2: The subarrays are merged back together in sorted order.
◦ Step 3: This merging process continues until the entire array is sorted.
Merge Sort has a time complexity of O(nlogn) in all cases (average, best, and worst), making it
ef cient for large datasets. It is a stable sorting algorithm, meaning that it preserves the relative
order of equal elements. However, it requires additional space proportional to the size of the input
array for storing temporary arrays during the merge process.
fi