产生随机数:
#include
#include
#include
#include
#include "generate.h"
void generate(int *data,int size)
{
int i = 0;
int j = 0;
int number = 0;
srand(time(NULL));
for(i = 0;i < NUM;i++)
{
number = rand()%UPPER_LIMIT;
data[i] = number;
}
}
#ifndef __GENERATE_H
#define __GENERATE_H
#define LOWER_LIMIT 0
#define UPPER_LIMIT 100
#define NUM 20
void generate(int *data,int size);
#endif
排序函数:
#include
#include
#include
#include
static int pow1(int k,int n)
{
int num = 1;
while(n--)
{
num *= k;
}
return num;
}
int rxsort(int *data,int size,int p,int k)
{
int *count = NULL;
int *temp = NULL;
int pval = 0;
int index = 0;
int i = 0;
int n = 0;
if((count = (int *)malloc(k * sizeof(int))) == NULL)
{
printf("Error:Allocate storage for count!");
return -1;
}
if((temp = (int *)malloc(size * sizeof(int))) == NULL)
{
printf("Error:Allocate storage for sorted elements!");
free(count);
return -1;
}
for(n = 0;n < p;n++)
{
pval = pow1(k,n);
for(i = 0;i < k;i++)
{
count[i] = 0;
}
for(i = 0;i < size;i++)
{
index = (data[i]/pval)%k;
count[index] += 1;
}
for(i = 1;i < k;i++)
{
count[i] = count[i] + count[i-1];
}
for(i = size - 1;i >= 0;i--)
{
index = (int)(data[i]/pval)%k;
temp[count[index] - 1] = data[i];
count[index] -= 1;
}
memcpy(data,temp,size * sizeof(int));
}
free(count);
free(temp);
return 0;
}
#ifndef __TXSORT_H
#define __TXSORT_H
int rxsort(int *data,int size,int p,int k);
#endif
main函数:
#include
#include "generate.h"
#include "rxsort.h"
int main()
{
int array[NUM] ={0};
int i = 0;
generate(array,NUM);
printf("Before sort:\n");
for(i=0;i
printf("%d ",array[i]);
printf("\n");
rxsort(array,NUM,3,10);
printf("After sort:\n");
for(i=0;i
printf("%d ",array[i]);
printf("\n");
}
MAKEFILE文件:
main:main.o generate.o rxsort.o
cc -o main main.o generate.o rxsort.o
main.o:main.c generate.h rxsort.h
cc -c main.c
generate.o:generate.c generate.h
cc -c generate.c
rxsort.o:rxsort.c rxsort.h
cc -c rxsort.c
clean:
rm *.o -rf
clear:
rm *.o main -rf
阅读(978) | 评论(0) | 转发(0) |