Question

In: Computer Science

Create an array of 10,000 elements, use sorted, near sorted, and unsorted arrays. Implement find the...

Create an array of 10,000 elements, use sorted, near sorted, and unsorted arrays. Implement find the kth smallest item in an array. Use the first item as the pivot. Compare sets of results using a static call counter. Reset counter before running another search. Create a Test drive to exhaustively test the program.

// Assume all values in S are unique.

kSmall(int [] S, int k): int (value of k-smallest element)

pivot = arbitrary element from S:  let’s use the first element.

S1 = < all elements from S less than pivot > // these two steps will require some thought

S2 = < all elements from S greater than pivot > //  and need to be implemented

if k <= S1.length

    return kSmall(S1, k)

else if k == S1.length + 1

    return pivot

else

    return kSmall(S2, k – S1.length – 1)

Solutions

Expert Solution

please give thumbs up, thanks

CODE:

/**
*
* @author VISHAL
*/
public class KthSmallest {
public static int kSmall(int[]S,int k)
{
int[] S1;
int []S2;
int greater=0;
int lesser=0;
int pivot=S[0];
for(int i=0; i<S.length;i++)
{
if(S[i]<pivot)
lesser++;
else if(S[i]>pivot)
greater++;
}
S1=new int[lesser];
S2=new int[greater];
int m=0,n=0;
for(int i=0; i<S.length; i++)
{
if(S[i]<pivot)
S1[m++]=S[i];
else if(S[i]>pivot)
S2[n++]=S[i];
}
if(k<=S1.length)
return kSmall(S1,k);
else if( k==S1.length+1)
return pivot;
else
return kSmall(S2,k-S1.length-1);
}
public static void main(String[]args)
{
int A[]={2,5,3,7,6,8,10,76,45};
System.out.println(kSmall(A,5));
}
}


Related Solutions

Problem 1: Unsorted arrays Given an array of integers that is unsorted, implement the following functions:...
Problem 1: Unsorted arrays Given an array of integers that is unsorted, implement the following functions: • myAdd ( ): add an integer d to the array; return 0 if the operation is successful; return a negative number otherwise. • search ( ): given an integer d, if d is found in the array, return the index of the cell containing d. Return a negative number otherwise (e.g., d is not found in the array). • myRemove ( ): Step...
Implement a Priority Queue (PQ) using an UNSORTED LIST. Use an array size of 10 elements....
Implement a Priority Queue (PQ) using an UNSORTED LIST. Use an array size of 10 elements. Use a circular array: Next index after last index is 0. Add the new node to next available index in the array. When you add an element, add 1 to index (hit max index, go to index 0). Test if array in full before you add. When you remove an element, from the list, move the following elements to the left to fill in...
Please fill in the 20 blanks below: 1, Considering using sorted and unsorted array as priority...
Please fill in the 20 blanks below: 1, Considering using sorted and unsorted array as priority queue to do sorting. For an input array, we insert each element to a PQ and remove them out to an output array. Please write the arrays below: Input array: 2,7,5,3 Sorted PQ after 1st Insertion: Sorted PQ after 2nd Insertion: Sorted PQ after 3rd Insertion: Sorted PQ after 4th Insertion: Output array after 1st removeMin: Output array after 2nd removeMin: Output array after...
C++ Program 1–Implement a Priority Queue(PQ) using an UNSORTED LIST. Use an array size of 10...
C++ Program 1–Implement a Priority Queue(PQ) using an UNSORTED LIST. Use an array size of 10 elements. Use a circular array: Next index after last index is 0. Add the new node to next available index in the array. When you add an element, add 1 to index (hit max index, go to index 0). Test if array in full before you add. When you remove an element, from the list, move the following elements to the left to fill...
Overlapping Arrays (C++) An array overlaps another array if all elements of the latter array exist...
Overlapping Arrays (C++) An array overlaps another array if all elements of the latter array exist in the former array. They need not necessarily be in the same order. For example, [1,7,3,4,2] overlaps [1,2,3] because 1,2 and 3 exist in [1,7,3,4,2]. To make the implementation easy, [1,7,3,4,2] overlaps [1,1,2,3] as well. We don’t need to check whether 1 appears twice in the first array. Write a program that lets the user enter two arrays and displays whether the first array...
Array with Pointers Find Continuous Sub-Array C++ Problem: Given an unsorted array A of size N...
Array with Pointers Find Continuous Sub-Array C++ Problem: Given an unsorted array A of size N of non-negative integers, find a continuous sub-array which adds to the given number. Declare dynamic arrays and use only pointers syntax (no [ ]’s or (ptr+i) stuff.     Input will be the number of input values to enter followed by the sum to compare with. Print out the continuous sub-array of values that are equal to sum or the message ‘No sum found’. There...
JAVA: Implement a Queue ADT using a circular array with 5 string elements. Create a Queue...
JAVA: Implement a Queue ADT using a circular array with 5 string elements. Create a Queue object and try various operations below. Queue myQueue = new Queue(); myQueue.enqueue(“CPS 123”); myQueue.enqueue(“CPS 223”); myQueue.enqueue(“CPS 323”); myQueue.dequeue(); myQueue.enqueue(“CPS 113”); myQueue.enqueue(“CPS 153”); string course = myQueue.front(); // course should be CPS 223 size = myQueue.size(); // size should be 4 // output course and size
Given an unsorted array A of size N of integers, find a continuous sub-array which adds...
Given an unsorted array A of size N of integers, find a continuous sub-array which adds to the given number. Declare dynamic arrays and use only pointers syntax. (no [ ]'s or (ptr+1) stuff. input will be the number of input values to enter followed by the sum to compare with. print out the continuous sub-array of values that are equal to sum or the message 'no sum ofund.' there may be more than one sub-array to be found in...
Find the K'th smallest element in an unsorted array of integers. Find the K'th largest element...
Find the K'th smallest element in an unsorted array of integers. Find the K'th largest element in an unsorted array of integers. please make two separate methods in java
In this task you will implement a C++ function with arguments: a sorted integer array, size...
In this task you will implement a C++ function with arguments: a sorted integer array, size of the array, and a target integer value. Find all combinations of two elements in the sorted array which sum up to the target value. When found, add the combinations into an array, and print it. Where there is greater than one combination, you may use the number 200 as a separator for the combinations in the output array. Do not use more than...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT