Question

In: Computer Science

Use quickselect algorithm to partition an array into 3 subsets: values less than pivot, values equal...

Use quickselect algorithm to partition an array into 3 subsets: values less than pivot, values equal to pivot, and values greater than pivot. The pivot must be a random element of the array. Find S1(the number of elements less than the pivot), S2 (the number of elements equal to the pivot), and S3 (the number of elements greater than the pivot). Your code must use only one array and must be able to handle duplicate array elements. Use Java.

Solutions

Expert Solution

import java.util.Arrays;

class quickselect  

{

    public static int partition (int[] arr,int low, int high)

    {

        int pivot = arr[high], loc = low;

        for (int i = low; i <= high; i++)

        {

            if(arr[i] < pivot)

            {

                int a = arr[i];

                arr[i] = arr[loc];

                arr[loc] = a;

                loc++;

            }

        }

        int a = arr[high];

        arr[high] = arr[loc];

        arr[loc] = a;

        return loc;

    }

    public static int Smallest(int[] arr, int low,int high, int k)

    {

        int partition = partition(arr,low,high);

        if(partition == k)

            return arr[partition];    

        else if(partition < k )

            return Smallest(arr, partition + 1, high, k );

        else

            return Smallest(arr, low, partition-1, k );         

    }

    public static void main(String[] args)

    {

      int b,i;

        Scanner s = new Scanner(System.in);   
     System.out.print("Enter no. of elements you want in array:"); 
       b= s.nextInt();      
       int array[] = new int[b];       
       System.out.println("Enter all the elements:");    
       for(i = 0; i < b; i++)  
     {                
        array[i] = s.nextInt();

}   

        int kPosition = 3;

        int length = array.length;

         

        if(kPosition > length)

        {

            System.out.println("Exception Index out of bound");

        }

        else

        {

            System.out.println("smallest element in array : " +

                                kthSmallest(array, 0, length - 1,

                                                          kPosition - 1));

        }

    }

}


Related Solutions

Following is the algorithm of Quicksort for sorting an array of integers in ascending order. Partition(numbers,...
Following is the algorithm of Quicksort for sorting an array of integers in ascending order. Partition(numbers, lowIndex, highIndex) {    midpoint = lowIndex + (highIndex - lowIndex) / 2    pivot = numbers[midpoint]    done = false    while (!done) {       while (numbers[lowIndex] < pivot)          lowIndex++       while (pivot < numbers[highIndex])          highIndex--       if (lowIndex >= highIndex) {          done = true       }       else {          temp = numbers[lowIndex]          numbers[lowIndex] = numbers[highIndex]          numbers[highIndex] = temp                 lowIndex++          highIndex--       }    }    return highIndex } Quicksort(numbers, lowIndex, highIndex) {    if (lowIndex...
prove that 2/pi is less than or equal to (sinx)/x which is less than or equal...
prove that 2/pi is less than or equal to (sinx)/x which is less than or equal to 1. for x is in (0,pi/2]
For each of the following, fill in the blanks with "Less than", "Greater than", or "Equal...
For each of the following, fill in the blanks with "Less than", "Greater than", or "Equal to" *) A gas flows through a one-inlet, one-exit control volume operating at steady state with no internal irreversibilities, Qcv = 0. Heat transfer at a rate Qcv takes place only at a location on the boundary where the temperature is Tb. The specific entropy of the gas at the exit is _____ than the specific entropy of the gas at the inlet. *)...
A. Let f(x) be: x+(12/(1-x)) if x is less than or equal to -3 x+3 if...
A. Let f(x) be: x+(12/(1-x)) if x is less than or equal to -3 x+3 if -3 < x and x is less than or equal to zero ln(x) if 0< x is less than equal to 5 (x-5)/5+ln5 if x>5 answer and explain these answers B. Is f differentialble at x=5? justify your answer by evaluating the one sided limits of lim as x approches 5 (f(x)-f(5))/x-5
Let V be the 3-dimensional vector space of all polynomials of order less than or equal...
Let V be the 3-dimensional vector space of all polynomials of order less than or equal to 2 with real coefficients. (a) Show that the function B: V ×V →R given by B(f,g) = f(−1)g(−1) + f(0)g(0) + f(1)g(1) is an inner product and write out its Gram matrix with respect to the basis (1,t,t2). DO NOT COPY YOUR SOLUTION FROM OTHER SOLUTIONS
use quick sort to sort the following array. show each pass and what the pivot is...
use quick sort to sort the following array. show each pass and what the pivot is during the pass. please explain why you are swapping positions. do not use a compiler. 30 5 40 11 20 9 15 2 60 25 80 3 73 35 4 75 20 6
1. Read 20 integers into an array. Next, use the unique algorithm to reduce the array...
1. Read 20 integers into an array. Next, use the unique algorithm to reduce the array to the unique values entered by the user. Use the copy algorithm to display the unique values. 2. Modify the Exercise 1 above to use the unique_copy algorith. The unique values should be inserted into a vector that's initially empty. Use a back_inserter to enable the vector to grow as new items are added. Use the copy algorithm to display the unique values.
If we are testing the null hypothesis that the mean is less than or equal to...
If we are testing the null hypothesis that the mean is less than or equal to 100, and the critical value for the test is determined to be z = 1. 645, then the rejection region would be all of the z values that are ____________ 1. 645. Multiple Choice greater than less than greater than or equal to less than -1.645 or greater than +1.645
For each, indicate whether the first item is (greater than, equal to, less than) the second...
For each, indicate whether the first item is (greater than, equal to, less than) the second item oxygen content in the pulmonary veins      ___________       oxygen content in the carotid arteries Answer 1Choose...greater thanequal toless than maximum pressure in the aorta ___________     maximum pressure in the left atrium   Answer 2Choose...greater thanequal toless than blood flow through the lungs ____________ blood flow through the kidneys Answer 3Choose...greater thanequal toless than flow of blood through a dilated vessel _____________ flow of blood through...
why is the median less affected by the extreme values than the mean?
why is the median less affected by the extreme values than the mean?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT