SHARE
TWEET

Untitled

a guest Sep 17th, 2019 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void merge_sort(int num[],int lo,int hi){
  5.     int i,j,k;
  6.     if(lo==hi) return;
  7.     int mid=(lo+hi)/2;
  8.  
  9.     merge_sort(num,lo,mid);
  10.     merge_sort(num,mid+1,hi);
  11.  
  12.     int temp[1000001];
  13.  
  14.     for(i=lo,j=mid+1,k=lo;k<=hi;k++){
  15.         if(i==mid+1) temp[k]=num[j++];
  16.         else if(j==hi+1) temp[k]=num[i++];
  17.         else if(num[i]<num[j]) temp[k]=num[i++];
  18.         else temp[k]=num[j++];
  19.     }
  20.     for(k=lo;k<=hi;k++){
  21.         num[k]=temp[k];
  22.     }
  23.  
  24. }
  25.  
  26. int findMedian(int arr[], int n){
  27.  
  28.     merge_sort(arr,0,n-1);
  29.     int mid=ceil(n/2);
  30.     return arr[mid];
  31.  
  32. }
  33.  
  34. int main(){
  35.     int n;
  36.     int a[1000001];
  37.  
  38.     scanf("%d",&n);
  39.     for(int i=0;i<n;i++){
  40.         scanf("%d",&a[i]);
  41.     }
  42.  
  43.     printf("%d",findMedian(a,n));
  44.  
  45.     return 0;
  46.  
  47. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top