65,189
社区成员




#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int int_compare(const void* a, const void* b)
{
return (*(int*)a - * (int*)b);
}
int char_compare(const void* a, const void* b)
{
return (*(char*)a - * (char*)b);
}
void int_sort(int* beg , int* end)
{
qsort(beg, end - beg , sizeof(int), int_compare);
}
void char_sort(char* beg , char* end)
{
qsort(beg, end - beg , sizeof(char), char_compare);
}
int main()
{
int arry[] = {23, 45, 56, 2, 56, 1, 3, 76, 89, 343}; //数字任意
int_sort(arry, arry + 10);
for (int n = 0; n != 10; n++)
printf("%d ", arry[n]);
char str[] = "int char_compare(const void* a, const void* b)";
char_sort(str, str + strlen(str));
printf("\n%s \n", str);
return 0;
}
#include <functional>
#include <algorithm>
int main(int argc, char* argv[])
{
int arry[]={23,45,56,2,56,1,3,76,89,343};//数字任意
std::sort(&arry[0], &arry[sizeof(arry) / sizeof(arry[0])] , std::greater<int>());
return 0;
}
#include <algorithm>
int main(int argc, char* argv[])
{
int arry[]={23,45,56,2,56,1,3,76,89,343};//数字任意
std::sort(&arry[0], &arry[sizeof(arry) / sizeof(arry[0])]);
return 0;
}
/* qsort example */
#include <stdio.h>
#include <stdlib.h>
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, 6, sizeof(int), compare);
for (n=0; n<6; n++)
printf ("%d ",values[n]);
return 0;
}