Selection sort using c language

 #include <stdio.h>


int main() {

    int n, i, j, min, temp;

    int arr[100];


    // Input: Number of elements

    printf("Enter number of elements: ");

    scanf("%d", &n);


    // Input: Array elements

    printf("Enter %d integers:

", n);

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

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


    // Selection Sort Algorithm

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

        min = i;

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

            if (arr[j] < arr[min])

                min = j;

        // Swap

        temp = arr[i];

        arr[i] = arr[min];

        arr[min] = temp;

    }


    // Output: Sorted array

    printf("Sorted array:

");

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

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

    printf("

");


    return 0;

}

Comments

Popular posts from this blog

B.tech 5th Sem Machin learning lab practical

DWDM 2024 question paper with solution

BCA 3rd year Web Technology(code 304) Web Site development using ASP.NET AND C#