forked from yuvrajjwala/-sumOfArray
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPendulumWiseSortingofanArray.java
More file actions
83 lines (81 loc) · 1023 Bytes
/
PendulumWiseSortingofanArray.java
File metadata and controls
83 lines (81 loc) · 1023 Bytes
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
80
81
82
83
import java.util.*;
class pendulum
{
int a[];
int n;
pendulum()
{
n=0;
}
void accept()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the limit");
n=sc.nextInt();
a=new int[n];
System.out.println("enter the values");
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
}
void sort()
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(a[j]>a[j + 1])
{
int t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
}
void check()
{
int r[]=new int[n];
int f=(n/2)+1;
int l=(n/2)-1;
r[n/2]=a[n-1];
int k=0;
for(int i=n-2;i>=0;i--)
{
if(k==0)
{
r[f]=a[i];
f++;
}
else
{
r[l]=a[i];
l--;
}
k=-k+1;
}
for(int i=0;i<n;i++)
{
System.out.print(r[i]+ "\t");
}
}
void disp()
{
if(n%2!=0)
{
sort();
check();
}
else
{
System.out.println("wrong input");
}
}
public static void main(String[]args)
{
pendulum ob=new pendulum();
ob.accept();
ob.disp();
}
}