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 Median Search time E seconds
Array size Median Search time E seconds
Array size Median Search time E seconds
Array size Median Search time E seconds
Array size Median Search time E seconds Actual: Array size Median Search time seconds
Array size Median Search time E seconds
Array size Median Search time E seconds
Array size Median Search time E seconds
Array size Median Search time E seconds Main Method in picture and here is the other java program: import edu.princeton.csalgsStdRandon;
import java.util.
double result:
double result; int size ;
Sort and print a fea small' arrays to make certain the sort works
SortandMediansize size;
Sort and time larger arrays
SortandMediansize size;
Sort and time larger arrays
SortandMedian size size
public static void SortandMedianint start, int end, int inc throws Throwable
double result; for int size start; sizecend; sizeinc
int array createRandomArraysize; Sygten.gc;
long startTine Systen, nanoTine ;
Instructors implementation of sort
Arrays. sortarray; You will call findledian method from FindMedian
chass
result arrayarray length; long endTine Systen. nanoTine;
Systen. out,printh Array size size
"VeMedian result
enearch tine
endTimestartTime seconds":
This method reads creates an array of randon integers
pubtic static int createRandomArrayint size throws Throwable
Random new Randon;
Randott new Randon; int array new int size;
for int ;;i import java.util.Arrays;
public class FindMedian
public static double findMedianint array
int length array.length;
Arrays.parallelSortarray;
if length
If the array has odd length, return the middle element
return quickSelectarray length ;
else
If the array has even length, return the average of the middle two elements
int middle quickSelectarray length ;
int middle quickSelectarray length ;
return middle middle;
private static int quickSelectint array, int k
int low ;
int high array.length ;
while low high
int partitionIndex partitionarray low, high;
if k partitionIndex
return arrayk;
else if k partitionIndex
high partitionIndex ;
else
low partitionIndex ;
return arraylow;
private static int partitionint array, int low, int high
int pivot arrayhigh;
int i low ;
for int j low; j high; j
if arrayj pivot
i;
swaparray i j;
swaparray i high;
return i ;
private static void swapint array, int i int j
int temp arrayi;
arrayi arrayj;
arrayj temp;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
