Thursday, August 13, 2026

Race Score Sorting || C Lab Programs

 Program 10: Sort race scores in descending order using Bubble Sort.

Problem Statement:: A sports teacher has recorded the scores of students in a 100-meter race. To prepare the result sheet, the teacher wants the scores arranged in descending order (highest to lowest). Develop a C program to sort these scores using the Bubble Sort technique.

Problem Description:

  • Input: Number of students (n) and their respective race scores.

  • Output: The list of scores displayed in descending order.

  • Constraints: Number of students must be > 0; scores are integers.

  • Method: Implement the Bubble Sort algorithm to arrange the elements.

Pgm Logic:

  1. Start.

  2. Input the number of students n.

  3. Input n scores into the array scores[].

  4. Perform nested loops for sorting:

    • Outer loop (i) from 0 to n-2.

    • Inner loop (j) from 0 to n-i-2.

    • If scores[j] < scores[j+1], swap the two elements.

  5. Print the sorted scores.

  6. Stop.

Program Code:

// Purpose: To sort student race scores in descending order using the Bubble Sort algorithm.

#include <stdio.h>


int main()

{

    int n, i, j, temp;

    printf("Enter number of students: ");

    scanf("%d", &n);

    int scores[n];

    printf("Enter the scores:\n");

    for(i = 0; i < n; i++)

        scanf("%d", &scores[i]);

    // Bubble Sort (Descending Order)

    for(i = 0; i < n-1; i++)

    {

        for(j = 0; j < n-i-1; j++)

        {

            if(scores[j] < scores[j+1])

            {

                temp = scores[j];

                scores[j] = scores[j+1];

                scores[j+1] = temp;

            }

        }

    }

    printf("Scores in descending order:\n");

    for(i = 0; i < n; i++)

        printf("%d ", scores[i]);

    return 0;

}


Output: 

Enter number of students: 4 

Enter the scores: 12 45 32 10 

Scores in descending order: 45 32 12 10

RESULT: Thus the program has been executed and the output was verified.

Remarks: Tested in the onlineGDB tool. Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order.

Program Explanation: The logic compares adjacent scores. If the current score is smaller than the next one, they are swapped. Through multiple passes, the smallest values "bubble" to the end of the array, leaving the largest values at the front.


For all 2026 published C Lab Program posts Index page: click here

For all 2026 published articles list: click here

…till the next post, bye-bye & take care