Advertisement
mbah_bejo

skorboard w/ Quicksort

Nov 22nd, 2019
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.37 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. typedef struct tes{
  4.     char nama[16];
  5.     int waktu;
  6.     int skor;
  7.     int ac;
  8. }tes;
  9.  
  10. void urut( int awal, int akhir, struct tes sb[], int kode)
  11. {  
  12. //printf("%d%d\n",awal,akhir);
  13.     tes temp;
  14.     int i, mot= akhir;
  15.     for(i=awal;i<mot;i++)
  16.     {
  17.     if (mot<0) break;
  18.         if((kode==1 && sb[i].ac<sb[mot].ac)
  19.         ||(kode==2 && sb[i].waktu>sb[mot].waktu && sb[i].ac==sb[mot].ac)
  20.         ||(kode==3 && sb[i].skor>sb[mot].skor&& sb[i].ac==sb[mot].ac && sb[i].waktu==sb[mot].waktu)
  21.         ||(kode==4 && strcmp(sb[i].nama,sb[mot].nama)>0))
  22.         {
  23.             temp=sb[i];
  24.            
  25.             sb[i]=sb[mot-1];
  26.             sb[mot-1]=sb[mot];
  27.             sb[mot]=temp;
  28.             i--; mot--;
  29.             //printf("%d%d\n",i,mot);
  30.         }
  31.     }
  32. //  for(i=0;i<akhir;i++)   
  33. //  printf("%s\n",sb[i].nama);
  34.     if (mot-1-awal > 0) urut(awal,mot-1,sb,kode);
  35.     if( akhir - (mot+1) > 0) urut(mot+1,akhir, sb,kode);
  36. }
  37.  
  38. int main(){
  39.    
  40.     int n,i,j,con,i1,n1,w,k, tanda=0;
  41.     char nama[100];
  42.     int ac,waktu,skor,ac1,waktu1,skor1;
  43.     scanf("%d",&n);
  44.     tes sb[n];
  45.     for(i=0;i<n;i++){
  46.         //getchar();
  47.         scanf("%s%d%d%d", nama,&ac,&waktu, &skor);
  48.         strcpy(sb[i].nama,nama);
  49.         sb[i].ac=ac; sb[i].waktu=waktu; sb[i].skor=skor;
  50.    
  51.     }
  52. //  for(i=0;i<n;i++)   
  53. //  printf("%s\n",sb[i].nama);
  54.    
  55.     for(w=1;w<=4;w++){
  56.         con=0;
  57.         if(w==1)
  58.         {
  59.         //printf("1");
  60.         urut(0,n-1,sb,w);
  61.  
  62.         }
  63.         else if (w==2)
  64.         {
  65.     //  printf("2");
  66.             con=sb[0].ac;
  67.             i=0;j=0;
  68.             for(k=0;k<n;k++){
  69.             if(sb[k].ac==con) j++;
  70.        
  71.         else
  72.         {
  73.         //printf("2.");
  74.             urut(i,j-1,sb,w); i=j; con=sb[k].ac;k--;
  75.         }
  76.     } urut(i,j-1,sb,w);
  77.     }
  78.    
  79.         else if (w==3)
  80.         {  
  81.     //  printf("3");
  82.             con=sb[0].waktu;
  83.             i=0;j=0;
  84.             for(k=0;k<n;k++){
  85.             if(sb[k].waktu==con) j++;
  86.          else
  87.         {
  88.     //  printf("3.");
  89.             urut(i,j-1,sb,w); i=j; con=sb[k].waktu;k--;
  90.         }
  91.     } urut(i,j-1,sb,w);
  92. }
  93.        
  94.         else if (w==4)
  95.         {
  96.     //  printf("4");
  97.             skor1=sb[0].skor;
  98.             ac1=sb[0].ac;
  99.             waktu1=sb[0].waktu;
  100.            
  101.             i=0;j=0;
  102.            
  103.             for(k=0;k<n;k++)
  104.             {
  105.             //  printf("%d",k);
  106.             if(sb[k].skor== skor1 && sb[k].ac==ac1 && sb[k].skor==skor1)   j++;
  107.         else
  108.         {
  109.     //  printf("4.");
  110.         urut(i,j-1,sb,w); i=j;
  111.             skor1=sb[k].skor;
  112.             ac1=sb[k].ac;
  113.             waktu1=sb[k].waktu;
  114.         k--;
  115.         }
  116.                
  117.             }
  118.             urut(i,j-1,sb,w);
  119.     }
  120.     }
  121.    
  122.             for(i=0;i<n;i++)   
  123.     printf("%s\n",sb[i].nama);
  124.     return 0;
  125. }
  126. /*
  127. 6
  128. palingjago 4 100 20
  129. jagobanget 4 90 25
  130. agakjago 4 90 20
  131. biasaaja 3 100 0
  132. acsemua 3 100 10
  133. semogaac 0 0 0
  134. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement