Question

In: Computer Science

C++ Consider the following algorithm for finding the distance between the two closest elements in an...

C++

Consider the following algorithm for finding the distance between the two closest elements in an array of numbers. ALGORITHM MinDistance(A[0..n − 1]) //Input: Array A[0..n − 1] of numbers //Output: Minimum distance between two of its elements dmin←∞ for i ←0 to n − 1 do for j ←0 to n − 1 do if i ≠ j and |A[i]− A[j ]| < dmin dmin ←|A[i]− A[j ]| return dmin Make as many improvements as you can in this algorithmic solution to the problem. If you need to, you may change the algorithm altogether; if not, improve the implementation given. List and explain the improvements you made and then present the improved algorithm.

Solutions

Expert Solution

The algorithm given in the problem takes O(n2) time because two for loops are present (nested).

We can improve the algorithm by reducing its time complexity to O(nlogn).

So we can improve the algorithm by using the idea of sorting.

We are given an array of integers and we have to return the minimum distance between closest elements.

Example-

Input- { 1, 3, 20, 19 , 5, 24}

Output- 1

Minimum difference is between 19 and 20.

Explanation-

we will sort the array int ascending order , it becomes {1, 3, 5, 19, 20, 24}.

set min difference = infinite

now traverse through the array and find difference between adjacent pairs and update min difference.

d=3-1=2 d<min difference , update min difference = 2

d=5-3= 2 d=min difference , no need to update min difference

d=19-5= 14 d>min difference , no need to update min difference

d=20-19=1 d<min difference, update min difference = 1

d=24-20 =4 d> min difference , no need to update min difference

So after traversing the whole array we get min difference = 1.

Steps-

1)Sort the given array by using sort function which takes O(nlogn ) time.

2) Initialize the minimum distance dmin as infinite.

3) Now after sorting compare the adjacent pairs in the sorted array and keep updating minimum difference -

a) if a[i+1] -a[i] < minimum difference then minimum difference = a[i+1] - a[i]

b) else do nothing and continue

this takes O(n) time.

So the overall time taken by the algorithm becomes is O(nlogn) + O(n) whick is equal to O(nlogn).

Algorithm for above steps-

Algorithm MinDistance( A[0........n-1]){

//Input- Array of size n

//Output- Minimum distance between two elements of array

//sort the array using sort function from algorithm library

sort(A, A+n);

dmin <- INT_MAX

for i<-0 to n do

        if A[i+1] - A[i]  <  dmin 

                     dmin = A[i+1] - A[i] ;

return dmin

This above algorithm takes O(nlogn) time.


Related Solutions

Closest and Farthest points. Distance between two points (x1,y1) and (x2, y2) can becalculated...
Closest and Farthest points. Distance between two points (x1, y1) and (x2, y2) can becalculated as d = sqt( (x1 − x2) ^2 + (y1 − y2)^ 2). You are given the coordinates of a sourcepoint and three destination points. Write a Python program stored in a file q8.py thattakes these information as input and determines the closest and farthest destinationpoints from the source point.ex:Enter source coordinates : 0 0Enter point A coordinates : 1 4Enter point B coordinates :...
The closest distance between the sun and Mercury is dm = 4.6 x 1010m. What is...
The closest distance between the sun and Mercury is dm = 4.6 x 1010m. What is the intensity of the sun’s light at Mercury? Mercury has a radius of rmer = 2.4 x 106m. How much power from the sun hits Mercury? Use the climate model to calculate the average temperature of Mercury, given a reflectivity of a = 0.07. At this distance, the temperatures reach 700 K during the day at the equator.   How does this compare to your...
Finding the complexity of Finding the largest two elements in a queue of size n+3 using...
Finding the complexity of Finding the largest two elements in a queue of size n+3 using Naïve search. with explain
Consider the following returns. The Correlation between Stock X's and Stock Z's returns is closest to:...
Consider the following returns. The Correlation between Stock X's and Stock Z's returns is closest to: Year End Stock X Realized Return Stock Y Realized Return Stock Z Realized Return 2004 20.1% -14.6% 0.2% 2005 72.7% 4.3% -3.2% 2006 -25.7% -58.1% -27.0% 2007 56.9% 71.1% 27.9% 2008 6.7% 17.3% -5.1% 2009 17.9% 0.9% -11.3% 0.71 0.60 0.62 0.05
consider the following returns. The Correlation between Stock X's and Stock Z's returns is closest to:...
consider the following returns. The Correlation between Stock X's and Stock Z's returns is closest to: Year End Stock X Realized Return Stock Y Realized Return Stock Z Realized Return 2004 20.1% -14.6% 0.2% 2005 72.7% 4.3% -3.2% 2006 -25.7% -58.1% -27.0% 2007 56.9% 71.1% 27.9% 2008 6.7% 17.3% -5.1% 2009 17.9% 0.9% -11.3% 0.71 0.60 0.62 0.05
Use quickselect algorithm to implement the two algorithms for finding the kth smallest integer in a...
Use quickselect algorithm to implement the two algorithms for finding the kth smallest integer in a set of integers. MUST USE JAVA AND QUICKSELECT. Algorithm 1: Procedure SELECT( k,S) { if ISI =1 then return the single element in S    else { choose an element randomly from S;               let S1, S2, and S3 be the sequences of elements in S                 less than, equal to, and greater than m, respectively;              if IS1I >=k then return SELECT(k,S1)              ...
z scores are useful for A) finding the average B) measuring distance C) comparing scores on...
z scores are useful for A) finding the average B) measuring distance C) comparing scores on different scales D) none of the above Kurtosis refers to a distribution that is negatively skewed. True or false The following is an example of a null hypothesis: People who eat dinner after 6 PM weigh more than people who do not eat after 6 PM. True or False The following is an example of a directional hypothesis: Students who spend more hours studying...
Describe an optimal algorithm for finding the integers common to two lists of n integers each....
Describe an optimal algorithm for finding the integers common to two lists of n integers each. Evaluate how long each step in your algorithm takes using Θ-notation.
Write a C++ function which will search for TWO elements. The two elements must be right...
Write a C++ function which will search for TWO elements. The two elements must be right next to each other, contiguously. The prototype of the function should be: bool search_adjacent_elements(int const *x, size_t n, int target1, target2); It will return true if target1 and target2 exist in the array x somewhere, with target1 immediately to the left of target2. For example: int x[] = { 3, 5, 10, 14, 19, 25, 45, 60 }; cout << search_adjacent_elements(x, sizeof x /...
Consider two oppositely charged, isolated parallel plates separated by distance D, with capacitance C, charge Q,...
Consider two oppositely charged, isolated parallel plates separated by distance D, with capacitance C, charge Q, and stored energy U. D is small compared to the dimensions of the plates. For each statement below, select "True" or "False". 1. Because of energy conservation, inserting a dielectric leaves U unchanged. 2. When D is halved, Q stays the same. 3. Inserting a dielectric decreases C. 4. When D is doubled, U increases. 5. When D is doubled, C is doubled. 6....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT