利用冒泡排序法模拟qsort函数

利用冒泡排序法模拟qsort函数,列如对int数组排序,struct数组排序,字符串排序,
<span style="font-family: Arial, Helvetica, sans-serif;">在以下函数参数里为了方便接收实参,形参给出了void *这种形式,</span><span style="font-family: Arial, Helvetica, sans-serif;">至于函数的实现部分必须要强制转换为需要的类型。</span>
<span style="font-family: Arial, Helvetica, sans-serif;"></span>
int Compare_int(const void* num1, const void* num2){
return *(int *)num1 - *(int *)num2;
}

int Compare_struct(const void* stu1, const void* stu2){
return ((pstu)stu1)->age - ((pstu)stu2)->age;
}

int Compare_str(const void* str1, const void* str2){
return ( strcmp (*(char**)str1, *(char**)str2) );
}
</pre><pre name="code" class="objc">
以下函数是为了交换满足要求的相邻元素。但是每个元素的具体类型并不知道,但可以通过每个元素的所占的字节数从而交换满足要求的相邻元素
void Swap(char *buf1, char *buf2, int width){
<span style="white-space:pre">	</span>int i = 0;
<span style="white-space:pre">	</span>for (i = 0; i < width; i++){
<span style="white-space:pre">		</span>char tmp = buf1[i];
<span style="white-space:pre">		</span>buf1[i] = buf2[i];
<span style="white-space:pre">		</span>buf2[i] = tmp;
<span style="white-space:pre">	</span>}
}

//利用冒泡排序法模拟qsort函数函数指针com参数为元素的首地址,Swap函数将满足要求的数据进行交换,
void my_qsort(void*base,int size,int width,int(*com)(const void*, const void*)){
<span style="white-space:pre">	</span>int i = 0, j = 0;
<span style="white-space:pre">	</span>for (i = 0; i < size - 1; i++){
<span style="white-space:pre">		</span>for (j = 0; j < size - 1 - i; j++){
<span style="white-space:pre">			</span>if (com((char *)base + j*width, (char *)base + (j + 1)*width)>0)
<span style="white-space:pre">			</span>{
<span style="white-space:pre">				</span>Swap((char *)base + j*width, (char *)base + (j + 1)*width, width);
<span style="white-space:pre">			</span>}
<span style="white-space:pre">		</span>} 
<span style="white-space:pre">	</span>}
}

//代码如下
<pre name="code" class="objc">#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct Stu{
	char name[10];
	int age;
	double score;
}stu,*pstu;
int Compare_int(const void* num1, const void* num2){
	return *(int *)num1 - *(int *)num2;
}

int Compare_struct(const void* stu1, const void* stu2){
	return ((pstu)stu1)->age - ((pstu)stu2)->age;

}

int Compare_str(const void* str1, const void* str2){
	return ( strcmp (*(char**)str1, *(char**)str2) );
}

void Swap(char *buf1, char *buf2, int width){
	int i = 0;
	for (i = 0; i < width; i++){
		char tmp = buf1[i];
		buf1[i] = buf2[i];
		buf2[i] = tmp;
	}
}

void my_qsort(void*base,int size,int width,int(*com)(const void*, const void*)){
	int i = 0, j = 0;
	for (i = 0; i < size - 1; i++){
		for (j = 0; j < size - 1 - i; j++){
			if (com((char *)base + j*width, (char *)base + (j + 1)*width)>0)
			{
				Swap((char *)base + j*width, (char *)base + (j + 1)*width, width);
			}
		} 
	}
}
int main(){
	int arr1[] = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 0 };
	int int_sz = sizeof(arr1) / sizeof(arr1[0]);
	my_qsort(arr1, int_sz, sizeof(int), Compare_int);
	for (int i = 0; i < int_sz; i++)
		printf("%-3d", arr1[i]);
	printf("\n");
	
	stu stu1[] = { { "zhangsan", 12, 56 }, { "lisi", 9, 78 }, { "wangmazi", 14, 67 } };
	int struct_sz = sizeof(stu1) / sizeof(stu1[0]);
	my_qsort(stu1, struct_sz, sizeof(stu), Compare_struct);
	for (int i = 0; i < struct_sz; i++)
		printf("%-10s,%2d,%15f\n", stu1[i].name, stu1[i].age, stu1[i].score);
	
	char *str1[] = {"aaaa","ccccc","bbbbbb","ddddd"};
	int str_sz = sizeof(str1) / sizeof(str1[0]);
	my_qsort(str1, str_sz, sizeof(char *), Compare_str);
	for (int i = 0; i < str_sz; i++)
		printf("%s\n",str1[i]);
	system("pause");
	return 0;
}



                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
冒泡排序和快速排序(qsort)虽然都是常见的排序算法,但它们的工作原理和效率有很大的不同。冒泡排序是一种简单的比较排序算法,而快速排序则是一种高效的分治策略。如果你想了解如何模仿`qsort`实现冒泡排序,其实这不是一个直接的对应,因为`qsort`是基于分治法的高效排序,而冒泡排序更适合教学和理解基本排序原理。 不过,为了满足你的要求,我们可以探讨一下如何用类似`qsort`的方式设计一个冒泡排序的“迭代”版本,虽然这并不是真正意义上的模仿,因为它们在实现上并不相同。 **冒泡排序的简化版(不是真正的`qsort`)**: 1. 定义一个辅助函数,类似于`qsort`的分区过程,但仅用于比较相邻元素并交换: ```c++ void bubbleSortPartition(int arr[], int low, int high) { while (low < high) { if (arr[low] > arr[high]) { std::swap(arr[low], arr[high]); } low++; high--; } } ``` 2. 用递归调用的方式实现冒泡排序: ```c++ void bubbleSortIterative(int arr[], int size) { for (int i = 0; i < size - 1; i++) { bubbleSortPartition(arr, 0, size - 1 - i); } } ``` 这里我们不是直接将整个数组作为一次排序,而是每次缩小待排序范围,直到整个序列有序。 **相关问题--:** 1. 冒泡排序与快速排序的主要区别是什么? 2. 在冒泡排序中,为什么要使用`bubbleSortPartition`函数? 3. 如何评价这种将冒泡排序与`qsort`风格结合的简化版本?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值