C - Write program to Sort structure array using bubble sort

Requirements

Display of structures is sorted with the highest score listed first.

Hint

Sorting an array of structures works just like sorting any other array.

You can swap structure array elements just as you can swap any array elements.

Demo

#include <stdio.h>

int main()/*from   w  w w  .  java  2s  . co  m*/
{
    struct scores
    {
        char name[32];
        int score;
    };
    struct scores player[4];
    struct scores temp;
    int x,a,b;

    for(x=0;x<4;x++)
    {
        printf("Enter player %d: ",x+1);
        scanf("%s",player[x].name);
        printf("Enter their score: ");
        scanf("%d",&player[x].score);
    }

    for(a=0;a<4;a++)
        for(b=a+1;b<4;b++)
            if(player[a].score<player[b].score)
            {
                temp=player[a];
                player[a]=player[b];
                player[b]=temp;
            }

    puts("Player Info");
    printf("#\tName\tScore\n");
    for(x=0;x<4;x++)
    {
        printf("%d\t%s\t%5d\n",
            x+1,player[x].name,player[x].score);
    }
    return(0);
}

Result

Related Exercise