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

Assignment 2

Uploaded by

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

Assignment 2

Uploaded by

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

1. Sort an array of n-elements in which every element is 0 or 1 and discuss its time complexity.

2. Given a sorted array of n-elements. Find any 2-element a and b such that a+b=1000 using
linear and Binary search.
3. Given an array A [0…n−1], where each element in the array represents a vote in an election,
and each vote is indicated by an integer that corresponds to the ID of the selected candidate,
create an algorithm to determine the winner of the election. Find the time and space
complexity.
4. What is the minimum number of comparisons needed to verify if an integer appears more
than n/2 times in a sorted array of n integers?
5. Write algorithms and analyse (Time and Space) the following sorting algorithms: Bubble
Sort, Selection Sort, Insertion Sort, Merge Sort, Quick Sort, and Heap Sort. Implement each
of the algorithms in C language.
6. Write the analysis of the algorithms given in Q5 on different types of data:
a) Generate random data of n- integers.
b) Use an already sorted data.
c) Use data sorted in reverse order.
d) Use data that is mostly sorted but with a few elements out of place.
Use the above data as test cases for algorithms.
7. Analyze the performance of Algorithms in Q5:
a) Measure the execution time for each sorting algorithm on each type of data and each
data size.
b) Calculate the number of comparisons and swaps for each algorithm.
c) Explain the observed time complexity for each algorithm, considering best-case, worst-
case, and average-case scenarios.
d) Compare the algorithms based on their performance and explain why certain algorithms
perform better or worse in different situations.

8. Discuss situations, i.e., data where Insertion Sort would be a better choice than Quick Sort
or Merge Sort.
9. What are the advantages of using Insertion Sort for small data.
10. Explain why Insertion Sort is more efficient than other O(n²) algorithms like Bubble Sort
and Selection Sort for nearly sorted arrays.
11. Compare Quick Sort with Merge Sort. Under what conditions would you prefer one over
the other? Discuss in terms of time complexity, space complexity, and use cases.
12. Why is quicksort often preferred in practice, even though it has a worst-case time
complexity of O(n²)?
13. There are two sorted lists of size n. write an algorithm to find the median element if you
combine the two lists. What is the time complexity of the algorithm?
14. Consider a modified version of binary search, where the array is divided into three equal
parts (ternary search) instead of two. Write the recurrence relation for this ternary search
and determine its time complexity.
15. Given an input array A. Let us assume that there can be duplicates in the list. Now, search
for an element in the list in such a way that we get the highest index if there are duplicates.
16. Given a set of n points, 𝑆 = { 𝑝1 , 𝑝2 , 𝑝3 , … , 𝑝𝑛 } , where each point 𝑝𝑖 = (𝑥𝑖 , 𝑦𝑖 ), find the
pair of points with the shortest distance between them from all possible pairs. Assume that
all points lie in a single dimension. Find the time complexity.
17. Solve the problem 14 when the points are in two-dimensional space.
18. Give the algorithm and time complexity to calculate 𝑘 𝑛 .
19. Given a sequence of n numbers A(1), A(2),…, A(n), develop an algorithm to find the
contiguous subsequence A(i), A(i+1),…, A(j) such that the sum of the elements in this
subsequence is maximized. For example, in the sequence {−2,11,−4,13,−5,2}, the
maximum sum is 20, and in the sequence {1,−3,4,−2,−1,6}, the maximum sum is 7.
20. Given a box with lock and keys where one lock can be opened by one key in the box. We
need to match the pair, and we cannot match locks with locks and keys with keys. Give the
solution to this problem and its time complexity.

You might also like