Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<conio.h>
- void insertsort(int a[], int n) {
- int i,j,target;
- for(i = 1 ; i < n ; i++) {
- target=a[i];
- j=i-1;
- while((j >= 0) && (target <= a[j])) {
- a[j+1]=a[j];
- j=j-1;
- }
- a[j+1]=target;
- }
- }
- void display(int a[], int n) {
- int i;
- for(i = 0 ; i < n ; i++) {
- printf("%d\n",a[i]);
- }
- }
- void main() {
- int a[20],n,i;
- clrscr();
- printf("Enter the total number of elements");
- scanf("%d",&n);
- printf("Enterthe elements\n");
- for(i = 0 ; i < n ; i++) {
- scanf("%d",&a[i]);
- }
- insertsort(a,n);
- printf("\n Array elements after sorting\n");
- display(a,n);
- getch();
- }
- #include<stdio.h>
- int main() {
- int s,i,j,temp,a[20];
- clrscr();
- printf("Enter total elements: ");
- scanf("%d",&s);
- printf("Enter %d elements: ",s);
- for(i = 0 ; i < s ; i++) {
- scanf("%d",&a[i]);
- }
- for(i = 0 ; i < s ; i++) {
- for(j = i+1 ; j < s ; j++) {
- if(a[i]>a[j]) {
- temp=a[i];
- a[i]=a[j];
- a[j]=temp;
- }
- }
- }
- printf("After sorting is: ");
- for(i = 0 ; i < s ; i++) {
- printf(" %d",a[i]);
- }
- getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment