0% found this document useful (0 votes)
15 views6 pages

DAA Assignment 1 Solution by ST

Uploaded by

movari9313
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)
15 views6 pages

DAA Assignment 1 Solution by ST

Uploaded by

movari9313
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
You are on page 1/ 6

Assignment 1 ST Daa

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

while left <= right:


mid = (left + right) // 2

// Check if target is present at mid


if array[mid] == target:
return mid
// If target is greater, ignore left half
else if array[mid] < target:
left = mid + 1
// If target is smaller, ignore right half
else:
right = mid - 1

// Target not present in array


return -1
Diagram:

Here's a diagram to visualize how binary search works:

Initial array: [2, 4, 7, 10, 13, 18, 22, 27]

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:

• Binary search is ef cient with a time complexity of O(logn)

O(logn) because it halves the search space in each iteration.


• It requires the array to be sorted initially.
• If the target is found, it returns the index of the target; otherwise, it returns -1 indicating the
target is not in the array.
Binary search is widely used in computer science and is a fundamental algorithm for searching and
retrieval tasks in sorted data structures.

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:

1. Start: Traverse the list from the beginning.


2. Comparison: Compare each element of the list with the target element.
3. Match: If the element matches the target, return its index.
4. End of List: If the target is not found after checking all elements, return a special value
(e.g., -1).
Pseudo-code:

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:

Here's a diagram to visualize how linear search works:

Initial array: [5, 9, 3, 7, 2, 8, 4]

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:

• The list is small.


• The list is unsorted.
• It is necessary to nd all occurrences of a target element.

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:

procedure selectionSort(A : list of sortable items)


n = length(A)
for i from 0 to n-2 do
minIndex = i
for j from i+1 to n-1 do
if A[j] < A[minIndex] then
minIndex = j
swap A[i] and A[minIndex]
Diagram:

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.

Initial Array: [7, 4, 5, 2, 1]

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

Final Sorted Array: [1, 2, 4, 5, 7]


In this diagram:

• 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.

Here's the algorithm for Merge Sort:

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:

procedure mergeSort( A : array of sortable items )

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.

32. Diagram: The diagram illustrates the process of Merge Sort:

◦ 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

You might also like