Question

Given a (Java) LinkedList , write a function that returns the k th minimum element from...

Given a (Java) LinkedList , write a function that returns the k th minimum element from the arraylist

^recursive method plz, psudocode is fine

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find my full working code (you can treat as pseudo code also) in Java using Min Heap.

import java.util.ArrayList;

public class MinHeapArrayList

{

   public static int kthMinimum(ArrayList<Integer> arr, int k)

   {

       int n = arr.size();

       BuildMinHeap(arr, n);

      

       int min = Integer.MIN_VALUE;

      

       // One by one extract an element from heap

       for (int i=1; i <=k; i++)

       {

           // Move current root to end

           int temp = arr.get(0);

           arr.set(0, arr.get(n-1));

           arr.set(n-1, temp);

           n = n-1;

           min = temp;

           // call max heapify on the reduced heap

           MinHeapify(arr, n, 0);

       }

      

       return min;

   }

  

  

   static void BuildMinHeap(ArrayList<Integer> arr, int n){

       // Build heap (rearrange array)

       for (int i = n / 2 - 1; i >= 0; i--)

           MinHeapify(arr, n, i);

   }

   // To heapify a subtree rooted with node i which is

   // an index in arr[]. n is size of heap

   static void MinHeapify(ArrayList<Integer> arr, int n, int i)

   {

       int smallest = i; // Initialize largest as root

       int l = 2*i + 1; // left = 2*i + 1

       int r = 2*i + 2; // right = 2*i + 2

       // If left child is larger than root

       if (l < n && arr.get(l) < arr.get(smallest))

           smallest = l;

       // If right child is larger than largest so far

       if (r < n && arr.get(r) < arr.get(smallest))

           smallest = r;

       // If largest is not root

       if (smallest != i)

       {

           int swap = arr.get(i);

           arr.set(i, arr.get(smallest));

           arr.set(smallest, swap);

           // Recursively heapify the affected sub-tree

           MinHeapify(arr, n, smallest);

       }

   }

   /* A utility function to print array of size n */

   public static void display(ArrayList<Integer> arr)

   {

       int n = arr.size();

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

           System.out.print(arr.get(i)+" ");

       System.out.println();

   }

   public static void main(String args[])

   {

       int arr[] = {15,13,9,5,12,8,7,4,0,6,2,1};

       ArrayList<Integer> list = new ArrayList<>();

       for(int x : arr)

           list.add(x);

       System.out.println("4th min: "+kthMinimum(list, 4));

      

   }

}

/*

Sample run:

4th min: 4

*/

Please let me know in case of any issue

Add a comment
Know the answer?
Add Answer to:
Given a (Java) LinkedList , write a function that returns the k th minimum element from...
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