任意类型通用的qsort冒泡排序
来源:互联网 发布:java实用教程第四版pdf 编辑:程序博客网 时间:2024/06/05 02:30
代码实现(Visual Studio 2017)
#include <stdio.h>#include <windows.h>static int int_cmp(void* p1, void* p2)//int型比较函数{ return *((int*)p1) > *((int*)p2);}static int float_cmp(void* p1, void* p2)//float型比较函数{ return *((float*)p1) > *((float*)p2);}void swap(void *p1, void *p2, int size)//任意类型通用交换函数{ char *p = (char *)p1; char *q = (char *)p2; while (size--) { *p ^= *q; *q ^= *p; *p ^= *q; q++; p++; }}void MySort(void *base, int count, int size, int(*cmp)(void *p1, void *p2))//任意类型通用排序{ int i = 0; int j = 0; int flag = 0; for (; i < count-1; i++) { flag = 0; for (j = 0; j < count - 1 -i; j++) { if (cmp( (char*)base+j*size , (char*)base + (j+1)*size ) > 0) { flag = 1; swap((char*)base + j*size, (char*)base + (j + 1)*size,size); } } if (flag == 0) break; }}void show_int(int *arr,int count)//int型输出函数{ for (int i = 0; i < count; i++) { printf("%d ", arr[i]); } printf("\n");}void show_float(float *arr, int count)//float型输出函数{ for (int i = 0; i < count; i++) { printf("%f ", arr[i]); } printf("\n");}//主程序int main(){ int arr1[10] = { 5,45,32,2,0,89,456,21,3556,51 }; float arr2[10] = { 5.1,45.5,12.5,100.9,100.1,100.54,456.1,12.2,3556.0,45.2 }; int count1 = sizeof(arr1) / sizeof(arr1[0]); int count2 = sizeof(arr2) / sizeof(arr2[0]); MySort(arr1, count1, sizeof(arr1[0]), int_cmp); MySort(arr2, count2, sizeof(arr2[0]), float_cmp); show_int(arr1,count1); show_float(arr2,count2); system("pause"); return 0;}
阅读全文
0 0
- 任意类型通用的qsort冒泡排序
- 通用类型的冒泡排序
- 模仿qsort的功能实现一个通用的冒泡排序。
- 通用冒泡排序法qsort的用法与其模拟实现
- C语言——通用的冒泡排序(qsort)
- 【C】模仿 qsort 的功能实现一个通用的冒泡排序
- (第三季)205-int类型的冒泡排序 206-拓展的通用的冒泡排序方法
- 有关qsort的应用以及冒泡排序的实现
- 模仿qsort()实现一个通用的排序函数:msort()
- 用冒泡排序的方法模拟实现qsort函数
- 冒泡排序函数,可以给任意类型的数据排序,比如整形数组,字符数组,字符串数组等
- c++::写一个通用的冒泡排序
- 写一个通用的冒泡排序函数
- 通用冒泡排序
- 通用冒泡排序算法
- 通用冒泡排序算法
- 通用冒泡排序算法
- 仿照qsort函数的形式编写简单选择排序,冒泡排序,希尔排序函数
- 基于.NET平台常用的框架整理
- mysql中的文件路径书写
- 将windows socket 初始化接口封装成一个类,方便好用
- 阿里NIPS 2017论文解读:如何降低TensorFlow训练的显存消耗?
- [知了堂学习笔记]_JQuery入门
- 任意类型通用的qsort冒泡排序
- 使用宏实现两个数中求较大值
- 踩坑筆記 —— ThinkPHP 框架下微信支付
- [CS231n] 第一讲:课程介绍
- 新巴巴运动网项目:SSM(Spring+SpringMVC+mybatis)框架的配置
- js进阶提高篇
- RDP 远程连接失败,禁用 FIPS
- java组合
- FZOJ1095 N皇后