Question

Implement Quick Select using the “Median of Medians in java in order to find the median.

Implement Quick Select using the “Median of Medians in java in order to find the median.

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

//Following is the required Java program. The array used to demonstrate is 8,5,3,4,5,1,3,2,6,8,6 with k = 5

import java.util.Arrays;

public class Main {

    public static void main(String[] args) {
        int[] arr = {8,5,3,4,5,1,3,2,6,8,6};
        int k=5;
        System.out.println(kth_smallest_iteratively(arr,k,0,arr.length-1));
    }

    //returns median of 5 or less elements, uses in-place bubble sort/very well can use other in-place sort
    public static int med5(int[] arr, int start, int end){
        if(start==end)return arr[start];
        //sort elements
        for(int i=end;i>start;i--){
            for(int j=start+1;j<=end;j++){
                if(arr[j-1]>arr[j])
                    swap(arr,j,j-1);
            }
        }
        //return median
        return arr[start+((end-start+1)>>1)];
    }

    //get value for median of median
    public static int getMedianOfMedian(int[] arr, int start, int end){
        int n=end-start+1;
        if(n<=5) return med5(arr,start,end);//less than 5 elements
        int numOfGroups = n/5 + (n%5==0?0:1);

        //below medians array is allocated on each recurrsive call
        //can be minimized by passing it from client and reusing
        int[] medians = new int[numOfGroups];//one median for each subgroup

        for(int i=0;i<numOfGroups;i++){
            int groupStart=i*5;
            int groupEnd = Math.min(arr.length-1, i*5+5-1);//last group can have less than 5 elements
            medians[i]=med5(arr,groupStart,groupEnd);
        }
        return getMedianOfMedian(medians,0,medians.length-1);
    }

    //partition finds an pivot and put at its final place
    private static int partition_lamuto_stable(int[] arr, int start, int end){
        int pivot = getMedianOfMedian(arr,start,end);
        int higher=start;//starting index of higher elements than pivot
        int lower=start;//start index of lower elements than pivot
        int pivotIndex=start;//starting index of elemetns equal to pivot(dup pivot)
        int[] copy= new int[end-start+1];
        for(int i=start,k=0;i<=end;i++){
            if(arr[i]<=pivot)higher++;//count all smaller elements and pivot elements
            if(arr[i]<pivot)pivotIndex++;//count all smaller elements
            copy[k++]=arr[i];//create copy
        }

        for(int i=0;i<copy.length;i++){
            int index;
            if(copy[i]<pivot) index=lower++;
            else if(copy[i]==pivot) index=pivotIndex++;
            else index=higher++;
            arr[index]=copy[i];
        }

        return lower;//index of pivot
    }

    public static int kth_smallest_iteratively(int[] arr, int k, int start, int end){
        int pivotIndex=-1;
        while(true){
            pivotIndex=partition_lamuto_stable(arr, start,end);
            if(pivotIndex+1==k)return arr[pivotIndex];
            else if(pivotIndex+1<k) start=pivotIndex+1;
            else end= pivotIndex-1;
        }
    }

    private static void swap(int[] arr, int index1, int index2){
        int temp=arr[index1];
        arr[index1]=arr[index2];
        arr[index2]=temp;
    }
}

Please refer to the screenshot of the code to understand the indentation of the code

import java.util.Arrays; 1. 2 3. 4 5. 6 public class Main { public static void main(String[] args) { int[] arr = {8,5,3,4,5,1

for(int i=0;i<numofGroups;i++) { int groupStart=i*5; int groupEnd = Math.min(arr. length-1, i*5+5-1);//last group can have le

Output:

4 ...Program finished with exit code 0 Press ENTER to exit console.]

Hope this helped. Please do upvote and if there are any queries please ask in comments section.

Add a comment
Know the answer?
Add Answer to:
Implement Quick Select using the “Median of Medians in java in order to find the median.
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