We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
voidswap(int*a,int*b){inttemp=*a;*a=*b;*b=temp;}intpartition(intarr[],intlow,inthigh){intpivot=arr[high];inti=(low-1);for(intj=low;j<=high-1;j++){if(arr[j]<=pivot){i++;swap(&arr[i],&arr[j]);}}swap(&arr[i+1],&arr[high]);return(i+1);}voidquickSort(intarr[],intlow,inthigh){if(low<high){intpi=partition(arr,low,high);quickSort(arr,low,pi-1);quickSort(arr,pi+1,high);}}/* * Complete the 'findMedian' function below. * * The function is expected to return an INTEGER. * The function accepts INTEGER_ARRAY arr as parameter. */intfindMedian(intarr_count,int*arr){intmedian=0;quickSort(arr,0,arr_count-1);if(arr_count%2!=0){intlongueur=((arr_count+1)/2)-1;median=arr[longueur];}else{intl1=arr_count/2,l2=l1+1,longueur=((l1+l2)/2)-1;median=arr[longueur];}returnmedian;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Find the Median
You are viewing a single comment's thread. Return to all comments →
the C code 😁😎