#include
#include
void bubble_sort(int *arr,int sz)
{
int i = 0;
int j = 0;
for (i = 0; i < sz - 1; i++)
{
for (j = 0; j < sz - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
int tmp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = tmp;
}
}
}
}
void print_sort(int* arr, int sz)
{
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%3d ", arr[i]);
}
printf("\n");
}
int main()
{
int arr[] = { 2,5,6,8,14,65,98,63,0,4 };
int n = sizeof(arr) / sizeof(int);
print_sort(arr, n);
bubble_sort(arr, n);
print_sort(arr, n);
system("pause");
return 0;
}
库函数:void qsort(void* base,size_t num,size_t width,int(__cdecl* compare)(const void* elem1, const void* elem2));
整数
#include
#include
int cmp_int(const void* e1, const void* e2)
{
return *(int *)e1 - *(int *)e2;
}
void print_sort(int* arr, int sz)
{
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%3d ", arr[i]);
}
printf("\n");
}
int main()
{
int arr[] = { 2,5,6,8,14,65,98,63,0,4 };
int n = sizeof(arr) / sizeof(int);
print_sort(arr, n);
qsort(arr,n, sizeof(int),cmp_int);
print_sort(arr, n);
system("pause");
return 0;
}
结构体
#include
#include
#include
struct S
{
char name[20];
int age;
};
//int s_b_name(const char* e1, const char* e2)
//{
// return strcmp(((struct S*)e1)->name, ((struct S*)e2)->name);
//}
int s_b_age(const char* e1, const char* e2)
{
return ((struct S*)e1)->age-((struct S*)e2)->age;
}
void output(struct S s[], int n)
{
int i = 0;
for(i=0;i<n;i++)
{
printf("%d ", s[i].age);
}
printf("\n");
return;
}
//void test()
//{
// struct S arr[] = { {"zhangsan",15},{"lisi",35},{"wangwu",20} };
// output(arr, sizeof(arr) / sizeof(arr[0]));
// qsort(arr, sizeof(arr) / sizeof(arr[0]), sizeof(struct S), s_b_name);
// output(arr, sizeof(arr) / sizeof(arr[0]));
//}
void test1()
{
struct S arr[] = { {"zhangsan",15},{"lisi",35},{"wangwu",20} };
output(arr, sizeof(arr) / sizeof(arr[0]));
qsort(arr, sizeof(arr) / sizeof(arr[0]), sizeof(struct S), s_b_age);
output(arr, sizeof(arr) / sizeof(arr[0]));
}
int main()
{
/*test();*/
test1();
system("pause");
return 0;
}
整数排序:
#include
#include
void swap(char* buf1,char* buf2,int width)
{
int i = 0;
for (i = 0; i < width; i++)
{
int tmp = *buf2;
*buf2 = *buf1;
*buf1 = tmp;
buf2++;
buf1++;
}
}
int cmp_int(const void* e1, const void* e2)
{
return *(int*)e1 - *(int*)e2;
}
void bubble_sort(void *base,int num,int width,
int(* cmp)(const void* e1, const void* e2))
{
int i = 0;
int j = 0;
for (i = 0; i < num - 1; i++)
{
for (j = 0; j < num - i - 1; j++)
{
if (cmp((char *)base+j*width,(char*)base + (j+1) * width)>0)
{
swap((char*)base + j * width, (char*)base + (j + 1) * width,width);
}
}
}
}
void print_sort(int* arr,int sz)
{
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
void test1()
{
int arr[] = { 1,3,5,7,9,2,4,6,8,0 };
print_sort(arr, sizeof(arr) / sizeof(int));
bubble_sort(arr, sizeof(arr) / sizeof(int), sizeof(int), cmp_int);
print_sort(arr, sizeof(arr) / sizeof(int));
}
int main()
{
test1();
system("pause");
return 0;
}
#include
#include
struct S
{
char name[20];
int age;
};
void swap(char* buf1,char* buf2,int width)
{
int i = 0;
for (i = 0; i < width; i++)
{
int tmp = *buf2;
*buf2 = *buf1;
*buf1 = tmp;
buf2++;
buf1++;
}
}
int s_b_name(const char* e1, const char* e2)
{
return strcmp(((struct S*)e1)->name, ((struct S*)e2)->name);
}
//int s_b_age(const char* e1, const char* e2)
//{
// return ((struct S*)e1)->age-((struct S*)e2)->age;
//}
id bubble_sort(void *base,int num,int width,
int(* cmp)(const void* e1, const void* e2))
{
int i = 0;
int j = 0;
for (i = 0; i < num - 1; i++)
{
for (j = 0; j < num - i - 1; j++)
{
if (cmp((char *)base+j*width,(char*)base + (j+1) * width)>0)
{
swap((char*)base + j * width, (char*)base + (j + 1) * width,width);
}
}
}
}
tput(struct S s[], int n)
{
int i = 0;
for(i=0;i<n;i++)
{
printf("%s ", s[i].name);
}
printf("\n");
}
test2()
{
struct S arr[] = { {"zhangsan",15},{"lisi",35},{"wangwu",20} };
output(arr, sizeof(arr) / sizeof(arr[0]));
qsort(arr, sizeof(arr) / sizeof(arr[0]), sizeof(struct S), s_b_name);
output(arr, sizeof(arr) / sizeof(arr[0]));
}
int main()
{
test2();
system("pause");
return 0;
}