Advertisement
VSZM

Rendezések

Apr 25th, 2014
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.13 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. void Swap(int *a, int *b)// a := 8, b := 3
  5. {
  6.     *a += *b; // a = 11, b = 3
  7.     *b = *a - *b;// a = 11, b = 8
  8.     *a -= *b;// a = 3, b = 8
  9. }
  10.  
  11.  
  12. // return <0 ha a előrébb van mint b
  13. // return 0 ha ekvivalensek a rendezés szempontjából
  14. // return >0 ha b előrébb van mint a
  15. int Min_Cmp(const void* _a,const void* _b)
  16. {
  17.     int a = *(int*)_a, b = *(int*)_b;
  18.     return a - b;
  19. }
  20.  
  21.  
  22. // Páros számokat rak előre, azon belül a kisebbeket.
  23. int Parity_Min_Cmp(const void* _a,const void* _b)
  24. {
  25.     int a = *(int*)_a, b = *(int*)_b;
  26.  
  27.     if(abs(a)%2 == abs(b)%2) return a - b;
  28.     return abs(a)%2 - abs(b)%2;
  29. }
  30.  
  31. void Min_Sort(int* arr, int len, int(*cmp)(int,int))
  32. {
  33.     int i, j;
  34.  
  35.     for(i = 0; i < len -1;++i )
  36.     {
  37.         int min_idx = i;       
  38.  
  39.         for(j = i+1; j < len; ++j)
  40.         {
  41.             if(cmp(arr[j], arr[min_idx]) < 0) min_idx = j;
  42.         }
  43.        
  44.         if(min_idx != i)
  45.             Swap(arr + i, arr + min_idx);
  46.        
  47.     }
  48.  
  49. }
  50.  
  51. int main()
  52. {
  53.     int arr[]={ 34, 45, 1 , 5, 15, 27, 88, -1,0 }, i;  
  54.  
  55.     qsort(arr, 9, sizeof(int),Parity_Min_Cmp);
  56.  
  57.  
  58.     for(i = 0; i < 9; ++i) printf("%d\t", arr[i]);
  59.    
  60.  
  61.     printf("\n\n\n");
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement