PTA_基础编程题目集_6-11 求自定类型元素序列的中位数 (25 分)"qsort使用案例"

#include <stdlib.h>
int compare(const void *arg1, const void *arg2);

ElementType Median(ElementType A[], int N)
{
	ElementType *t;
	t = &A[0];
	qsort(t,N, sizeof(ElementType), compare);

  return A[N / 2];
}

int compare(const void *arg1, const void *arg2)
{
  if(*(ElementType*)arg2<=*(ElementType*)arg1)
    return 1;
  
  if(*(ElementType*)arg2>*(ElementType*)arg1)
    return -1;
}
 
 
 

猜你喜欢

转载自blog.csdn.net/qq_43269246/article/details/83512434