TechArena Community how to find median of given number in java?

#1
26-11-2009
 Member Join Date: May 2009 Posts: 999
how to find median of given number in java?

Hi,
I am new to programming language. I recently started learning java language. Yesterday our sir has given us program like find median of given numbers in java?. I use various method but I unable to write that program. That's why I asked this question on this forum. So if you have any idea about this please help me.
#2
26-11-2009
 Member Join Date: Apr 2008 Posts: 1,948
Re: how to find median of given number in java?

Here I posted code for finding median, try to compile in your computer.

Code:
```
import java.util.Scanner;

public class arraytesting{

public static void main(String args[])
{

Scanner p=new Scanner(System.in);

System.out.print("Enter the numbers : ");

int s=p.nextInt();

int[] arr1=new int[s];

System.out.print(arr1.length+"indexes\n");

for(int p=0;p<size;p++){

System.out.print("enter an integer number: ");

int num=p.nextInt();

arr[p]=num;

}

int p;

for( p=0;p<size;++p){//for p<s

int count=0;

for (int t=0;t<=s;++t){

if(arr1[p]>arr1[p]){

count++;

}

}

if(count==arr1.length/2){

break;

}

System.out.print(arr1[p]);

}
}
}```
#3
26-11-2009
 Member Join Date: May 2008 Posts: 2,008
Re: how to find median of given number in java?

Here is simple code to find median. Try to understand each line.

Code:
```package net.sourceforge.jiu.util;

public class MedianNum
{

private MedianNum()
{
}

public static void swaping(int[] p, int s1, int s2)
{
int temp1 = p[si1];
p[s1] = p[s2];
p[s2] = temp1;
}

public static int find(int[] p, int from, int to)
{
int lowvalue = from;
int highvalu = to;
int med = (lowvalu + highvalu) / 2;
do
{
if (highvalu <= lowvalu)
{
return a[med];
}
if (highvalu == lowvalu + 1)
{
if (a[lowvalu] > a[highvalu])
{
swaping(p, lowvalu, highvalu);
}
return p[med];
}
int middle = (lowvalu + highvalu) / 2;
if (p[middle] > p[highvalu])
{
swaping(a, middle, highvalu);
}
if (p[lowvalu] > p[highvalu])
{
swaping(p, lowvalu, highvalu);
}
if (p[middle] > p[lowvalu])
{
swaping(p, middle, lowvalu);
}
swaping(p, middle, lowvalu + 1);
int lowl = lowvalu + 1;
int highh = highvalu;
do
{
do
{
lowl++;
}
while(a[lowvalu] > p[lowl]);
do
{
highh--;
}
while(a[highh] > p[lowvalu]);
if (highh < lowl)
{
break;
}
swaping(p, lowl, highh);
}
while(true);
swaping(p, lowvalu, highh);
if (highh <= median)
{
lowvalu = lowl;
}
if (highh >= med)
{
highvalu = highh - 1;
}
}
while(true);
}
}```
#4
26-11-2009
 Member Join Date: Apr 2008 Posts: 2,001
Re: how to find median of given number in java?

Here I am not writing any code to find median of given numbers rather I just given you logic behind it. Let's see how we have to find median of given number.If there are 7 numbers then the median will be at number 4 and if there are 8 numbers then also median will be at number 4.

Use following formula to get logic behind it:
MedianPosition = (nuberofitems+1) / 2

It means that if numberofitems is 5 then median will be 3.

 Tags: