Menu

Question 1 Suppose N Points Lying Horizontal Axis Given Coordinates X 0 X 1 X N 1 Distan Q43778529

Need help with these about Algorithms.

Question 1 Suppose n points lying on a horizontal axis are given by their coordinates x[0], X[1], ... , X[n-1). The distance

Question 1 Suppose n points lying on a horizontal axis are given by their coordinates x[0], X[1], … , X[n-1). The distance between points i and jis x[i] – [j] . The task is to find the distance between two nearest points. For example, given 5 points with coordinates 8, 1, 15, 3, 12, the two nearest points have coordinates 1 and 3 and the distance is 11 – 31=2. The two algorithms Al and A2 are presented below. ALGORITHM Al (x[0..n-1]) // Input: array x[o..n-l] of x-coordinates of n points on X-axis 11 Output: the distance between two nearest points minDist + x[0] – X[1] | for it to n-2 do for j i +l to n-1 do Distance + x[i] – [j] | if Distance < mindist mindist + Distance output minDist ALGORITHM A2 (X[0..n-1]) // Input: array x[0..n-1) of X-coordinates of n points on X-axis // Output: the distance between two nearest points Sort array X using quick-sort minDist + x[0] – X[1] for iti to n-2 do Distance + x[i] – x[i+1] if Distance < mindist mindist + Distance output mindist (a) Provide a brief explanation on how algorithms Al and A2 operate and why they succeed in solving the problem. [2 marks] (b) Trace algorithm Al on the list 6, 15, 2, 13, 3, 10. Show how the values of variables Distance and mindist change for different values i and j. What does the algorithm output? [2 marks] (c) What is the time complexity of algorithm Al in terms of big-O? Explain your answer by using the formal definition of big-o. [3 marks] Show transcribed image text Question 1 Suppose n points lying on a horizontal axis are given by their coordinates x[0], X[1], … , X[n-1). The distance between points i and jis x[i] – [j] . The task is to find the distance between two nearest points. For example, given 5 points with coordinates 8, 1, 15, 3, 12, the two nearest points have coordinates 1 and 3 and the distance is 11 – 31=2. The two algorithms Al and A2 are presented below. ALGORITHM Al (x[0..n-1]) // Input: array x[o..n-l] of x-coordinates of n points on X-axis 11 Output: the distance between two nearest points minDist + x[0] – X[1] | for it to n-2 do for j i +l to n-1 do Distance + x[i] – [j] | if Distance

Expert Answer


Answer to Question 1 Suppose n points lying on a horizontal axis are given by their coordinates x[0], X[1], … , X[n-1). The dist…

OR