-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindTheMedian.java
More file actions
80 lines (51 loc) · 1.69 KB
/
FindTheMedian.java
File metadata and controls
80 lines (51 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*Find the median
Problem Statement
The goal is to read in a list of integers into an array and output the one which is the median, that is, the one which if you put them all in order would be in the middle. If there are two in the middle then take the average of those two. For example the median of 4, 8, 2, 3 and 5 is 4, because if you put them all in order 4 would be the middle value. The median of 2, 7, 4, and 9 is 5.5 because 5.5 is the average of 4 and 7.
Input Format
The first line contains N, the number of inputs. The second line contains N integers separated by a space.
Output Format
A double that represents the median.
Constraints
1<=N<=1000
-1000<=n<=1000
Sample Input
7
15 18 3 2 -5 6 2
Sample Output
3.0*/
import java.util.*;
import java.util.Arrays;
public class FindTheMedian {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int ile=sc.nextInt();
int[]tab=new int[ile];
for(int i=0;i<ile;i++)
{
tab[i]=sc.nextInt();
}
for(int outer=ile-1;outer>0;outer--)
{
for(int inner=0; inner<outer; inner++)
{ if(tab[inner+1]<tab[inner]){
swap4(inner,inner+1, tab);
}
}
}
System.out.println(Arrays.toString(tab));
int p2=ile%2;
if(p2==0)
{
double h=(tab[ile/2]+tab[ile/2-1])/2;
System.out.println(h);
}
else {System.out.println(tab[ile/2]);
}
}
public static void swap4(int x,int y, int[]tab)
{
int temp=tab[x];
tab[x]=tab[y];
tab[y]=temp;
}
}