0
Pls help me answer this for school purpose
Add a method called median() to the ArrayIns class in the insertSort.java program(Listing Program). This method should return the median value in the array. https://code.sololearn.com/cpL2QbDyqbB7/?ref=app
3 Answers
+ 2
+ 1
For your program
import static java.lang.Math.*;
public void MedianFinder() {
System.out.print("Consider the sequence: ");
insertionSort();
display();
System.out.println();
int totalElements =nElems; // a.length;
int mIdx = (int)Math.floor(totalElements/2); // ceil floor
double median;
if (totalElements % 2 == 0) {
System.out.println("Total number of elements: " + totalElements + " (even) \n");
median = (a[mIdx-1]+a[mIdx])/2;
} else {
System.out.println("Total number of elements: " + totalElements + " (odd) \n");
median = (a[mIdx]);
}
System.out.println("Median is : " + median);
}
arr.MedianFinder();
0
public class Program{
public static void main(String arg[]) {
int n=5;
double a[]=new double[n];
a[0]=10;
a[1]=20;
a[2]=30;
a[3]=40;
a[4]=50;
double m=0;
if(n%2==1){ m=a[(n+1)/2-1];}
else{m=(a[n/2-1]+a[n/2])/2;}
System.out.println("Median :"+m);
}}
https://code.sololearn.com/crV2pFviIuux/?ref=app