Q:

Write a C program to sort even and odd elements of array separately

0

Write a C Program to sort even and odd elements of array separately. Here’s simple Program to sort even and odd elements of array separately in C Programming Language.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

What is an Array ?


Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

 
 

Instead of declaring individual variables, such as number0, number1, …, and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and …, numbers[99] to represent individual variables. A specific element in an array is accessed by an index.

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.


Here is source code of the C Program to sort even and odd elements of array separately. The C program is successfully compiled and run(on Codeblocks) on a Windows system. The program output is also shown in below.

 

SOURCE CODE : :

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

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

#define MAX_SIZE 1000 //Maximum size of the array

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

    printf("Enter size of the array :: ");
    scanf("%d", &n);
    printf("\nEnter elements in the array ::\n ");
    for(i=0; i<n; i++)
    {
        printf("\nEnter %d element in an array :: ",i+1);
        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("\nElements in the array: ");
    for(i=0; i<len; i++)
    {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

OUTPUT : :


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

Enter size of the array :: 8

Enter elements in the array ::

Enter 1 element in an array :: 1

Enter 2 element in an array :: 2

Enter 3 element in an array :: 3

Enter 4 element in an array :: 4

Enter 5 element in an array :: 5

Enter 6 element in an array :: 6

Enter 7 element in an array :: 7

Enter 8 element in an array :: 8

Elements in the array: 1 2 3 4 5 6 7 8

Final array after sorting even and odd elements separately

Elements in the array: 1 3 5 7 2 4 6 8

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Arrays Solved Programs – C Programming

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a C Program to search an element in an array... >>
<< Write a C program to sort array elements in descen...