-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRearrangeArrayAlternately.c
More file actions
40 lines (40 loc) · 901 Bytes
/
RearrangeArrayAlternately.c
File metadata and controls
40 lines (40 loc) · 901 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
#include<stdio.h>
int main()
{
int t,n,i,j,max,min,k,temp;
scanf("%d",&t);
for(i=0;i<t;i++){
scanf("%d",&n);
int a[n];
for(j=0;j<n;j++){
scanf("%d",&a[j]);
}
for(j=0;j<n-1;j++){
if(j%2==0){
max=j;
for(k=j+1;k<n;k++){
if(a[k]>a[max]){
max=k;
}
}
temp=a[max];
a[max]=a[j];
a[j]=temp;
}
else{
min=j;
for(k=j+1;k<n;k++){
if(a[k]<a[min]){
min=k;
}
}
temp=a[min];
a[min]=a[j];
a[j]=temp;
}
}
for(j=0;j<n;j++){
printf("%d ",a[j]);
}
}
}