Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Why am i not getting the execpted output? Output in picture and code below: import java.util.Arrays; public class FindMedian { public static double findMedian (
Why am i not getting the execpted output? Output in picture and code below: 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;
AlgorithrsVayne algs FindMedianTest
Array size Median Search time seconds
Array size Median Search time seconds
Array size Median Search time seconds
Array size Merlian Search time F seconds
i Array size Median $ Search tima E sconds
Array size Medion Scarch ime scconds
Array size Median Search ine seconds
Array si Median Search ine seconds
Array size Median Search ime seconds
Array size Median Search : ine seconds
Array size Median Search ine seconds
Array size
import edu.princeton.csalgsStdRandom;
import java.util.;
public class FindMedianTest
public static void main String args throws Throwable
double result;
int size ;
Sort and print a few small arrays to make certain the sort works
SortandMediansize size;
Sort and time larger arrays
SortandMediansize size;
Sort and time larger arrays
SortandMediansize size;
public static void SortandMedianint start, int end, int inc throws Throwable
double result;
for int size start; sizeend; sizeinc
int array createRandomArraysize;
System.gc;
long startTime System.nanoTime;
Instructors implementation of sort
FindMedian.findMedianarray; You will call findMedian method from FindMedian
result arrayarraylength;
long endTime System.nanoTime;
System.out.println Array size size
tMedian result
tSearch time
endTimestartTime seconds";
This method reads creates an array of random integers
public static int createRandomArrayint size throws Throwable
Random r new Random;
int array new int size;
for int i; i
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started