Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- void Insertsort(int data[],int n)
- {
- int temp,i,j;
- printf("Initial array is\n");
- for(i=0;i<n;i++)
- {
- printf("%d\t",data[i]);
- }
- for(i=0;i<n-1;i++)
- {
- for(j=1;j<n;j++)
- {
- temp=data[j];
- i=j-1;
- while(i>=0 && data[i]>temp)
- {
- data[i+1]=data[i];
- i=i-1;
- data[i+1]=temp;
- }
- }
- }
- printf("\nThe final array is\n ");
- for(i=0;i<n;i++)
- {
- printf("%d\t",data[i]);
- }
- }
- void main()
- {
- int a[20],n,i,j;
- printf("Enter the no of elemnets\n");
- scanf("%d",&n);
- printf("Enter elements\n");
- for(i=0;i<n;i++)
- scanf("%d",&a[i]);
- Insertsort(a,n);
- return 0;
- }
- OUTPUT
- Enter the no of elemnets
- 5
- Enter elements
- 4
- 5
- 9
- 1
- 2
- Initial array is
- 4 5 9 1 2
- The final array is
- 1 2 4 5 9 Press any key to continue
Advertisement
Add Comment
Please, Sign In to add comment