微库版本实现: #include"stdlib.h" int cmpfunc( const void* a, const void* b ) { float da = *( float* )a; float db = *( float* )b; float tmp = da - db; if( tmp > 0.0001 ) return 1; else return ( -1 ); } qsort( MYPressure_float1, 30, sizeof( MYPressure_float1[0] ), cmpfunc ); 堆栈一定要给充足不然容易进入段错误 个人版本实现: #include <QCoreApplication>
#include <string.h>
#include <QTime>
#include <QMutex>
QMutex mutex;
#define SORTINT
void Bubble_sort(void *dst,const void *src,unsigned int size,unsigned int datasize)
{
unsigned int i=0,j=0;
int tmp=0;
#ifdef SORTCHAR
char *tmpdst=(char *)dst;
#endif
#ifdef SORTINT
int *tmpdst=(int *)dst;
#endif
mutex.lock();
memcpy(dst,src,datasize*size);
for(i=0;i<(size-1);i++)
{
for(j=0;j<(size-1-i);j++)
{
if(tmpdst[j]>tmpdst[j+1])
{
tmp=tmpdst[j+1];
tmpdst[j+1]=tmpdst[j];
tmpdst[j]=tmp;
}
}
}
mutex.unlock();
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
int data[1000]={0,1514,45,16,78,59,2,45,35,14,20,3,6,2,1,1,8,9,45,65,85,25,37};
int dy[1000]={0};
int i=0;
qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
for(i=0;i<1000;i++)
{
data[i]=qrand()%1000;
}
Bubble_sort(dy,data,1000,4);
for(i=0;i<1000;i++)
printf("%ld %ld \n",i,dy[i]);
return a.exec();
}