Sorting - I

Sorting - I

Write a C program to sort the given array of elements , place the even numbers in the right side and odd numbers in the left side in a sorted order.

Sample input   : 6
                          8 2 7 0 5 3
Sample output : 3 5 7 0 2 8


 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
#include<stdio.h>
int odd[20],even[20]; //global array variables
int top_o=-1,top_e=-1; 
void oddnum(int num) //to store odd numbers
{
    top_o++; //index of odd array
    odd[top_o]=num;
}
void evennum(int num) //to store even numbers
{
    top_e++; //index of even array
    even[top_e]=num;
}
void sort(int array[],int top) //to sort
{
    int tmp;
    for(int i=0;i<top;i++)
    {
        for(int j=i+1;j<=top;j++)
        {
            if(array[i]>=array[j])
            {
                tmp=array[i];
                array[i]=array[j];
                array[j]=tmp;
            }
        }
        printf("%d ",array[i]);
    }
    printf("%d ",array[top]);
}
 
int main()
{
    int n;
    scanf("%d",&n);
    int arr[n],i;
    for(i = 0; i < n ; i++)
    {
        scanf("%d",&arr[i]);
        if(arr[i]%2==0){
            evennum(arr[i]);}
        else{
            oddnum(arr[i]);}
    }
    sort(odd,top_o);
    sort(even,top_e);
    return 0;
}

OUTPUT :

0 comments