0% found this document useful (0 votes)
88 views

Algorithm Design and Analysis

This document discusses insertion sort and provides C code to implement insertion sort. The code takes in an array of integers as input, sorts the array using insertion sort, and prints out the sorted array after each iteration. It loops through the array, selects an element, finds its correct position in the sorted part of the array, and inserts it there by shifting other elements over.

Uploaded by

Candy Angel
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views

Algorithm Design and Analysis

This document discusses insertion sort and provides C code to implement insertion sort. The code takes in an array of integers as input, sorts the array using insertion sort, and prints out the sorted array after each iteration. It loops through the array, selects an element, finds its correct position in the sorted part of the array, and inserts it there by shifting other elements over.

Uploaded by

Candy Angel
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

ALGORITHM DESIGN AND ANALYSIS

1) Insertion sort- part 2


Code:
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int a[1000],i,j,k,n,c;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<n;i++)
{
c=a[i];
for(j=i-1;j>=0&&a[j]>c;j--)
{
a[j+1]=a[j];
}
a[j+1]=c;
for(k=0;k<n;k++)
{
printf("%d ",a[k]);
}
printf("\n");
}

return 0;
}

OUTPUT:
6
143562
Sample Output
143562
134562
134562
134562
1 23456

You might also like