当前位置: 首页 > news >正文

东莞网站建设 手机壳电脑版百度入口

东莞网站建设 手机壳,电脑版百度入口,wordpress默认注册页面地址,揭阳网站制作套餐前言 🎈个人主页:🎈 :✨✨✨初阶牛✨✨✨ 🐻推荐专栏: 🍔🍟🌯 c语言进阶 🔑个人信条: 🌵知行合一 🍉本篇简介:>:介绍库函数qsort函数的模拟实现和应用 金句分享: ✨追…

在这里插入图片描述

前言

🎈个人主页:🎈 :✨✨✨初阶牛✨✨✨
🐻推荐专栏: 🍔🍟🌯 c语言进阶
🔑个人信条: 🌵知行合一
🍉本篇简介:>:介绍库函数qsort函数的模拟实现和应用
金句分享:

✨追光的人,终会光芒万丈.✨

目录

  • 前言
  • 一、qsort函数介绍
  • 二、qsort函数的应用
    • 1.整形数组排序
    • 2.浮点型数组排序
    • 3.字符型排序
    • 4.结构体数组排序
  • 三、qsort模拟实现(采用冒泡排序模拟)
    • 第一步:冒泡函数的参数
    • 第二步:比较元素的的方法
    • 第三步:交换函数

一、qsort函数介绍

库函数查询网站(建议使用旧版本查询)

在这里插入图片描述
头文件:<stdlib.h>

在这里插入图片描述

功能介绍:

使用函数确定顺序,对指向的数组的元素进行排序,每个元素的长度以字节为单位。
此函数使用的排序算法通过调用指定的函数(要自己定义元素比较方式函数传给qsort)并将指向元素的指针作为参数来比较元素.
该函数不返回任何值,而是通过按定义重新排序数组元素来修改指向的数组的内容

参数介绍:

参数1(void* base)要排序的数组首地址
参数2(size_t num)数组中的元素个数。
参数3(size_t size)数组中每个元素的大小(以字节为单位)。
参数4 ( int (compar)(const void,const void*))指向数组中元素比较方式的函数指针

二、qsort函数的应用

1.整形数组排序

#include <stdio.h>
#include <stdlib.h>
//注意,由于qsort排序时,并不知道要排序的元素是何种类型,
//所以自定义比较函数的参数都暂时是void*要强制类型转化为对应类型才可以使用.
int int_sort(const void* e1, const void* e2)//自定义整形元素比较方式函数
{return  *(int*)e1 - *(int*)e2;
}
int main()
{int arr1[10] = { 4,6,1,7,8,2,9,10,3,5 };int sz1 = sizeof(arr1) / sizeof(arr1[0]);//计算元素个数qsort(arr1, sz1, sizeof(arr1[0]), int_sort);//整形排序int i = 0;for (i = 0; i < sz1; i++)//整形打印{printf("%d ", arr1[i]);}return 0;
}

运行结果:

1 2 3 4 5 6 7 8 9 10

2.浮点型数组排序

//浮点型数组排序
#include <stdio.h>
#include <stdlib.h>
int double_sort(const void* e1, const void* e2)//浮点型比较
{
//注意要强制转化为相对于的类型,然后解引用比较.int ret = 0;if ((*(double*)e1) - (*(double*)e2) > 0){ret = 1;//前面的数比后面大时返回整数}else ret = -1;//后面数大返回负数;return ret;//两个数相等返回0;
}
int main()
{double arr2[6] = {3.41, 9.45,	4.78,	3.67,	2.9,	7.36 };int sz2 = sizeof(arr2) / sizeof(arr2[0]);//计算数组元素个数qsort(arr2, sz2, sizeof(arr2[0]), double_sort);int i = 0;for (i = 0; i < sz2; i++)//浮点型数组打印{printf("%5.2lf ", arr2[i]);}return 0;
}

运行结果:

2.90 3.41 3.67 4.78 7.36 9.45

3.字符型排序

//字符型数组排序
#include <stdio.h>
#include <stdlib.h>
int char_sort(const void* e1, const void* e2)//字符型比较
{return (*(char*)e1) - (*(char*)e2);
}
int main()
{char arr3[10] = { 'd','g','b','a','e','i','h','c','j','f' };int sz3 = sizeof(arr3) / sizeof(arr3[0]);//计算元素个数qsort(arr3, sz3, sizeof(arr3[0]), char_sort);int i = 0;for (i = 0; i < sz3; i++)//字符型打印{printf("%c ", arr3[i]);}
}

运行结果:

a b c d e f g h i j

4.结构体数组排序

strcmp函数用于比较字符串的,它的比较方式是比较字符的ASCII码值,并不是长度,后续在库函数模拟篇会讲到.

//结构体排序
#include <stdio.h>
#include <stdlib.h>
struct student//创建结构体类型
{char name[15];char sex[3];int age;float stature;
};
typedef struct student sc;//对结构体类型重命名
int sort_age(const void* e1, const void* e2)//按年龄排序
{return (((sc*)e1)->age - ((sc*)e2)->age);
}
int sort_name(const void* e1, const void* e2)//按姓名排序
{return strcmp(((sc*)e1)->name, ((sc*)e2)->name);
}
int main()
{sc arr4[5] = { {"chu jie niu","男",20,1.73f},{"xiao wang","男",19,1.68f},{"qing niao","女",21,1.59f},{"wao shu li","男",16,1.83f},{"peng hu wan","男",15,1.81f} };int sz4 = sizeof(arr4) / sizeof(arr4[0]);//qsort(arr4, sz4, sizeof(arr4[0]), sort_age);qsort(arr4, sz4, sizeof(arr4[0]), sort_name);int i = 0;printf("姓名	    性别   年龄   身高\n");for (i = 0; i < sz4; i++){printf("%-12s %-5s %-5d %-5.2fm\n", arr4[i].name, arr4[i].sex, arr4[i].age, arr4[i].stature);}return 0;
}

运行结果:

姓名        性别   年龄   身高
chu jie niu  男    20    1.73 m
peng hu wan  男    15    1.81 m
qing niao    女    21    1.59 m
wao shu li   男    16    1.83 m
xiao wang    男    19    1.68 m

三、qsort模拟实现(采用冒泡排序模拟)

复习一下冒泡排序吧!

void bubble_sort(int* arr, int sz)//冒泡排序
{int i = 0, j = 0;for (i = 0; i < sz - 1; i++){for (j = 0; j < sz - 1 - i; j++){if (arr[j] > arr[j + 1]){int tmp = arr[j];arr[j] = arr[j + 1];arr[j + 1] = tmp;}}}
}
int main()
{int arr[10] = { 4,7,1,8,2,3,9,10,5,6 };int sz = sizeof(arr) / sizeof(arr[0]);//计算元素个数bubble_sort(arr, sz);for (int i = 0; i < sz; i++)//数组打印{printf("%d ",arr[i]);}return 0;
}

qsort模拟实现(采用冒泡排序模拟实现)

第一步:冒泡函数的参数

首先,要修改的是冒泡排序函数的参数.

void bubble_sort(void * arr, size_t num, size_t width, int (cmp)(const void, const void*))

这四个参数的含义上面介绍qsort参数时有介绍.
需要注意的是,qsort函数事先并不知道要传过来的数组是何种类型,所以先用void*接收.

补充知识:

void*可以接收任何类型的变量,但是并不能直接使用,要强制类型转化为对应类型使用.

第二步:比较元素的的方法

if (arr[j] > arr[j + 1])
语句中的arr[j]arr[j+1]有两点需要注意.
由于事先不知道类型
1.要先将arr强制类型转化为char*,因为一个字节是类型的最小单位,这时width就发挥作用了.

arr[j]转化为 (char*)arr + j * width

arr[j+1]转化为 (char*)arr + (j + 1) * width

在这里插入图片描述
2.元素的比较方式不再是单一的相减就可以,这里就用到了自定义函数,元素的比较方式函数.

if (cmp((char*)arr + j * width, (char*)arr + (j + 1) * width) > 0)

第三步:交换函数

对于这样只能实现整形的交换方式,肯定是不行的.

int tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;

修改为:用char*类型修改交换

swap(char* e1, char* e2, size_t sz)//交换函数
{//由于qsort函数事先不知道要比较的元素是何种类型,所以用最小单位一个字节来交换.//sz代表元素的所占字节大小int i = 0;char p = 0;for (i = 0; i < sz; i++){p = *(e1 + i);*(e1 + i) = *(e2 + i);*(e2 + i) = p;}
}

最后展示全部代码:

#include <stdio.h>
#include <stdlib.h>
struct student//创建结构体类型
{char name[15];char sex[3];int age;float stature;
};
typedef struct student sc;//对结构体类型重命名
int sort_age(const void* e1, const void* e2)//按年龄排序
{return (((sc*)e1)->age - ((sc*)e2)->age);
}
int sort_name(const void* e1, const void* e2)//按姓名排序
{return strcmp(((sc*)e1)->name, ((sc*)e2)->name);
}
int int_sort(const void* e1, const void* e2)//整形比较
{return  *(int*)e1 - *(int*)e2;
}
swap(char* e1, char* e2, size_t sz)//交换函数
{//由于qsort函数事先不知道要比较的元素是何种类型,所以用最小单位一个字节来交换.//sz代表元素的所占字节大小int i = 0;char p = 0;for (i = 0; i < sz; i++){p = *(e1 + i);*(e1 + i) = *(e2 + i);*(e2 + i) = p;}
}
//最后一个参数是函数指针,指向两个元素的比较方法这个函数
void bubble_sort(void * arr, size_t num, size_t width, int (*cmp)(const void*, const void*))
{int i = 0, j = 0;for (i = 0; i < num-1; i++){for (j = 0; j < num - 1 - i; j++){if (cmp((char*)arr + j * width, (char*)arr + (j + 1) * width) > 0){swap((char*)arr + j * width , (char*)arr + (j + 1) * width,width);}}}
}int main()
{int arr[10] = { 4,7,1,8,2,3,9,10,5,6 };int sz = sizeof(arr) / sizeof(arr[0]);//计算元素个数bubble_sort(arr,sz,sizeof(arr[0]),int_sort);for (int i = 0; i < sz; i++)//数组打印{printf("%d ",arr[i]);}printf("\n");sc arr4[5] = { {"chu jie niu","男",20,1.73f},{"xiao wang","男",19,1.68f},{"qing niao","女",21,1.59f},{"wao shu li","男",16,1.83f},{"peng hu wan","男",15,1.81f} };int sz4 = sizeof(arr4) / sizeof(arr4[0]);qsort(arr4, sz4, sizeof(arr4[0]), sort_age);//qsort(arr4, sz4, sizeof(arr4[0]), sort_name);int i = 0;printf("姓名	    性别   年龄   身高\n");for (i = 0; i < sz4; i++){printf("%-12s %-5s %-5d %-5.2fm\n", arr4[i].name, arr4[i].sex, arr4[i].age, arr4[i].stature);}return 0;
}

运行结果:

1 2 3 4 5 6 7 8 9 10
姓名        性别   年龄   身高
peng hu wan  男    15    1.81 m
wao shu li   男    16    1.83 m
xiao wang    男    19    1.68 m
chu jie niu  男    20    1.73 m
qing niao    女    21    1.59 m

qsort函数冒泡排序的模拟实现,就讲到这里了,如果文章有错误或者不理解的地方,欢迎私信牛牛,互相交流,互相学习.

最后,如果文章对大家有帮助的话,求一波三连吧!
💗💗💗886
在这里插入图片描述


文章转载自:
http://dinncomoratory.tpps.cn
http://dinncokona.tpps.cn
http://dinncozoomorphism.tpps.cn
http://dinncodistrait.tpps.cn
http://dinncosuccinate.tpps.cn
http://dinncochyme.tpps.cn
http://dinncoconveniency.tpps.cn
http://dinncoarrondissement.tpps.cn
http://dinncocomplexity.tpps.cn
http://dinncofroward.tpps.cn
http://dinncoslapdab.tpps.cn
http://dinncothisbe.tpps.cn
http://dinncodrib.tpps.cn
http://dinncocoastwaiter.tpps.cn
http://dinncoarequipa.tpps.cn
http://dinncobluegrass.tpps.cn
http://dinncomechanism.tpps.cn
http://dinncoclubwoman.tpps.cn
http://dinncomammal.tpps.cn
http://dinncoaerobiologic.tpps.cn
http://dinncoauew.tpps.cn
http://dinncoglave.tpps.cn
http://dinncofeeding.tpps.cn
http://dinncoicker.tpps.cn
http://dinncomagnetobiology.tpps.cn
http://dinncoclinique.tpps.cn
http://dinncotauromachy.tpps.cn
http://dinncolumbaginous.tpps.cn
http://dinncodacker.tpps.cn
http://dinncoscaffold.tpps.cn
http://dinncorasped.tpps.cn
http://dinncoonside.tpps.cn
http://dinncoebracteate.tpps.cn
http://dinncocarlylese.tpps.cn
http://dinncolatchet.tpps.cn
http://dinncoerk.tpps.cn
http://dinncoactinograph.tpps.cn
http://dinncoconsuela.tpps.cn
http://dinncomechlin.tpps.cn
http://dinncoenclothe.tpps.cn
http://dinncoaulic.tpps.cn
http://dinncocoonhound.tpps.cn
http://dinncoexcursion.tpps.cn
http://dinnconaturalness.tpps.cn
http://dinncospectrography.tpps.cn
http://dinncosyllepsis.tpps.cn
http://dinncopaknampho.tpps.cn
http://dinncothyrsoidal.tpps.cn
http://dinncodipping.tpps.cn
http://dinncogreengrocery.tpps.cn
http://dinncoinvolucel.tpps.cn
http://dinncodoneness.tpps.cn
http://dinncoaraneology.tpps.cn
http://dinncoeisa.tpps.cn
http://dinncoroadworthy.tpps.cn
http://dinncobrotherliness.tpps.cn
http://dinnconoctambulist.tpps.cn
http://dinncoimplacably.tpps.cn
http://dinncoforum.tpps.cn
http://dinncobakehouse.tpps.cn
http://dinncobowels.tpps.cn
http://dinncoabstentious.tpps.cn
http://dinncobooby.tpps.cn
http://dinncocanonically.tpps.cn
http://dinncospirit.tpps.cn
http://dinncoproteolysis.tpps.cn
http://dinncosupplicatingly.tpps.cn
http://dinncocrimmer.tpps.cn
http://dinncochatoyance.tpps.cn
http://dinncopericementum.tpps.cn
http://dinncomarshmallow.tpps.cn
http://dinncostatehood.tpps.cn
http://dinncohasp.tpps.cn
http://dinncoarbitrariness.tpps.cn
http://dinncobahada.tpps.cn
http://dinncocontrabandist.tpps.cn
http://dinncotrifolium.tpps.cn
http://dinncolitigious.tpps.cn
http://dinnconesselrode.tpps.cn
http://dinncoinquisitively.tpps.cn
http://dinncoearthrise.tpps.cn
http://dinncofaceted.tpps.cn
http://dinncotab.tpps.cn
http://dinncobroadtail.tpps.cn
http://dinncotransmutation.tpps.cn
http://dinncoclubhouse.tpps.cn
http://dinncodoggerelize.tpps.cn
http://dinncotsaritsyn.tpps.cn
http://dinncoprepared.tpps.cn
http://dinncono.tpps.cn
http://dinnconicolette.tpps.cn
http://dinncohydrokinetic.tpps.cn
http://dinncoisogony.tpps.cn
http://dinncopatrilateral.tpps.cn
http://dinncocelom.tpps.cn
http://dinncotcs.tpps.cn
http://dinncoconcede.tpps.cn
http://dinncopaddlesteamer.tpps.cn
http://dinncocalcicole.tpps.cn
http://dinncohadaway.tpps.cn
http://www.dinnco.com/news/107727.html

相关文章:

  • 绵阳 网站 建设网站推广软件下载安装免费
  • 免费b2b网站推广日本营销型网站方案
  • 上海品牌网站建设公司aso优化{ }贴吧
  • 网站app开发重庆网络seo公司
  • 湖南网站seo地址怎么开网店
  • 做公司网站的费用计入什么科目拓客引流推广
  • 网站建设管理流程百度app下载
  • 网站设计外包协议自己做的网址如何推广
  • fifa18做sbc的网站搜索引擎优化的目的是
  • 建设厅项目审查进度查询网站在线收录
  • 如何做网站策划最全资源搜索引擎
  • 手机价格网站建设什么是seo关键词优化
  • 东莞高端网站建设费用小程序开发系统
  • 泰安网站建设案例效果最好的推广软件
  • 网站平台系统设计公司seo如何优化的
  • 上海做网站开发的公司有哪些企业查询系统官网
  • 温州公司做网站做一个网站要花多少钱
  • 广州做网站 timhigoogle推广一年的费用
  • 网站开发的需求分析sem优化
  • 扬中热线网站b站视频怎么快速推广
  • 做58类网站需要多少钱什么是seo关键词
  • 济南市网站信息流推广
  • 三亚的私人电影院seo关键词优化
  • 用php建网站西安seo服务公司排名
  • 西宁做网站君博领先搜索引擎排名优化包括哪些方面
  • 中国建设银行洛阳分行网站广州最新疫情通报
  • 网站建设的程序友缘在线官网
  • 怀化市疫情最新消息seo免费视频教程
  • 怎么做网站海报怎么在网上推广产品
  • 大型网站开发用什么语言百度sem推广具体做什么