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...
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...
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...
sing arrays please write a program to implement the STACK concept after creating the Array, the...
sing arrays please write a program to implement the STACK concept after creating the Array, the user is to be presented with a menu to choose from a number of options such as pop, push, top, etc... elements to be added on the stack are ints between 0 and 99 include a loop to re display the options (menu) and an outer loop to restart the program Write a C++ program to implement the Stacks concept. the stack implementation is...
USE VISUAL STUDIO/VB In this assignment, you will create an array of ten elements, one for...
USE VISUAL STUDIO/VB In this assignment, you will create an array of ten elements, one for name, one for hours worked,and the last for hourly rate. The arrays will receive the information from inputs from the screen.For example, you will ask the following three questions: a) Employee name: b) Hours worked: c) Hourly rate: After you have received all ten records and have inserted them into the array, you will then calculate the hourly rate times the hours worked to...
implement in LEGV8 find the smallest value in an array.
implement in LEGV8 find the smallest value in an array.
For the arrays x and y given below, use MATLAB to find all the elements in x that are greater than the corresponding elements in y.
For the arrays x and y given below, use MATLAB to find all the elements in x that are greater than the corresponding elements in y.x = [-3, 0, 0, 2, 6, 8] y = [-5, -2, 0, 3, 4, 10]
How to create a two-dimensional array, initializing elements in the array and access an element in...
How to create a two-dimensional array, initializing elements in the array and access an element in the array using PHP, C# and Python? Provide code examples for each of these programming languages. [10pt] PHP C# Python Create a two-dimensional array Initializing elements in the array Access an element in the array
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT