All of these questions are from the algorithms fourth editio

All of these questions are from the algorithms fourth edition book by sedgewick I have checked the solution manual and the answers there are not what I am looking for please do not use them and also do not give me anything from github currently. Please use the algs jar file used for the book.

4. My code is quadratic change it to linearithmic.

//import edu.princeton.cs.algs4.*;

public class Exercise_1_4_16 {

   public static double closest_pair(double[] a) {

       // Your code here.

       double least = Double.MAX_VALUE;

  

       double aLength = a.length;

       for(int i=0; i< aLength; i++)

       {

           for(int j=i+1; j< aLength;j++)

           {

               least = Math.min(least, Math.abs( a[i]-a[j]));

           }

       }

      

       return least;

   }

   public static void main(String[] args) {

       double[] a = StdIn.readAllDoubles();

       StdOut.printf(\"%6.4f\", closest_pair(a));

   }

}

5

Give a quick answers as to why this intuitive implementation of union for quick find is not correct/won’t work.

Public void union(int p, int q)

{

if(connected(p,q)) return;

//Rename p’s component to q’s name.

for(int i = 0; i < id.length; i++)

   if(id[i] == id[p]) id[i] = id[q];

count--;

                  

}      

      

Solution

5. SInce the value of id[p] changes to id[q] in the for loop. Thus for any object r > p with id[r] equal to id[p], its value will not be updated to id[q]

All of these questions are from the algorithms fourth edition book by sedgewick I have checked the solution manual and the answers there are not what I am looki
All of these questions are from the algorithms fourth edition book by sedgewick I have checked the solution manual and the answers there are not what I am looki

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site