-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathSwapping.c
56 lines (47 loc) · 950 Bytes
/
Swapping.c
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
#include <stdio.h>
#include <stdlib.h>
void swap(int *x, int *y)
{
int temp = *y;
*y = *x;
*x = temp;
}
int main()
{
int *arr,n, min, max;
scanf("%d", &n);
arr=(int*)malloc(n*sizeof(int));
for(int i =0;i<n;i++){
scanf("%d",&arr[i]);
}
for (int i = 0; i < n; i++)
{
if (i % 2 == 0)
{
min = i;
for (int j = i; j < n; j++)
{
if (arr[j] < arr[min])
{
min = j;
}
}
swap(&arr[i], &arr[min]);
}
else{
max=i;
for (int j = i; j < n; j++)
{
if (arr[j] > arr[max])
{
max = j;
}
}
swap(&arr[i], &arr[max]);
}
}
for( int i =0;i<n;i++){
printf("%d ",arr[i]);
}
return 0;
}