-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem_3.c
74 lines (61 loc) · 1.55 KB
/
Problem_3.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
Alternate index sort in unsorted array(both ascending and descending)
( no extra space)
Sample Input 1
9
23 7 8 30 18 12 6 28 16
Sample Output 1
23 7 18 12 16 28 8 30 6
*/
#include <stdio.h>
void ascendingSort(int array[], int size)
{
int temp;
for(int index = 1; index < size - 1 ; index = index + 2)
{
for(int sortIndex = index + 2; sortIndex < size; sortIndex = sortIndex + 2)
{
if(array[index] > array[sortIndex])
{
temp = array[index];
array[index] = array[sortIndex];
array[sortIndex] = temp;
}
}
}
}
void descendingSort(int array[], int size)
{
int temp;
for(int index = 0; index < size - 1 ; index = index + 2)
{
for(int sortIndex = index + 2; sortIndex < size; sortIndex = sortIndex + 2)
{
if(array[index] < array[sortIndex])
{
temp = array[index];
array[index] = array[sortIndex];
array[sortIndex] = temp;
}
}
}
}
void main()
{
int inputArray[20], size;
system("cls");
printf("\nEnter the length of array : ");
scanf("%d",&size);
for(int index = 0; index < size; index++)
{
printf("Enter number[%d]",index + 1);
scanf("%d", &inputArray[index]);
}
ascendingSort(inputArray,size);
descendingSort(inputArray,size);
printf("\nSorted array\n");
for(int index = 0; index < size; index++)
{
printf("\t%d",inputArray[index]);
}
}