Question

In: Computer Science

Use quickselect algorithm to implement the two algorithms for finding the kth smallest integer in a...

Use quickselect algorithm to implement the two algorithms for finding the kth smallest integer in a set of integers.

MUST USE JAVA AND QUICKSELECT.

Algorithm 1:

Procedure SELECT( k,S)

{ if ISI =1 then return the single element in S

   else { choose an element randomly from S;

              let S1, S2, and S3 be the sequences of elements in S  

              less than, equal to, and greater than m, respectively;

             if IS1I >=k then return SELECT(k,S1)

              else

                   if (IS1I + IS2I >=k) then return m

                   else return SELECT(k-IS1I-IS2I , S3);

             }

}

Algorithm 2:

Procedure SELECT(k,S)

{if ISI<50 then { sort S; return the kth smallest element;}

else

   { divide S into ISI/5 sequences of 5 elements each with up

     to four leftover elements;

    sort each 5-element sequence;

    let M be the sequence of the medians of the 5-element sets;

    let m= SELECT( │M│/2, M);  

    let S1,S2,and S3 be the sequences of elements in S  

   less than, equal to, and greater than m, respectively;

    if IS1I >=k then return SELECT(k,S1)

              else

                   if (IS1I + IS2I >=k then return m

                   else return SELECT(k-IS1I-IS2I , S3);

}

Solutions

Expert Solution

PROGRAM :

File: FindingKthSmallest.java

// FindingKthSmallest class implementation
import java.util.Random;
import java.util.Scanner;
public class FindingKthSmallest
{
private static Random rand = new Random();
private static Scanner keyboard = new Scanner(System.in);

// implementation of Algorithm #1
public static int select1(int k, int[] S)
{
if (S.length == 1)
return S[0];

int m = S[rand.nextInt(S.length)];
int[] S1 = new int[S.length];
int[] S2 = new int[S.length];
int[] S3 = new int[S.length];
int p = 0;
int q = 0;
int r = 0;

for (int i = 0; i < S.length; i++)
{
if (S[i] < m)
{
S1[p] = S[i];
p++;
}
else if (S[i] == m)
{
S2[q] = S[i];
q++;
}
else
{
S3[r] = S[i];
r++;
}
}

S1 = trimToSize(S1, p);
S2 = trimToSize(S2, q);
S3 = trimToSize(S3, r);

if (S1.length >= k)
return select1(k, S1);
else if (S1.length + S2.length >= k)
return m;
else
return select1(k - S1.length - S2.length, S3);
}

// implementation of Algorithm #2
public static int select2(int k, int[] S)
{
if (S.length < 50)
{
sort(S, 0, S.length - 1);
return S[k - 1];
}

for (int i = 0; i < S.length; i += 5)
{
if (i + 4 < S.length)
sort(S, i, i + 4);
else
sort(S, i, S.length - 1);
}
  
int[] M = new int[(int)Math.ceil(S.length / 5.0)];
  
for (int i = 0, j = 0; i < S.length; i += 5, j++)
{
if (i + 4 < S.length)
M[j] = S[(2 * i + 4) / 2];
else
M[j] = S[(i + S.length - 1) / 2];
}

int m = select2(M.length / 2 + 1, M);
int[] S1 = new int[S.length];
int[] S2 = new int[S.length];
int[] S3 = new int[S.length];
int p = 0;
int q = 0;
int r = 0;

for (int i = 0; i < S.length; i++)
{
if (S[i] < m)
{
S1[p] = S[i];
p++;
}
else if (S[i] == m)
{
S2[q] = S[i];
q++;
}
else
{
S3[r] = S[i];
r++;
}
}

S1 = trimToSize(S1, p);
S2 = trimToSize(S2, q);
S3 = trimToSize(S3, r);

if (S1.length >= k)
return select2(k, S1);
else if (S1.length + S2.length >= k)
return m;
else
return select2(k - S1.length - S2.length, S3);
}
  
// trimToSize helper method
private static int[] trimToSize(int[] arr, int size)
{
int[] temp = new int[size];
for (int i = 0; i < size; i++)
{
temp[i] = arr[i];
}
return temp;
}

// sort helper method
private static void sort(int arr[], int begin, int end)
{
if (begin < end)
{
int pivot = arr[end];
int i = (begin - 1);

for (int j = begin; j < end; j++)
{
if (arr[j] <= pivot)
{
i++;

int swapTemp = arr[i];
arr[i] = arr[j];
arr[j] = swapTemp;
}
}

int swapTemp = arr[i + 1];
arr[i + 1] = arr[end];
arr[end] = swapTemp;

int partitionIndex = i + 1;

sort(arr, begin, partitionIndex - 1);
sort(arr, partitionIndex + 1, end);
}
}
  
// printArray method to print the array of integers
public static void printArray(int[] S)
{
for(int i = 0; i < S.length; i++)
{
System.out.print(S[i] + "\t");
if(i % 10 == 9)
System.out.println();
}
}

// start main method
public static void main(String[] args)
{
System.out.print("Enter the size of an array: ");
int size = keyboard.nextInt();
while(size < 1)
{
System.out.println("Size of the array should be greater than 0.");
System.out.print("Enter the size of an array: ");
size = keyboard.nextInt();
}
  
System.out.print("Enter the value of k: ");
int k = keyboard.nextInt();
while(k < 1 || k > size)
{
System.out.println("Value of k should be in the range 1-" + size + ".");
System.out.print("Enter the value of k: ");
k = keyboard.nextInt();
}
  
// fill the array with the random integers in the range [100, 999]
int[] S = new int[size];
for(int i = 0; i < size; i++)
{
S[i] = 100 + rand.nextInt(900);
}
  
System.out.println("\nRandom values generated in the array:");
printArray(S);
System.out.println();
  
System.out.println(k + "th smallest value in the array using Algorithm #1: " + select1(k, S));
System.out.println(k + "th smallest value in the array using Algorithm #2: " + select2(k, S));

} // end of main method
} // end of FindingKthSmallest class

Sample Output:


Related Solutions

Analyzing Selection Sort Algorithm The selection sort algorithm works by first finding the smallest value in...
Analyzing Selection Sort Algorithm The selection sort algorithm works by first finding the smallest value in a list and swapping the first value with it, then finding the second smallest value and swapping the second value with it, continuing until all the values are in order. Implement this algorithm, then determine its growth function, and hence the order of the algorithm. Find how many swaps are made. Use Java Code to create algorithm
need a code MIPS assembly language program to implement algorithms of an 8-bit integer "positive integer"...
need a code MIPS assembly language program to implement algorithms of an 8-bit integer "positive integer" to calculate the square root for an-8 bit integer using The Radix-2 SRT-Redundant and Non-Redundant Algorithm to approximate square root
Write two algorithms to find both the smallest and largest numbers in a list of n...
Write two algorithms to find both the smallest and largest numbers in a list of n numbers. In first algorithm, you simply write one loop to find the minimum and maximum. So there will be 2(n - 1) comparisons. In second algorithm, you try to find a method that does at most 1.5n comparisons of array items. Determine the largest list size (i.e., n) that Algorithm 1 can process and still compute the answer within 60 seconds. Report how long...
Consider the following algorithm to find the kth largest elementof a given array A of...
Consider the following algorithm to find the kth largest element of a given array A of n numbers. We pick every fifth element of A and place them in the array B. We find the median of B recursively, and use this median of B as a pivot to partition the array A. Depending on k and the number of elements that are smaller than the chosen pivot, we recurse into an appropriate subproblem of A.Answer the following questions.Write the...
1)Think of a better way to enhance Fibonacci recursive algorithm,,, report your finding. 2) Implement a...
1)Think of a better way to enhance Fibonacci recursive algorithm,,, report your finding. 2) Implement a recursive function to print an array from the middle and from left to right. 3) Trace tower of Hanoi recursive algorithm for 4 discs.
in C++ Description: For a given integer n > 1, the smallest integer d > 1...
in C++ Description: For a given integer n > 1, the smallest integer d > 1 that divides n is a prime factor. We can find the prime factorization of n if we find d and then replace n by the quotient of n divided by d, repeating this until n becomes 1.             Write a program that determines the prime factorization of n in this manner, but that displays the prime factors in descending order. (When you find a...
implement in LEGV8 find the smallest value in an array.
implement in LEGV8 find the smallest value in an array.
C++ Consider the following algorithm for finding the distance between the two closest elements in an...
C++ Consider the following algorithm for finding the distance between the two closest elements in an array of numbers. ALGORITHM MinDistance(A[0..n − 1]) //Input: Array A[0..n − 1] of numbers //Output: Minimum distance between two of its elements dmin←∞ for i ←0 to n − 1 do for j ←0 to n − 1 do if i ≠ j and |A[i]− A[j ]| < dmin dmin ←|A[i]− A[j ]| return dmin Make as many improvements as you can in this...
Provide and implement three completely different algorithms of different running time that will check if two...
Provide and implement three completely different algorithms of different running time that will check if two strings are anagrams.
Describe an optimal algorithm for finding the integers common to two lists of n integers each....
Describe an optimal algorithm for finding the integers common to two lists of n integers each. Evaluate how long each step in your algorithm takes using Θ-notation.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT