Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.86 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define N 10
  4. #define M 11
  5.  
  6. void swap(int *max, int *min)
  7. {
  8.     if (*min > *max)
  9.     {
  10.         int temp = *max;
  11.         *max = *min;
  12.         *min = temp;
  13.     }
  14. }
  15.  
  16. void minmax(int *arr, int n, int *max, int *min)
  17. {
  18.     int Lmax, Rmax, Lmin, Rmin;
  19.    
  20.     if (n == 0)
  21.     {
  22.         *min = *max = -1;
  23.         return;
  24.     }
  25.  
  26.     if (n == 1)
  27.     {
  28.         *min = *max = arr[0];
  29.         return;
  30.     }
  31.  
  32.     if (n == 2)
  33.     {
  34.         *max = arr[0];
  35.         *min = arr[1];
  36.         swap(max,min);
  37.         return;
  38.     }
  39.  
  40.     minmax(arr,n/2,&Lmax,&Lmin);
  41.     minmax(arr+(n/2),(n/2)+(n%2),&Rmax,&Rmin);
  42.  
  43.     *max = (Lmax > Rmax)? Lmax : Rmax;
  44.     *min = (Lmin < Rmin)? Lmin : Rmin;
  45. }
  46.  
  47. void main()
  48. {
  49.     int max,min;
  50.     int arr1[N] = {3,1,17,9,4,2,27,21,7,10};
  51.     int arr2[M] = {6,5,4,8,1,90,11,21,34,32,33};
  52.  
  53.     minmax(arr1,N,&max,&min);
  54.     printf("max = %d, min = %d\n",max,min);
  55.  
  56.     minmax(arr2,M,&max,&min);
  57.     printf("max = %d, min = %d\n",max,min);
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement