Selection Sort using C Programing

 Selection Sort using C Programing



Code With Rudy
#include <stdio.h>

void main() {
    int array[100], c, j, swap, pos, n;
    printf("Enter number of elements: ");
    scanf("%d", &n);
    printf("Enter %d elements: ", n);
    for (c = 0; c < n; c++) {
        scanf("%d", &array[c]);
    }
    for (c = 0; c < n - 1; c++) {
        pos = c; 
        for (j = c + 1; j < n; j++) {
            if (array[j] < array[pos]) {
                pos = j;
            }
        }
        if (pos != c) {
            swap = array[c];
            array[c] = array[pos];
            array[pos] = swap;
        }
    }
    printf("Sorted elements: ");
    for (c = 0; c < n; c++) {
        printf("%d ", array[c]);
    }
    printf("\n");
}

Post a Comment

0 Comments