Tuesday, October 28, 2014

C implement sort pro



Implement qsort using function pointers
 
#include 
#include 
#include 
#include 
 struct s
{
    char empname[5];
    int empid;
};
 
int int_call(const void *a1,const void *b1)
{
    const int *a = (const int *)a1;
    const int *b = (const int *)b1;
 
    if (*a > *b)
        return 1;
    else
    {
        if (*a == *b) 
            return 0;
        else
            return -1;
    }
}
 
int string_call(const void *a1, const void *b1)
{
    const char *a = (const char *)a1;
    const char *b = (const char *)b1;
    return(strcmp(a, b));
}
 
void main()
{
    int array1[5]={20, 30, 50, 60, 10};
    struct s emprec[5];
    int i, j;
 
    strcpy(emprec[0].empname, "bbb");
    emprec[0].empid = 100;
    strcpy(emprec[1].empname, "ccc");
    emprec[1].empid = 200;
    strcpy(emprec[2].empname, "eee");
    emprec[2].empid = 300;
    strcpy(emprec[3].empname, "aaa");
    emprec[3].empid = 400;
    strcpy(emprec[4].empname,"ddd");
    emprec[4].empid = 500;
    qsort(array1, 5, sizeof(int), int_call);
    qsort(emprec, 5, sizeof(struct s), string_call);
    for (i = 0; i < 5; i++)
        printf("%d\t", array1[i]);
    printf("\nSorting of Structure elements ");
    for (i = 0; i < 5; i++)
        printf("\n%s\t%d", emprec[i].empname, emprec[i].empid);
    printf("\n");
}
 
Output
 
10    20    30    50    60    
Sorting of Structure elements 
aaa    400
bbb    100
ccc    200
ddd    500
eee    300
 
Implement Pigeonhole Sort
 
#include 
#include 
#define max 7
void pigeonhole_sort(int, int, int *);
void main()
{
    int a[max], i, min, max;
    printf("enter the values into the matrix :");
    for (i = 0; i < max; i++)
    {
        scanf("%d", &a[i]);
    }
    min = a[0];
    max = a[0];
    for (i = 1; i < max; i++)
    {
        if (a[i] < min)
        {
            min = a[i];
        }
        if (a[i] > max)
        {
            max = a[i];
        }
    }
    pigeonhole_sort(min, max, a);
    printf("Sorted order is :\n");
    for (i = 0; i < max; i++)
    {
        printf("%d", a[i]);
    }
}
 
void pigeonhole_sort(int mi, int ma, int * a)
{
 
    int size, count = 0, i;
    int *current;
    current = a;
    size = ma - mi + 1;
    int holes[size];
    for (i = 0; i < size; i++)
    {
        holes[i] = 0;
    }
    for (i = 0; i < size; i++, current++)
    {
        holes[*current-mi] += 1;
    }
    for (count = 0, current = &a[0]; count < size; count++)
    {
        while (holes[count]--> 0)
        {
            *current++ = count + mi;
        }
    }
}
 
Output
 
Enter the values into the matrix :7 3 8 2 5 4 9
Sorted order is :
2345789
Enter the values into the matrix :1 2 3 4 5 6 7
Sorted order is :
1234567
Enter the values into the matrix :7 6 5 4 3 2 1
Sorted order is :
1234567
Implement Cyclesort
 
#include 
#include 
#define max 8
void cycle_sort(int *);
void main()
{
    int a[max],i;
    printf("enter the elements into array :");
    for (i = 0;i < max; i++)
    {
        scanf("%d", &a[i]);
    }
    cycle_sort(a);
    printf("sorted elements are :\n");
    for (i = 0;i < max; i++)
    {
        printf("%d", a[i]);
    }
}
 void cycle_sort(int * a)
{
    int temp, item, pos, i, j, k;
 
    for (i = 0;i < max; i++)
    {
        item = a[i];
        pos = i;
        do
        {
            k = 0;
            for (j = 0;j < max;j++)
            {
                if (pos != j && a[j] < item)
                {
                    k++;
                }
            }
            if (pos != k)
            {
                while (pos != k && item == a[k])
                {
                    k++;
                }
                temp = a[k];
                a[k] = item;
                item = temp;
                pos = k;
            }
        }while (pos != i);
    }
}
 
Output
 
Enter the elements into array :7 3 2 5 4 8 9 6
sorted elements are :
23456789
Enter the elements into array :7 3 2 4 5 4 6 3
sorted elements are :
23344567

No comments:

Post a Comment