通用冒泡排序算法
来源:互联网 发布:三一重工 知乎 编辑:程序博客网 时间:2024/05/17 00:50
#include<stdio.h>#include<string.h>#if 0//冒泡排序算法void bubble_shot(int arr[],int len){ int i=0,j=0,temp=0; for(i=0;i<len-1;i++) for(j=0;j<len-i-1;j++) { if(arr[j]>arr[j+1]) { temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } }}int main(void){ int arr[10]={4,8,5,7,2,1,9,3,1,5}; int sz=sizeof(arr)/sizeof(arr[0]); int i; bubble_shot(arr,sz); for(i=0;i<sz;i++) printf("%d\t",arr[i]) ; return 0;}#endif#if 1typedef struct S{ int a; char name[10];}S;//交换函数(按字节交换)void temp(const void* s1,const void* s2,int width){ char* p1=(char*)s1; char* p2=(char*)s2; char ch; while(width--) { ch=*p1; *p1=*p2; *p2=ch; p1++; p2++; }}//通用冒泡排序算法实现部分(升序)void bubble_shot(void* arr,int len,int width,int (*p)(const void* s1,const void* s2)){ int i=0,j=0; for(i=0;i<len-1;i++) for(j=0;j<len-i-1;j++) { if(p((char*)arr+j*width,(char*)arr+(j+1)*width)>0) { temp((char*)arr+j*width,(char*)arr+(j+1)*width,width); } }}//int型比较函数(前》后--返回小于0的数,后《前---返回大于0的数,相等分返回0,后面的比较结果输出一样)int int_int(const void* s1,const void* s2){ return *(int*)s1-*(int*)s2;}//char型比较函数int char_char(const void* s1,const void* s2){ return strcmp((char*)s1,(char*)s2);}//结构体中按名字排序int s_s_name(const void* s1,const void* s2){ return strcmp(((S*)s1)->name, ((S*)s2)->name) ;}//结构体中按int排序int s_s_int(const void* s1,const void* s2){ return (((S*)s1)->a,((S*)s2)->a);}//测试主函数int main(void){ int arr[10]={4,8,5,7,2,1,9,3,1,5}; int sz=sizeof(arr)/sizeof(arr[0]); S s[3]={{10,"zhangsan"},{4,"lisi"},{12,"wangwu"}}; int i; int sz1=sizeof(s)/sizeof(s[0]); //定义函数指针 int (*p_int)(const void* s1,const void* s2)=&int_int; int (*p_char)(const void* s1,const void* s2)=&char_char; int (*p_s_s_name)(const void* s1,const void* s2)=&s_s_name; int (*p_s_s_int)(const void* s1,const void* s2)=&s_s_int; //开始比较调用函数 bubble_shot(arr,sz,sizeof(arr[0]),p_int); printf("按int排序\n"); for(i=0;i<sz;i++) printf("%d\t",arr[i]) ; printf("\n"); bubble_shot(s,sz1,sizeof(s[0]),p_s_s_name); printf("结构体按name排序\n"); for(i=0;i<sz1;i++) printf("%s\t",s[i].name) ; printf("\n"); bubble_shot(s,sz1,sizeof(s[0]),p_s_s_int); printf("结构体按int排序\n"); for(i=0;i<sz1;i++) printf("%s\t",s[i].name) ; printf("\n"); return 0;}#endif
阅读全文
0 0
- 通用冒泡排序算法
- 通用冒泡排序算法
- 通用冒泡排序算法
- 通用冒泡排序
- 通用类型的冒泡排序
- 排序算法--冒泡排序
- 排序算法-冒泡排序
- 排序算法--冒泡排序
- 排序算法--冒泡排序
- 排序算法-冒泡排序
- 排序算法 冒泡排序
- 排序算法-- 冒泡排序
- 排序算法-冒泡排序
- 排序算法---冒泡排序
- 排序算法-冒泡排序
- 排序算法--冒泡排序
- 排序算法--冒泡排序
- 排序算法--冒泡排序
- "libcudnn.so.5 cannot open shared object file: No such file or directory"
- css day02
- 微服务从设计到部署(四)服务发现
- Linux搭建maven私服nexus3.5
- 软件项目开发模式——三层模式
- 通用冒泡排序算法
- OCS——史上最疯狂的iOS动态化方案
- vue中如何在外部调用methods的方法
- JS四舍五入函数-- toFixed(number)
- DOM方法添加标签
- chrome developer tool 调试技巧
- timezone
- DSP基础知识
- Tomcat-----Tomcat设置启动参数