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

DIVIDE AND CONQUER Implement the two algorithms for finding the kth smallest integer in a set...
DIVIDE AND CONQUER Implement the two algorithms for finding the kth smallest integer in a set of integers using only one array that contains all the integers. Test your programs and compute the CPU time for different sets of integers generated by a random number generator. Must sure have the CPU time.
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
I need to find the kth smallest element in the union of 2 sorted arrays in...
I need to find the kth smallest element in the union of 2 sorted arrays in O(log |A| + log |B|) time. I understand this is similar to binary search, but I don't understand which parts of the arrays I can throw out at each level of recursion, and why. In the pseudocode below, what should go into $1 through $16 and why? // A and B are each sorted into ascending order, and 0 <= k < |A|+|B| //...
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...
Design and implement an algorithm that gets as input a list of k integer values N1,...
Design and implement an algorithm that gets as input a list of k integer values N1, N2, …., Nk, as well as a special value SUM. Your algorithm must locate a pair of values in the list N that sum to the value SUM. For example, If your list of values is 3, 8, 13, 2, 17, 18, 10, and the value of SUM is 20, then your algorithm would output either of the two values (2, 18) or (3,...
1. Implement the faster algorithm for integer multiplication in C++ as a function, called “multiply”, that...
1. Implement the faster algorithm for integer multiplication in C++ as a function, called “multiply”, that takes 2 unsigned integers and returns their multiplication as an unsigned integer. 2. Test your code in main. Hints: Represent integers as strings. Write a utility function that pads integers with zeros, this will be useful If the 2 integers differ in length In calculating ??10^? and (??+??) 10^(?/2)
design two algorithms, an iterative version and a recursive version , for finding the min and...
design two algorithms, an iterative version and a recursive version , for finding the min and max values in an array of n numbers A.) a iterative version B.) a recursive version derive the time efficiency functions (or time function in terms of n) for each, analyze each function, and compare and discuss the results of the analysis
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...
Problem 1: Describe using pseudocode and implement an efficient algorithm for finding the ten largest elements...
Problem 1: Describe using pseudocode and implement an efficient algorithm for finding the ten largest elements in an array of size n. What is the running time of your algorithm? Problem 2: An array A contains n-1 unique integersin the range [0, n-1]. There is one number from this range that is not in A. Design a O(n) algorithm for finding that number. Describe the algorithm in pseudocode. Implement the algorithm in Java. Hint : Consider computing a function of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT