# qsort 排序

``````#include <stdio.h>
#include <stdlib.h>

int Compare(void* elem1,void* elem2)
{
return (*(int *)elem1)-(*(int*)elem2);
}
void Print(int * arr,int sz)
{
int i = 0;
for (i=0; i<sz; i++)
{
printf("%d ",arr[i]);
}
printf("\n");
}
int  main()
{
int arr[] = {2,9,8,6,5,1,3,4,7};
int sz = sizeof(arr)/sizeof(arr[0]);
int elem_sz = sizeof(int);
Print(arr,sz);
qsort(arr,sz,elem_sz,Compare);
Print(arr,sz);

return 0;
}

#include <stdio.h>
#include <stdlib.h>

int compare(void* elem1,void* elem2)
{
return (*(float*)elem1)>(*(float*)elem2)?1:-1;
}

void Print(float* arr,int sz)
{
int i = 0;
for (i=0; i<sz; i++)
{
printf("%f ",arr[i]);
}
printf("\n");
}
int main()
{
float arr[] = {1.2,0.1,3.2,2.3,1.4,0.3,2.8,3.0,4.0};
int sz = sizeof(arr)/sizeof(arr[0]);
int elem_sz = sizeof(float);
Print(arr,sz);
qsort(arr,sz,elem_sz,&compare);
Print(arr,sz);
return 0;
}

#include <stdio.h>
#include <stdlib.h>

int ccmp(void* elem1,void* elem2)
{
return strcmp(*(char**)elem1,*(char**)elem2);
}

void Print(float** arr,int sz)
{
int i = 0;
for (i=0; i<sz; i++)
{
printf("%s ",arr[i]);
}
printf("\n");
}

int main()
{
char* arr[] = {"ccccc","bbbbb","aaaaa"};
int sz = sizeof(arr)/sizeof(arr[0]);
int elem_sz = sizeof(char*);
Print(arr,sz);
qsort (arr,sz,elem_sz,&ccmp);
Print(arr,sz);

return 0;
}
``````