C

Insertion sort algorithm in C

#include <stdio.h>
int main()
{
    int n, i, j, temp;
    int arr[64];

    printf("Enter number of elements\n");
    scanf("%d", &n);

    printf("Enter %d integers\n", n);
    for (i = 0; i < n; i++)
    {
        scanf("%d", &arr[i]);
    }
    for (i = 1 ; i <= n - 1; i++)
    {
        j = i;
        while ( j > 0 && arr[j-1] > arr[j])
        {
            temp     = arr[j];
            arr[j]   = arr[j-1];
            arr[j-1] = temp;
            j--;
        }
    }
    printf("Sorted list in ascending order:\n");
    for (i = 0; i <= n - 1; i++)
    {
        printf("%d\n", arr[i]);
    }
    return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

October 2020
M T W T F S S
« Sep   Nov »
 1234
567891011
12131415161718
19202122232425
262728293031  

Site Statistics

  • Users online: 0 
  • Visitors today : 13
  • Page views today : 13
  • Total visitors : 3,981
  • Total page view: 4,456