Question

Problem 2 You are given an array A with n distinct elements. Implement an (n log n)-time algorithm that creates an array B wh

the programming language is in java
0 0
Add a comment Improve this question Transcribed image text
Answer #1

IntKVPair.java

class IntKVPair
{
   private int key;
   private int value;
   IntKVPair(int key, int value)
   {
       this.key=key;
       this.value=value;
   }
   public int getKey()
   {
       return this.key;
   }
   public int getValue()
   {
       return this.value;
   }
}

SortedPositions.java

public class SortedPositions
{
   public static void main(String[] args)
   {
       //Array A initialization
       int[] A={4,9,1,5};
       //Array of IntKVPair objects declaration of size of array A
       IntKVPair[] arrOfObj=new IntKVPair[A.length];
       //Create object with key as A[i] and value as i
       for(int i=0;i<A.length;i++)
       {
           arrOfObj[i]=new IntKVPair(A[i],i);
       }
       //Sort arrOfObj using mergesort which take O(nlogn) time
       mergesort(arrOfObj,0, arrOfObj.length-1);
      
       //Array B contains indices
       int[] B=new int[arrOfObj.length];
       //For each element of A
       for(int i=0;i<B.length;i++)
       {
           //Get index of element A[i] using binarySearch which take O(logn) time
           B[i]=binarySearch(arrOfObj,0,arrOfObj.length,A[i]);
       }
       //Total time O(nlogn) + O(nlogn) = O(nlogn)
       //Print array B
       for(int i=0;i<B.length;i++)
       {
           System.out.print(B[i]+" ");
       }
   }

   //This method will return index of key in arr[]
   public static int binarySearch(IntKVPair arr[],int l, int r, int key)
   {
       if(l<=r)
       {
           //middle
           int m=(l+r)/2;
           //If key found return m
           if(arr[m].getKey()==key)
           {
               return m;
           }
           //If key is lesser go to left subpart
           if(arr[m].getKey()>key)
           {
               return binarySearch(arr, l,m-1,key);
           }
           //goto right subpart
           return binarySearch(arr, m+1,r,key);
       }
       return -1;
   }

   //Standard merge sort
   public static void mergesort(IntKVPair arr[], int l, int r)
   {
       if(l<r)
       {
           int m = (l+r)/2;
           mergesort(arr,l,m);
           mergesort(arr,m+1,r);
           merge(arr,l,m,r);
       }
   }  

   //Standard merge
   public static void merge(IntKVPair arr[], int l, int m, int r)
   {
       int n1=m-l+1;
       int n2 = r-m;

       IntKVPair L[] = new IntKVPair[n1];
       IntKVPair R[] = new IntKVPair[n1];

       for(int i=0;i<n1;i++)
       {
           L[i] = arr[l+i];
       }
       for(int i=0;i<n2;i++)
       {
           R[i] = arr[m+i+1];
       }

       int i=0,j=0;
       int k=l;
       while(i<n1 && j<n2)
       {
           if(L[i].getKey()<R[j].getKey())
           {
               arr[k++]=L[i++];
           }
           else
           {
               arr[k++]=R[j++];
           }
       }
       while(i<n1)
       {
           arr[k++]=L[i++];
       }
       while(j<n2)
       {
           arr[k++]=R[j++];
       }
   }
}

Time complexity:

Merge sort will take O(nlogn) time

Binary search will take O(logn) time

Here we are using BinarySearch() for n times to get index of each element of A, So it'll take O(nlogn) time.

Total time = O(nlogn) + O(nlogn) = O(nlogn)

output screenshot:

C:\Users\srini Desktop>java SortedPositions 1 3 0 2 C:\Users\srini\Desktop>

Add a comment
Know the answer?
Add Answer to:
the programming language is in java Problem 2 You are given an array A with n...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT