0% found this document useful (0 votes)
4 views1 page

C Program for Insertion Sort

The document contains a C program that implements the insertion sort algorithm. It includes functions to print an array and to sort it using insertion sort, displaying the array after each insertion. The main function reads the size of the array and its elements from user input, then calls the sorting function.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

C Program for Insertion Sort

The document contains a C program that implements the insertion sort algorithm. It includes functions to print an array and to sort it using insertion sort, displaying the array after each insertion. The main function reads the size of the array and its elements from user input, then calls the sorting function.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <stdio.

h>

void print(int n, int arr[]) {


int i;
for(i=0; i<n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

void insertionSort(int n, int arr[]) {


int temp,i,j;
for(i=1;i<n;i++)
{
temp=arr[i];
j=i-1;
while(j>=0 && arr[j]>temp)
{
arr[j+1]=arr[j];
j--;

}
arr[j+1]=temp;
print(n, arr);
}
}

int main() {

int n;
scanf("%d", &n);
int arr[n],i;
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
print(n, arr);
insertionSort(n, arr);

return 0;
}

You might also like