Advertisement
fiveriverflow

Array Sort + Median

Dec 15th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. double getMedian();
  6.  
  7. int a[] = {3, 4, 5, 1, 6};
  8.  
  9. double getMedian(int array[], int size) {
  10.     return (size % 2 ? array[size / 2] : (array[size / 2 - 1] + array[size / 2]) / 2);
  11. }
  12.  
  13. void sort(int array[], int size) {
  14.     for(int i=0;i<size;i++)
  15.     {
  16.         for(int j=0;j<size;j++)
  17.         {
  18.             if(a[i]>a[j])
  19.             {
  20.                 int temp=a[i];
  21.                 a[i]=a[j];
  22.                 a[j]=temp;
  23.             }
  24.         }
  25.     }  
  26. }
  27.  
  28. int main() {
  29.    
  30.     int size = sizeof(a)/sizeof(int);
  31.    
  32.     sort(a, size);
  33.  
  34.     cout << getMedian(a, size) << " ";
  35.  
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement