insertion sort using c language

 #include <stdio.h>


int main() {

    int n, i, j, 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]);


    // Insertion Sort Algorithm

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

        temp = arr[i];

        j = i - 1;

        while(j >= 0 && arr[j] > temp) {

            arr[j + 1] = arr[j];

            j = j - 1;

        }

        arr[j + 1] = 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

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

DWDM 2024 question paper with solution