C program to sort even and odd elements of array separately

Write a C program to input elements in an array from user and sort all even and odd elements of the given array separately without using any other array. If minimum element of the array is even then all even elements should be placed in sorted order before odd elements otherwise all odd elements should be placed first. How to sort all even and odd elements of a given array separately in C programming.

Example:
Input size of array: 10
Input elements of array: 0 5 1 2 3 4 6 12 10 9
Output in sorted order: 0 2 4 6 10 12 1 3 5 9

Required knowledge

Basic C programming, If else, Loop, Array, Functions

Logic to sort even and odd elements separately

Before continuing to this program you must be done with simple sorting of array in ascending order or descending order. Now, coming up to the logic of this program, what we need to do is first to make our task easy we must arrange all elements of array in two parts even and odd (proper sorting of elements are not necessary). Then after arranging all even and odd elements separately sort both parts one by one.

Program to sort even and odd elements separately

/**
 * C program to sort even and odd elements of an array separately
 */

#include <stdio.h>
#include <limits.h> //Used for INT_MAX

#define MAX_SIZE 1000 //Maximum size of the array


/*
 * Functions used in this program
 */
void arrange(int arr[], int len, int evenIndex, int evenCount, int oddIndex, int oddCount);
void sort(int arr[], int start, int end);
void print(int arr[], int len);



int main()
{
    int arr[MAX_SIZE], i, n;
    int evenIndex, oddIndex, evenCount, oddCount, min;

    evenCount = oddCount = 0;
    min = INT_MAX; //Maximum value of integer

    /*
     * Reads size and elements in the array
     */
    printf("Enter size of the array: ");
    scanf("%d", &n);
    printf("Enter elements in the array: ");
    for(i=0; i<n; i++)
    {
        scanf("%d", &arr[i]);

        // Counts total number of even and odd elements
        if(arr[i] & 1)
            oddCount++;
        else
            evenCount++;

        //Checks for the minimum element
        if(arr[i] <= min)
            min = arr[i];
    }

    /*
     * Finds the first index of even and odd elements.
     * If minimum element is odd then put odd elements before even
     * elements otherwise put even elements first.
     */
    if(min & 1)
    {
        oddIndex = 0;
        evenIndex = oddCount;
    }
    else
    {
        evenIndex = 0;
        oddIndex = evenCount;
    }


    print(arr, n);

    //Arranges all even and odd elements sequentially
    arrange(arr, n, evenIndex, evenCount, oddIndex, oddCount);

    //Sorts even part of the array
    sort(arr, evenIndex, evenCount);

    //Sorts odd part of the array
    sort(arr, oddIndex, oddCount);

    //Prints the final sorted array
    printf("\nFinal array after sorting even and odd elements separately\n");
    print(arr, n);

    return 0;
}



/**
 * Arranges all even and odd elements of the array separately. 
 */
void arrange(int arr[], int len, int evenIndex, int evenCount, int oddIndex, int oddCount)
{
    int i, j, temp;

    for(i=0; i<len; i++)
    {
        /*
         * If current element of array is odd put it into
         * odd element place
         */
        if(arr[i] & 1)
        {
            for(j=oddIndex; j<oddCount; j++)
            {
                //Look for an even element then swap with odd element
                if(!(arr[j] & 1))
                {
                    temp = arr[j];
                    arr[j] = arr[i];
                    arr[i] = temp;
                    break;
                }
            }
        }
        else //If current element is even
        {
            for(j=evenIndex; j<evenCount; j++)
            {
                //Look for an odd element then swap with even element
                if(arr[j] & 1)
                {
                    temp = arr[j];
                    arr[j] = arr[i];
                    arr[i] = temp;
                    break;
                }
            }
        }
    }
}



/**
 * Sorts the elements of array within a range 
 */
void sort(int arr[], int start, int end)
{
    int i, j, temp;
    int len = start + end;

    for(i=start; i<len; i++)
    {
        for(j=i+1; j<len; j++)
        {
            if(arr[j] < arr[i])
            {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
}



/**
 * Prints the entire integer array 
 */
void print(int arr[], int len)
{
    int i;
    printf("Elements in the array: ");
    for(i=0; i<len; i++)
    {
        printf("%d ", arr[i]);
    }
    printf("\n");
} 


Output
Enter size of the array: 10
Enter elements in the array: 0 1 2 3 4 5 6 7 8 9
Elements in the array: 0 1 2 3 4 5 6 7 8 9
Final array after sorting even and odd elements separately
Elements in the array: 0 2 4 6 8 1 3 5 7 9

Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment