Wednesday, April 17, 2013

Sort structure type array using bubble sort

This program sorts the structure type array using bubble sort method.In bubble sorting,every value of the array is compared sequentially and temporary variable t is used for sorting the values.Here the student details are stored in the struture type array and it is sorted by percentage using bubble sort method.The percentage value is used in bubble sort method for sorting the structure type array.
-Initially,n different values are placed in constant array with sufficient large size.
-Here,the comparison starts from bottom position(0) and in each iteration the smallest bubble is shifted upward..
-Each comparison starts from bottom position by using the values from 2 positions (0 and 1). After comparision the smallest value is placed in position 1.Then the values from next 2 positions (1 and 2) are used.After comparison the smallest value is placed in position 2.This process continous in upward direction.
-In first iteration,the smallest value is placed in position (n-1).
-In second iteration,the smallest value is placed in position (n-2).
-In third iteration,the smallest value is placed in position (n-3).
-In last iteration,the last smallest value is placed at n th position i.e. at the bottom.
//... C Language Program to Sort a Struct type Array by using a Bubble Sort method
#include < stdio.h >
#include < conio.h >

struct stud
{
    int roll;
    char name[15];
    float per;
};

void main()
{
    struct stud a[50], t;
    int i, j, n;

    clrscr();

    printf("\n C Language Program to Sort Struct type Array by using a Bubble Sort method ");
    printf("\n To sort the Student Records in Dercreasing Order of % (Percentage) \n");
    printf("\n Enter How Many Records [ i.e. Size of Array (n) ] : ");
    scanf("%d", &n);
    read_data(a, n);

    printf("\n %d Records Before Sorting are \n", n);
    print_data(a, n);

    bbl_sort(a, n);

    printf("\n %d Values After Sorting are \n", n);
    print_data(a, n);

} // main

int read_data( struct stud a[], int n )
{
    int i;
    float t;

    printf("\n Enter %d Records \n", n);
    for(i = 0; i < n; i++)
    {
        printf("\n Roll No. : ");
        scanf("%d", &a[i].roll);
        printf("\n Name : ");
        flushall();
        gets(a[i].name);
        printf("\n Percentage (%) : ");
        scanf("%f", &t);
        a[i].per = t;
    } // for
    return;
} // read_data

int print_data( struct stud a[], int n )
{
    int i;
    float t;

    printf("\n Roll No. \t Name \t Percentage (%) \n");
    for(i = 0; i < n; i++)
    {
        printf("\n \t %d \t %s \t %.2f", a[i].roll, a[i].name, a[i].per);
    } // for
    return;
} // print_data

int bbl_sort( struct stud a[], int n )
{
    int i,j, k;
    struct stud t;

    for(k = n - 1; k >= 1; k--)
    {
        for(i = 0,j = 1; j <= k; i++,j++)
        {
            if( a[i].per > a[j].per)
            {
                t = a[i];
                a[i] = a[j];
                a[j] = t;
            } // if
        } // for
    } // for
    return;
} // bbl_sort

No comments:

Post a Comment