qsort函数的模拟实现以及练习使用


//20.6:练习使用qsort函数排序各种类型的数据。 
/*qsort 的函数原型是
void qsort(void*base, size_t num, size_t width, int(__cdecl*compare)(const void*, const void*));
各参数:1 待排序数组首地址 2 数组中待排序元素数量
3 各元素的占用空间大小 4 指向函数的指针
其中base是排序的一个集合数组,num是这个数组元素的个数,width是一个元素的大小,comp是一个比较函数。
比如:对一个长为1000的数组进行排序时,int a[1000]; 那么base应为a,num应为 1000,width应为 sizeof(int),comp函数随自己的命名。
qsort(a, 1000, sizeof(int), comp);*/
#define _CRT_SECURE_NO_WARNINGS 1  
#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#pragma warning(disable:4996)
int cmp_int(const void *x, const void *y)//升序,降序将return 1改为-1,return -1改为1
{
	int *_x = (int *)x;
	int *_y = (int *)y;
	if (*_x > *_y)
	{
		return 1;
	}
	else if (*_x < *_y)
	{
		return -1;
	}
	else{
		return 0;
	}
}
int cmp_char(const void *x, const void *y)
{
	char *_x = (char*)x;
	char *_y = (char*)y;
	if (*_x > *_y)
	{
		return 1;
	}
	else if (*_x < *_y)
	{
		return -1;
	}
	else{
		return 0;
	}
}
int  cmp_float(const void *x, const void *y)
{
	float *_x = (float *)x;
	float *_y = (float *)y;
	if (*_x > *_y)
	{
		return 1;
	}
	else if (*_x < *_y)
	{
		return -1;
	}
	else{
		return 0;
	}
}
int cmp_double(const void *x, const void *y)
{
	double *_x = (double *)x;
	double *_y = (double *)y;
	if (*_x > *_y)
	{
		return 1;
	}
	else if (*_x < *_y)
	{
		return -1;
	}
	else{
		return 0;
	}
}
int main()
{
	int i = 0;
	int arr1[] = { 12, 32, 5, 6, 4, 78, 984, 85, 21, 56 };
	char str[] = {'a','c','b','y','d'};
	float arr2[] = { 1.256, 36.2598, 56.6544, 85.2853, 96.3211, 94.28951, 51.28743, 8.2196, 9.98564, 41.654156 };
	double arr3[] = { 5.68945648, 7.89455555, 6.542595871, 3.58424569, 5.26598695 };
	
	int size1 = sizeof(arr1) / sizeof(arr1[0]);
	int size2 = sizeof(str) / sizeof(str[0]);
	int size3 = sizeof(arr2) / sizeof(arr2[0]);
	int size4 = sizeof(arr3) / sizeof(arr3[0]);
	qsort(arr1, size1, sizeof(int), cmp_int);
	qsort(str, size2, sizeof(char), cmp_char);
	qsort(arr2, size3, sizeof(float), cmp_float);
	qsort(arr3, size4, sizeof(double), cmp_double);
	system("pause");
	return 0;
}

最初的值


最后运行之后的值(升序)


最后运行之后的值(降序)


//7.模仿qsort的功能实现一个通用的冒泡排序。
//qsort 的函数原型是
//void qsort(void*base, size_t num, size_t width, int(__cdecl*compare)(const void*, const void*));
#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<assert.h>
#pragma warning(disable:4996)
int cmp_int(const void * x, const void * y)
{
	int *_x = (int*)x;
	int *_y = (int*)y;
	if (*_x > *_y)
	{
		return 1;
	}
	else if (*_x < *_y)
	{
		return -1;
	}
	else{
		return 0;
	}
}
int cmp_string(const void * x, const void * y)
{
	if (strcmp(*(char **)x, *(char**)y))
	{
		;
	}
}
void _swap(char * x, char * y, int size)
{
	while (size--)
	{
		*x ^= *y;
		*y ^= *x;
		*x ^= *y;
		x++;
		y++;
	}
}
void bubble1(void * arr, int count, int size, int(*cmp)(void *, void *))
{
	assert(arr);
	assert(cmp);
	int i = 0; 
	int j = 0;
	for (i=0; i < count; i++)
	{
		int flage = 0;
		for (j=0; j < count - 1 - i; j++)
		{
			if (cmp((char *)arr + j * size, (char*)arr + (j + 1) * size)>0)//相邻两个元素的比较
			{
				flage = 1;
				_swap((char *)arr + j * size, (char*)arr + (j + 1) * size, size);//按字节交换
			}
		}
		if (flage == 0)
		{
			break;
		}
	}
}
void bubble2(void * str, int count, int size, int(*cmp)(void *, void *))
{
	assert(str);
	assert(cmp);
	int i = 0;
	int j = 0;
	for (i = 0; i < count; i++)
	{
		int flage = 0;
		for (j = 0; j < count - 1 - i; j++)
		{
			if (cmp((char *)str + j * size, (char*)str + (j + 1) * size)>0)//相邻两个元素的比较
			{
				flage = 1;
				_swap((char *)str + j * size, (char*)str + (j + 1) * size, size);//按字节交换
			}
		}
		if (flage == 0)
		{
			break;
		}
	}
}
int main()
{
	int arr[] = { 1, 58, 6, 94, 21, 3, 9, 100, 5987, 4 };
	char *str[] = { "aaaaa", "ddddd", "bbbbb", "ccccc" };
	int i = 0;
	int size1 = sizeof(arr) / sizeof(arr[0]);
	int size2 = sizeof(str) / sizeof(str[0]);
	bubble1(arr, size1, sizeof(int), cmp_int);
	bubble2(str, size2, sizeof(char*), cmp_string);
	for (; i < size1; i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");
	for (i=0; i < size2; i++)
	{
		printf("%s  ", str[i]);
	}
	system("pause");
	return 0;
}


猜你喜欢

转载自blog.csdn.net/pigdwh/article/details/80394891