ランキング関数 使用例



[ 簡単な説明 ]

ランキング関数の使用例です。
100個のデータ(No.0〜99)のNo.50のデータを抽出しています。

テストデータ作成のため、ランダム・データ生成関数、一様乱数生成関数、シャッフル関数等を実装しています。

プログラム・ソース("test3.c")           top (先頭に戻る)
/*		test3.c		*/
#include <stdio.h>
#include <stdlib.h>
#include "sort.h"

static long _idum_o1 = 987612345L;

double ornd1(void);
int irnd(int max, int min);
int *mkdata_i(int n, int min, int max);
double *mkdata_d(int n, double min, double max);
uchar **mkdata_s(int n, int smin, int smax, uchar min, uchar max);
uchar **mkdata_sd(int n, int smin, int smax, uchar min, uchar max);
void shuffle_c(uchar **a, int n);

int main(void)
{
	int i, n;
	int *data_i;
	double *data_d;
	uchar **data_s;

	n = 100;

	data_i = mkdata_i(n, 0, 299);
	printf("rank_i : result=%d\n", rank_i(data_i, n, 50));
	qsort_i(data_i, n);
	for(i = 0; i < n; i++)
	{
		printf("%2d:%3d  ", i, *(data_i + i));
		if(i % 10 == 9)	putchar('\n');
	}
	putchar('\n');

	data_d = mkdata_d(n, 0.,299.);
	printf("rank_d : result=%3.0f\n", rank_d(data_d, n, 50));
	qsort_d(data_d, n);
	for(i = 0; i < n; i++)
	{
		printf("%2d:%3.0f  ", i, *(data_d + i));
		if(i % 10 == 9)	putchar('\n');
	}
	putchar('\n');

	data_s = mkdata_sd(n, 3, 3, 'a', 'z');
	printf("rank_c : result=%s\n", rank_c(data_s, n, 50));
	qsort_c(data_s, n);
	for(i = 0; i < n; i++)
	{
		printf("%2d:%3s  ", i, *(data_s + i));
		if(i % 10 == 9)	putchar('\n');
	}
	putchar('\n');
	return 1;
}

int *mkdata_i(int n, int min, int max)
{
	int i, *data, *p;

	if(n < 1 || max < min)
	{
		fprintf(stderr, "Error : illegal input  in mkdata_i()\n");
		fprintf(stderr, "    n=%d, min=%d, max=%d\n", n, min, max);
		return NULL;
	}
	data = (int *)malloc(n * sizeof(int));
	if(data == NULL)
	{
		fprintf(stderr, "Error : out of memory  in mkdata_i()\n");
		return NULL;
	}
	for(i = 0, p = data; i < n; i++)	*p++ = irnd(max, min);
	return data;
}

double *mkdata_d(int n, double min, double max)
{
	int i;
	double *data, *p;

	if(n < 1 || max < min)
	{
		fprintf(stderr, "Error : illegal input  in mkdata_d()\n");
		fprintf(stderr, "    n=%d, min=%e, max=%e\n", n, min, max);
		return NULL;
	}
	data = (double *)malloc(n * sizeof(double));
	if(data == NULL)
	{
		fprintf(stderr, "Error : out of memory  in mkdata_d()\n");
		return NULL;
	}
	for(i = 0, p = data; i < n; i++)	*p++ = min + (max - min) * ornd1();
	return data;
}

uchar **mkdata_s(int n, int smin, int smax, uchar min, uchar max)
{
	int i, *len, *lp;
	uchar **data, **p, *pp;

	if(n < 1 || max < min || smax < smin || smin < 1)
	{
		fprintf(stderr, "Error : illegal input  in mkdata_s()\n");
		fprintf(stderr, "    n=%d, smin=%d, smax=%d, min=%u, max=%u\n",
			n, smin, smax, min, max);
		return NULL;
	}
	data = (uchar **)malloc(n * sizeof(uchar *));
	len = mkdata_i(n, smin, smax); 
	if(data == NULL || len == NULL)
	{
		fprintf(stderr, "Error : out of memory  in mkdata_s()\n");
		return NULL;
	}
	for(i = 0, p = data, lp = len; i < n; i++, lp++)
	{
		pp = *p++ = (uchar *)malloc((smax + 1) * sizeof(uchar *));
		while((*lp)--)	*pp++ = irnd(max, min);
		*pp = '\0';
	}
	free((uchar *)len);
	return data;
}

double ornd1(void)					/* Park and Miller の「最低基準」乱数 */
{
	long w;
	double d = 1. / 2147483647.;

	w = _idum_o1 / 44488L;
	if((_idum_o1 = (_idum_o1 - w * 44488L) * 48271L - w * 3399) <= 0)
		_idum_o1 += 2147483647L;
	return (double)_idum_o1 * d;
}

int irnd(int max, int min)
{
	return min + (int)(ornd1() * (double)(max - min + 1));
}

uchar **mkdata_sd(int n, int smin, int smax, uchar min, uchar max)
{
	uchar **data, **dp, *dpp; 
	int i, len, nn;

	data = mkdata_s(n, smin, smax, min, max);
	while(1)
	{
		qsort_c(data, n);
		if((nn = n - dbldel_c(data, n, 1)) == 0)	break;
		dp = data;
		while(nn--) 
		{
			while(**dp)	dp++;
			len = irnd(smax, smin);
			dpp = *dp;
			while(len--)	*dpp++ = irnd(max, min);
			*dpp = '\0';
		} 
	}
	shuffle_c(data, n);
	return data;
}

void shuffle_c(uchar **a, int n)
{
	int i, m;
	uchar **p, **q, *w;

	for(i = 0, m = n * 5; i < m; i++)
	{
		p = a + (int)(ornd1() * n);
		q = a + (int)(ornd1() * n);
		w = *p;
		*p = *q;
		*q = w;
	}
}

出力結果           top (先頭に戻る)
rank_i : result=172
 0:  0   1: 11   2: 13   3: 14   4: 19   5: 38   6: 41   7: 47   8: 51   9: 54  
10: 55  11: 56  12: 56  13: 62  14: 65  15: 65  16: 65  17: 67  18: 68  19: 82  
20: 82  21: 85  22: 85  23: 97  24: 99  25:107  26:109  27:112  28:113  29:114  
30:115  31:115  32:121  33:135  34:135  35:136  36:138  37:146  38:147  39:149  
40:150  41:150  42:151  43:151  44:160  45:169  46:171  47:171  48:171  49:172  
50:172  51:172  52:176  53:176  54:179  55:179  56:183  57:183  58:185  59:190  
60:193  61:195  62:196  63:200  64:203  65:203  66:205  67:210  68:212  69:213  
70:216  71:219  72:223  73:225  74:226  75:229  76:229  77:232  78:232  79:236  
80:237  81:238  82:239  83:247  84:249  85:250  86:253  87:254  88:256  89:265  
90:267  91:268  92:272  93:275  94:277  95:280  96:286  97:292  98:294  99:295  

rank_d : result=165
 0:  0   1:  2   2:  4   3:  6   4:  9   5: 13   6: 13   7: 17   8: 17   9: 18  
10: 24  11: 25  12: 28  13: 31  14: 31  15: 40  16: 43  17: 46  18: 50  19: 50  
20: 51  21: 51  22: 53  23: 54  24: 55  25: 63  26: 64  27: 64  28: 64  29: 69  
30: 72  31: 77  32: 77  33: 87  34: 99  35: 99  36:116  37:121  38:125  39:127  
40:129  41:133  42:136  43:136  44:145  45:146  46:148  47:150  48:156  49:162  
50:165  51:171  52:172  53:173  54:175  55:175  56:184  57:194  58:196  59:197  
60:198  61:201  62:204  63:212  64:213  65:213  66:213  67:215  68:220  69:223  
70:224  71:224  72:224  73:224  74:225  75:231  76:234  77:237  78:242  79:242  
80:248  81:251  82:253  83:254  84:256  85:258  86:258  87:262  88:263  89:266  
90:267  91:268  92:269  93:269  94:270  95:285  96:285  97:292  98:293  99:293  

rank_c : result=nxl
 0:abh   1:bbw   2:bna   3:bqb   4:bxa   5:bzv   6:dav   7:dij   8:diq   9:dnl  
10:drv  11:dur  12:dzq  13:ebf  14:etz  15:evm  16:fdi  17:fdm  18:fgf  19:fin  
20:fta  21:fzb  22:giq  23:gyg  24:gyh  25:hcq  26:hey  27:hfz  28:hlt  29:ibo  
30:ilp  31:izo  32:jgq  33:kmv  34:knh  35:kvd  36:lcf  37:lgg  38:lhi  39:lhl  
40:lhw  41:mbl  42:mix  43:mkd  44:mmw  45:mok  46:mxn  47:mzm  48:nam  49:nti  
50:nxl  51:nyu  52:oai  53:ojc  54:okv  55:opz  56:otm  57:par  58:pwv  59:qjn  
60:qjo  61:rfl  62:rmn  63:rue  64:rwi  65:srl  66:srr  67:ulr  68:upv  69:utl  
70:uxd  71:vaw  72:vev  73:voe  74:vtr  75:vwk  76:wfz  77:wgw  78:wio  79:wnh  
80:wpl  81:wse  82:wuw  83:wxa  84:xgk  85:xku  86:xkx  87:xzu  88:ycv  89:ygg  
90:ykm  91:ylc  92:yry  93:zaz  94:ziq  95:zoe  96:zrc  97:zrw  98:zry  99:zvp