Answered step by step
Verified Expert Solution
Link Copied!

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 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;
}2021?? Algorithrs-Vayne \algs4" FindMedianTest
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 =4 Merlian =47210. Search time =1.411F-5 seconds
i) Array size -50 Median - $2485.0 Search tima -2.98E-5 s9conds
Array size =1000000, Medion =49901.0, Scarch -ime =0.1731655 scconds
Array size -2000000, Median -50011.0 Search -ine -0.102556379 seconds
Array si=49964.0=0.224727399=5000000,=49953.0,=0.3544674=1000001,=50031.0,=0.0698845=2000001,=50683.0,=0.1420275=300000 Median =49964.0 Search . ine =0.224727399 seconds
Array size =5000000, Median =49953.0, Search -ime =0.3544674 seconds
Array size =1000001, Median =50031.0, Search : ine =0.0698845 seconds
Array size =2000001, Median =50683.0, Search -ine =0.1420275 seconds
Array size
} import edu.princeton.cs.algs4.StdRandom;
import java.util.*;
public class FindMedianTest {
public static void main (String [] args) throws Throwable {
double result;
int[] size ={10,50,1000000,5000000};
// Sort and print a few 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; size=end; size+=inc){
int [] array = createRandomArray(size);
System.gc();
long startTime = System.nanoTime();
//Instructor's implementation of sort
FindMedian.findMedian(array); // You will call findMedian method from FindMedian
result = array[array.length/2];
long endTime = System.nanoTime();
System.out.println ("Array size ="+ size +
"\tMedian ="+ result +
"\tSearch time ="+
(endTime-startTime)/1000000000.0+" seconds");
}
}
// This method reads creates an array of random integers
public static int[] createRandomArray(int size) throws Throwable {
Random r = new Random(50);
int [] array = new int [size];
for (int i=0; i
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Domain Transfer Learning With 3q Data Processing

Authors: Ahmed Atif Hussain

1st Edition

B0CQS1NSHF, 979-8869061805

More Books

Students also viewed these Databases questions

Question

10-9 How have social technologies changed e-commerce?

Answered: 1 week ago