๐Ÿ’ป
B.TECH I YEAR โ€ข I SEMESTER โ€ข R25

Programming for Problem Solving - Lab Manual

C Programming Laboratory โ€ข Program Solution

๐Ÿ“˜ R25 Regulation ๐ŸŽ“ I Year ๐Ÿ“š I Semester ๐Ÿ’ป C Programming ๐Ÿงช Lab Program
Selected Program

Write a C program that sorts the given array of integers using selection sort in descending order.

โ† All Lab Programs

Write a C program that sorts the given array of integers using selection sort in descending order.

selection sort
C Program
//The given array of integers using selection sort in descending order.
#include <stdio.h>

int main() {
    int arr[50], n, i;
    void selectionSortDescending(int [], int );

    // Input number of elements
    printf("Enter number of elements: ");
    scanf("%d", &n);

    // Input array elements
    printf("Enter %d integers:\n", n);
    for (i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    // Call function
    selectionSortDescending(arr, n);

    // Output sorted array
    printf("Array sorted in descending order:\n");
    for (i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }

    return 0;
}

// Function to perform selection sort in descending order
void selectionSortDescending(int arr[], int n) {
    int i, j, maxIndex, temp;

    for (i = 0; i < n - 1; i++) {
        maxIndex = i;  // Assume current index is the largest

        // Find the actual largest element in the remaining array
        for (j = i + 1; j < n; j++) {
            if (arr[j] > arr[maxIndex]) {
                maxIndex = j;
            }
        }

        // Swap if needed
        if (maxIndex != i) {
            temp = arr[i];
            arr[i] = arr[maxIndex];
            arr[maxIndex] = temp;
        }
    }
}

OUTPUT
Enter number of elements: 9
Enter 9 integers:
4 5 6 1 7 2 8 3 9
Array sorted in descending order:
9 8 7 6 5 4 3 2 1 

๐Ÿ’ป Run This C Program

Write a C program that sorts the given array of integers using selection sort in descending order.
The selected C program is loaded automatically. Edit the code, provide input through STDIN and click Run.

The compiler is embedded using OneCompiler. Your program runs in the embedded C environment; Study Glance does not execute C code directly on your web server.
โ† Back to PPS Lab Programs ๐Ÿ“š All Lab Manuals