Advertisement
mbah_bejo

Quick Sort

Nov 5th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.34 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3.  
  4.  
  5.  
  6. void cekin1( int arr[], int down, int up)
  7. {  
  8. int pisah,temp,i;
  9. pisah = up;
  10.     for(i=down;i<pisah;i++)
  11.     {  
  12.         if(arr[i]>arr[pisah])
  13.         {
  14.             temp= arr[i];
  15.             arr[i]=arr[pisah-1];
  16.             arr[pisah-1]=arr[pisah];
  17.             arr[pisah]=temp;
  18.             i--;pisah--;
  19.         }
  20.        
  21.     }
  22.        
  23.         if(pisah-1-down>0)cekin1(arr,down,pisah-1);
  24.         if(up-(pisah+1)>0)cekin1(arr,pisah+1,up);
  25.    
  26. }
  27.  
  28. int main()
  29. {   int n;
  30.     int j,z,tut,i,m,target, start,counter;
  31.     char tanda;
  32.    
  33.     scanf("%d",&n);
  34.  
  35.     int arr[n+1];
  36.    
  37.     for(i=0;i<n;i++)
  38.     {
  39.         scanf("%d", &arr[i]);
  40.     }
  41. //  for(z=0;z<n;z++)
  42. //      {
  43. //          printf("%d",arr[z]);
  44. //      } printf("\n");
  45. // 
  46.  
  47.     cekin1(arr,0,n-1);  
  48.    
  49.    
  50.     scanf(" %c", &tanda);
  51.     scanf("%d",&m);
  52.    
  53.    
  54.    
  55. //      for(z=0;z<n;z++)
  56. //      {
  57. //          printf("%d",arr[z]);
  58. //      } printf("\n");
  59.    
  60.         for(j=0;j<m;j++)
  61.         { counter=-1;
  62.         //  getchar();
  63.             scanf("%d",&target);
  64.        
  65.         if(tanda=='d')
  66.         {tut=0;
  67.             for(start=n-1;start>=0;start--)
  68.             {  
  69.                 //printf("%d_",arr[start]);
  70.                 if(arr[start]==target)
  71.                 {
  72.                     tut++;
  73.                 counter=tut;
  74.                
  75.                 break;
  76.                 } else
  77.                 tut++;
  78.             }
  79.        
  80.         } else
  81.         {
  82.             for(start=0;start<n;start++)
  83.             {
  84.                 //printf("%d_",arr[start]);
  85.                 if(arr[start]==target){
  86.                 counter=start+1;
  87.                 break;}
  88.             }
  89.            
  90.  
  91.         }
  92.                  printf("%d\n",counter);
  93.             //if(counter==1)continue;
  94.     }
  95.        
  96.             return 0;
  97.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement