C数学库中的中值函数?

C数学库中的中值函数?,c,C,C库中是否有计算“n”个数字的中值的数学函数?要获得中值,您可以对数字数组进行排序,然后取: 1) 如果项目数量为奇数-中间的数字 2) 如果项目数为偶数-中间两个数字的平均值否,则标准C库中没有中值函数。否,标准C库中没有此类函数 但是,您可以实现一个(或者可以在线找到代码)。寻找中值的一种有效的O(n)算法称为“选择算法”,它与快速排序有关。阅读所有相关内容。要使用标准C库计算中值,请使用标准库函数qsort(),然后取中间元素。如果数组是a且具有n元素,则: qsort(a, n, siz

C库中是否有计算“n”个数字的中值的数学函数?

要获得中值,您可以对数字数组进行排序,然后取:

1) 如果项目数量为奇数-中间的数字


2) 如果项目数为偶数-中间两个数字的平均值

否,则标准C库中没有中值函数。

否,标准C库中没有此类函数


但是,您可以实现一个(或者可以在线找到代码)。寻找中值的一种有效的O(n)算法称为“选择算法”,它与快速排序有关。阅读所有相关内容。

要使用标准C库计算中值,请使用标准库函数
qsort()
,然后取中间元素。如果数组是
a
且具有
n
元素,则:

qsort(a, n, sizeof(a[0]), compare);
return a[n/2];

您必须编写自己的
compare
函数,该函数将取决于数组元素的类型。有关详细信息,请参阅
qsort
手册页或在Kernighan和Ritchie索引中查找。

常规方法:(如果您正在从事图像处理工作,则不建议使用此方法)

/*通过qsort示例的中值*/
#包括
#包括
#定义要素6
int值[]={40,10,100,90,20,25};
整数比较(常数无效*a,常数无效*b)
{
返回(*(int*)a-*(int*)b);
}
int main()
{
int n;
qsort(值、元素、sizeof(int)、比较);

对于(n=0;n关于
std::nth_元素
?如果我正确理解了中位数的性质,那么对于奇数个元素,这会给你一个中位数。

@Eli:简单往往胜过效率,我直觉认为这就是OPwants@catwalk:这很公平,但谨慎的做法是在回答中明确指出这是一个简单而不是有效的解决方案,上面的代码在偶数个元素的情况下会失败(它不会取中间两个元素的平均值)。
/* median through qsort example */
#include <stdio.h>
#include <stdlib.h>

#define ELEMENTS 6

int values[] = { 40, 10, 100, 90, 20, 25 };

int compare (const void * a, const void * b)
{
  return ( *(int*)a - *(int*)b );
}

int main ()
{
  int n;
  qsort (values, ELEMENTS, sizeof(int), compare);
  for (n=0; n<ELEMENTS; n++)
  {   printf ("%d ",values[n]); }
  printf ("median=%d ",values[ELEMENTS/2]);
  return 0;
}
//===================== Method 1: =============================================
//Algorithm from N. Wirth’s book Algorithms + data structures = programs of 1976    

typedef int_fast16_t elem_type ;

#ifndef ELEM_SWAP(a,b)
#define ELEM_SWAP(a,b) { register elem_type t=(a);(a)=(b);(b)=t; }

elem_type kth_smallest(elem_type a[], uint16_t n, uint16_t k)
{
    uint64_t i,j,l,m ;
    elem_type x ;
    l=0 ; m=n-1 ;
    while (l<m) {
    x=a[k] ;
    i=l ;
    j=m ;
    do {
    while (a[i]<x) i++ ;
    while (x<a[j]) j-- ;
    if (i<=j) {
    ELEM_SWAP(a[i],a[j]) ;
    i++ ; j-- ;
    }
    } while (i<=j) ;
    if (j<k) l=i ;
    if (k<i) m=j ;
    }
    return a[k] ;
}

    #define wirth_median(a,n) kth_smallest(a,n,(((n)&1)?((n)/2):(((n)/2)-1)))

//===================== Method 2: =============================================
//This is the faster median determination method.
//Algorithm from Numerical recipes in C of 1992

elem_type quick_select_median(elem_type arr[], uint16_t n)
{
    uint16_t low, high ;
    uint16_t median;
    uint16_t middle, ll, hh;
    low = 0 ; high = n-1 ; median = (low + high) / 2;
    for (;;) {
    if (high <= low) /* One element only */
    return arr[median] ;
    if (high == low + 1) { /* Two elements only */
    if (arr[low] > arr[high])
    ELEM_SWAP(arr[low], arr[high]) ;
    return arr[median] ;
    }
    /* Find median of low, middle and high items; swap into position low */
    middle = (low + high) / 2;
    if (arr[middle] > arr[high])
    ELEM_SWAP(arr[middle], arr[high]) ;
    if (arr[low] > arr[high])
    ELEM_SWAP(arr[low], arr[high]) ;
    if (arr[middle] > arr[low])
    ELEM_SWAP(arr[middle], arr[low]) ;
    /* Swap low item (now in position middle) into position (low+1) */
    ELEM_SWAP(arr[middle], arr[low+1]) ;
    /* Nibble from each end towards middle, swapping items when stuck */
    ll = low + 1;
    hh = high;
    for (;;) {
    do ll++; while (arr[low] > arr[ll]) ;
    do hh--; while (arr[hh] > arr[low]) ;
    if (hh < ll)
    break;
    ELEM_SWAP(arr[ll], arr[hh]) ;
    }
    /* Swap middle item (in position low) back into correct position */
    ELEM_SWAP(arr[low], arr[hh]) ;
    /* Re-set active partition */
    if (hh <= median)
    low = ll;
    if (hh >= median)
    high = hh - 1;
    }
    return arr[median] ;
}
#endif