Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Data File StructureRSS Feeds

Sorting of a structure on names using bubble sort

Posted By: Vilhelm Fischer     Category: C Programming     Views: 11462

Sorting of a structure on names using bubble sort.

Code for Sorting of a structure on names using bubble sort in C Programming

#include <stdio.h>
#include <conio.h>

void main( )
{
    struct emp
    {
        char empid[7] ;
        char name[25] ;
        int age ;
        float sal ;
    } ;

    int i, j ;
    struct emp e[5] ;
    char id[5][7], temp[7] ;
    float ff ( float ) ;

    clrscr( ) ;
    printf ( "Enter empid, name, age and salary :-\n") ;
    for ( i = 0 ; i <= 4 ; i++ )
    {
        scanf ( "%s %s %d %f", e[i].empid, e[i].name, &e[i].age, &e[i].sal ) ;
        strcpy ( id[i], e[i].empid ) ;
    }

    for ( i = 0 ; i <= 3 ; i++ )
    {
        for ( j = 0 ; j <= 3 - i ; j++ )
        {
            if ( strcmp ( id[j], id[j + 1] ) > 0 )
            {
                strcpy ( temp, id[j] ) ;
                strcpy ( id[j], id[j + 1] ) ;
                strcpy ( id[j + 1], temp ) ;
            }
        }
    }

    printf ( "\nRecords after sorting") ;
    printf ( "\nName, age and salary after sorting :-\n") ;
    for ( i = 0 ; i <= 4 ; i++ )
    {
        for ( j = 0 ; j <= 4 ; j++ )
        {
            if ( strcmp( id[i], e[j].empid ) == 0 )
                printf ( "%s %s %d %f\n", e[j].empid, e[j].name, e[j].age, e[j].sal ) ;
        }
    }

    getch( ) ;
}

float ff ( float f )
{
    float *f1 = &f ;
    return *f1 ;
}
  
Share: 


Didn't find what you were looking for? Find more on Sorting of a structure on names using bubble sort Or get search suggestion and latest updates.

Vilhelm Fischer
Vilhelm Fischer author of Sorting of a structure on names using bubble sort is from Frankfurt, Germany.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!