使用qsort排序一个整型数组,一个浮点型数组,一个字符串数组

void show(const int *arr, int elems)
{
	assert(arr != NULL && elems > 0);
	int i = 0;
	for (; i < elems; i++){
		printf("%d ", arr[i]);
	}
	printf("\n");
}
int cmp(void *a, void*b)
{
	return *((int *)a) - *((int *)b);
}
int fcmp(void *a, void*b)
{
	return *((float *)a) - *((float *)b);

}
int ccmp(const void *a,const void*b)
{
	return strcmp(*(char **)a ,*(char **)b);
}
int ccmp(const void *str1, const void *str2)
{
	return strcmp(*(char**)str1, *(char**)str2);//str1 str2,都是元素的地址char**
}
int main()
{
	char *str[] = { "aaa", "bbb", "ccc", "ddd" };
	qsort(str,3,4,ccmp);
}
int main()
{
	//int arr[] = {23,12,3,23,2,23,23,34,45,667,78,78,78,67,0};
	//int elems = sizeof(arr)/sizeof(arr[0]);
	//int elem_size = sizeof(int);
	//qsort(arr, elems, elem_size, cmp);
	//show(arr, elems);
	/*float farr[] = { 1.12, 23.2, 45.23, 126.0, 21.9, 0 };
	int elems = sizeof(farr) / sizeof(farr[0]);
	int elem_size = sizeof(float);
	qsort(farr, elems, elem_size, fcmp);
	int i = 0;
	for (; i < elems; i++){
	printf("%f ", farr[i]);
	}*/
	char *carr[] = { "bbb", "ccc", "aaa", "zzz", "xxx" };
	int elems = sizeof(carr) / sizeof(carr[0]);
	int elem_size = sizeof(char*);
	qsort(carr, 5, 4, ccmp);
	int i = 0;
	for (; i < elems; i++){
		printf("%s ", carr[i]);
	}
	printf("\n");
	system("pause");
	return 0;
}


 
  

你可能感兴趣的:(c)