#include<stdio.h>
void insertion_sort(int [], int k);
int main()
{
int arr[100], n, i, j;
printf("\n= = = = = = = = = = Insertion Sort in C = = = = = = = = = =\n");
printf("How many integers you wanna store..? : ");
scanf("%d",&n);
printf("Enter %d integers into the array:\n", n);
for(i=0; i<n; i++)
{
scanf(" %d",&arr[i]);
}
printf("Before sorting the array elements are:\n");
for(j=0; j<n; j++)
{
printf("%d\t", arr[j]);
}
insertion_sort(arr,n);
printf("\n\nAfter sorting the array elements are:\n");
for(i=0; i<n; i++)
{
printf("%d\t",arr[i]);
}
printf("\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
return 0;
}
void insertion_sort(int arr[], int k)
{
int i, j, temp;
for(i=1; i<k; i++)
{
temp=arr[i];
j=i-1;
while((j>=0) && (arr[j]>temp))
{
arr[j+1]=arr[j];
j--;
}
arr[j+1]=temp;
}
}
OutPut:
manoj@ubuntu:~/cp$ gcc insertionsort.c -o insertionsort
manoj@ubuntu:~/cp$ ./insertionsort
= = = = = = = = = = Insertion Sort in C = = = = = = = = = =
How many integers you wanna store..? : 8
Enter 8 integers into the array:
2
6
7
3
5
4
8
1
Before sorting the array elements are:
2 6 7 3 5 4 8 1
After sorting the array elements are:
1 2 3 4 5 6 7 8
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
manoj@ubuntu:~/cp$
void insertion_sort(int [], int k);
int main()
{
int arr[100], n, i, j;
printf("\n= = = = = = = = = = Insertion Sort in C = = = = = = = = = =\n");
printf("How many integers you wanna store..? : ");
scanf("%d",&n);
printf("Enter %d integers into the array:\n", n);
for(i=0; i<n; i++)
{
scanf(" %d",&arr[i]);
}
printf("Before sorting the array elements are:\n");
for(j=0; j<n; j++)
{
printf("%d\t", arr[j]);
}
insertion_sort(arr,n);
printf("\n\nAfter sorting the array elements are:\n");
for(i=0; i<n; i++)
{
printf("%d\t",arr[i]);
}
printf("\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
return 0;
}
void insertion_sort(int arr[], int k)
{
int i, j, temp;
for(i=1; i<k; i++)
{
temp=arr[i];
j=i-1;
while((j>=0) && (arr[j]>temp))
{
arr[j+1]=arr[j];
j--;
}
arr[j+1]=temp;
}
}
OutPut:
manoj@ubuntu:~/cp$ gcc insertionsort.c -o insertionsort
manoj@ubuntu:~/cp$ ./insertionsort
= = = = = = = = = = Insertion Sort in C = = = = = = = = = =
How many integers you wanna store..? : 8
Enter 8 integers into the array:
2
6
7
3
5
4
8
1
Before sorting the array elements are:
2 6 7 3 5 4 8 1
After sorting the array elements are:
1 2 3 4 5 6 7 8
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
manoj@ubuntu:~/cp$
No comments:
Post a Comment