/*
Write a program to sort n integers using insertion sort.
*/
#include <stdio.h>
void insert_sort(int arr[],int n)
{
int i,j,temp;
for(j=1;j<n;j++)
{
temp=arr[j];
for(i=j-1;i>=0 && temp<arr[i];i--)
arr[i+1]=arr[i];
arr[i+1]=temp;
}
}
main()
{
int arr[100],i,j,n;
printf("Enter the number of elements : ");
scanf("%d",&n);
for (i = 0; i < n; i++)
{
printf("Enter element %d : ",i+1);
scanf("%d", &arr[i]);
}
printf("Unsorted list is :\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
insert_sort(arr,n);
printf("Sorted list is :\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
Write a program to sort n integers using insertion sort.
*/
#include <stdio.h>
void insert_sort(int arr[],int n)
{
int i,j,temp;
for(j=1;j<n;j++)
{
temp=arr[j];
for(i=j-1;i>=0 && temp<arr[i];i--)
arr[i+1]=arr[i];
arr[i+1]=temp;
}
}
main()
{
int arr[100],i,j,n;
printf("Enter the number of elements : ");
scanf("%d",&n);
for (i = 0; i < n; i++)
{
printf("Enter element %d : ",i+1);
scanf("%d", &arr[i]);
}
printf("Unsorted list is :\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
insert_sort(arr,n);
printf("Sorted list is :\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
No comments:
Post a Comment