Question: why am i not getting the expected output? Excpected: Array size = 1 0 Median = 6 6 0 5 1 . 0 Search time

why am i not getting the expected output? Excpected: Array size =10 Median =66051.0 Search time =4.233E-4 seconds
Array size =20 Median =74090.0 Search time =7.4E-6 seconds
Array size =30 Median =47986.0 Search time =9.601E-6 seconds
Array size =40 Median =47210.0 Search time =1.4101E-5 seconds
Array size =50 Median =62485.0 Search time =2.98E-5 seconds Actual: Array size =10 Median =72516.0 Search time =0.0023991 seconds
Array size =20 Median =50939.0 Search time =5.59E-5 seconds
Array size =30 Median =45477.0 Search time =6.75E-5 seconds
Array size =40 Median =56444.0 Search time =1.003E-4 seconds
Array size =50 Median =48757.0 Search time =1.534E-4 seconds Main Method in picture and here is the other java program: import edu.princeton.cs.algs4.StdRandon;
import java.util.*
double result:
double result; int[] size ={10,50,1000000,5000000};
// Sort and print a fea small' arrays to make certain the sort works
SortandMedian(size[0], size[1],10);
// Sort and time larger arrays
SortandMedian(size[2], size[3],1000000);
// Sort and time larger arrays
SortandMedian (size [2]+1, size [3]+1,1000000)
public static void SortandMedian(int start, int end, int inc) throws Throwable
{
double result; for (int size = start; sizec=end; size+=inc)}
int [] array = createRandomArray(size); Sygten.gc();
long startTine = Systen, nanoTine ();
//Instructor's implementation of sort
Arrays. sort(array); // You will call findledian method from FindMedian
chass
result = array[array, length/2]; long endTine = Systen. nanoTine();
Systen. out,printh ("Array size ="+ size +
"VeMedian ="+ result
"'enearch tine ="+
3.3
(endTime-startTime)/1000000000.0+" seconds"):
3/ This method reads creates an array of randon integers
pubtic static int[] createRandomArray(int size) throws Throwable {
Random r= new Randon(50);
Randott r= new Randon(50); int [] array = new int [size];
for (int i=0;i+4i=0;i import java.util.Arrays;
public class FindMedian {
public static double findMedian(int[] array){
int length = array.length;
Arrays.parallelSort(array);
if (length %2==1){
// If the array has odd length, return the middle element
return quickSelect(array, length /2);
} else {
// If the array has even length, return the average of the middle two elements
int middle1= quickSelect(array, length /2-1);
int middle2= quickSelect(array, length /2);
return (middle1+ middle2)/2.0;
}
}
private static int quickSelect(int[] array, int k){
int low =0;
int high = array.length -1;
while (low high){
int partitionIndex = partition(array, low, high);
if (k == partitionIndex){
return array[k];
} else if (k partitionIndex){
high = partitionIndex -1;
} else {
low = partitionIndex +1;
}
}
return array[low];
}
private static int partition(int[] array, int low, int high){
int pivot = array[high];
int i = low -1;
for (int j = low; j high; j++){
if (array[j]= pivot){
i++;
swap(array, i, j);
}
}
swap(array, i +1, high);
return i +1;
}
private static void swap(int[] array, int i, int j){
int temp = array[i];
array[i]= array[j];
array[j]= temp;
}
}
 why am i not getting the expected output? Excpected: Array size

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!