forked from Vishal-Aggarwal0305/DSA-CODE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sorted_OddEvenArray.c
85 lines (77 loc) · 1.75 KB
/
Sorted_OddEvenArray.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
75
76
77
78
79
80
81
82
83
84
85
#include <stdio.h>
void main()
{
int array[100], odd[100], even[100], i, num, swap = 0;
printf("Enter the size of an array \n");
scanf("%d", &num);
printf("Enter the elements of the array \n");
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
int k = 0;
for (i = 0; i < num; i++)
{
if (array[i] % 2 == 0)
{
even[k] = array[i];
k++;
}
}
int l = 0;
for (i = 0; i < num; i++)
{
if (array[i] % 2 != 0)
{
odd[l] = array[i];
l++;
}
}
//odd and even array separated
// printf("odd unsorted array is\n");
// for (i = 0; i < l; i++)
// {
// printf("%d \t", odd[i]);
// }
// printf("\neven unsorted array is\n");
// for (i = 0; i < k; i++)
// {
// printf("%d \t", even[i]);
// }
//-------------------sorting odd array----------------------------
for (i = 0; i < l; i++)
{
for (int j = i + 1; j < l; j++)
{
if (odd[j] < odd[i])
{
swap = odd[j];
odd[j] = odd[i];
odd[i] = swap;
}
}
}
//-------------------sorting even array----------------------------
for (i = 0; i < k; i++)
{
for (int j = i + 1; j < k; j++)
{
if (even[j] < even[i])
{
swap = even[j];
even[j] = even[i];
even[i] = swap;
}
}
}
printf("\nodd sorted array is\n");
for (i = 0; i < l; i++)
{
printf("%d \t", odd[i]);
}
printf("\neven sorted array is\n");
for (i = 0; i < k; i++)
{
printf("%d \t", even[i]);
}
}