Advertisement
Adri_Satria

Mengurutkan Bilangan

Nov 13th, 2019
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.55 KB | None | 0 0
  1. //Insertion Sort
  2.  
  3. #include <stdio.h>
  4.  
  5. int total, data[10];
  6. int a, b;
  7.  
  8. void input()
  9.     {
  10.     scanf("%d",&total);
  11.  
  12.     for(a=0;a<total;a++)
  13.         {
  14.         scanf("%d",&data[a]);
  15.         }
  16.  
  17.     }
  18.  
  19. void sort()
  20.     {
  21.     int temp,key,i;
  22.  
  23.     for(a=0;a<total;a++)
  24.         {
  25.         key=data[a];
  26.         i=a-1;
  27.  
  28.         while(i>=0 && data[i]>key)
  29.             {
  30.             data[i+1]=data[i];
  31.             i=i-1;
  32.             data[i+1]=key;
  33.             }
  34.  
  35.         }
  36.  
  37.     }
  38.  
  39. void view()
  40.     {
  41.     for(a=0;a<total;a++)
  42.         {
  43.         printf("%d",data[a]);
  44.         }
  45.     printf("\n");
  46.     }
  47.  
  48. int main()
  49.     {
  50.     input();
  51.     sort();
  52.     view();
  53.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement