Monday 13 November 2017

C Program to Implement Oddeven Sort


Code:

#include   stdio.h
#define MAX 7

void swap(int *,int *);
void oddeven_sort(int *);

void main()
{
    int a[MAX], i;

    printf("enter the elements in to the matrix :");
    for (i = 0;i < MAX;i++)
    {
        scanf("%d", &a[i]);
    }
    printf("sorted elements are :\n");
    oddeven_sort(a);
    for (i = 0;i < MAX;i++)
    {
        printf(" %d", a[i]);
    }
}

/* swaps the elements */
void swap(int * x, int * y)
{
    int temp;

    temp = *x;
    *x = *y;
    *y = temp; 
}

/* sorts the array using oddeven algorithm */
void oddeven_sort(int * x)
{
    int sort = 0, i;

    while (!sort)
    {
        sort = 1;
        for (i = 1;i < MAX;i += 2)
        {
            if (x[i] > x[i+1])
            {
                swap(&x[i], &x[i+1]);
                sort = 0;
            }
        }
        for (i = 0;i < MAX - 1;i += 2)
        {
            if (x[i] > x[i + 1])
            {
                swap(&x[i], &x[i + 1]);
                sort = 0;
            }
        }
    }
}



Output:

/* average case */
$ a.out
enter the elements in to the matrix :7 8 3 2 5 4 9
sorted elements are :
 2 3 4 5 7 8 9

/* best case */
$ a.out
enter the elements in to the matrix :1 2 3 4 5 6 7
sorted elements are :
 1 2 3 4 5 6 7

/* worst case */
$ a.out
enter the elements in to the matrix :7 6 5 4 3 2 1
sorted elements are :
 1 2 3 4 5 6 7



More C Programs:














100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...