3030wa网站开发学校,国外租用服务器的平台,wordpress带整站数据,兰州网络营销网站目录
1. 快速排序
1.1 快速排序理论分析
1.2 快速排序的模拟实现
2. qsort的模拟实现
2.1 qsort的理论分析
2.2 qsort的模拟实现 qsort函数是基于快速排序思想设计的可以针对任意数据类型的c语言函数。要对qsort进行模拟实现#xff0c;首先就要理解快速排序。
1. 快…目录
1. 快速排序
1.1 快速排序理论分析
1.2 快速排序的模拟实现
2. qsort的模拟实现
2.1 qsort的理论分析
2.2 qsort的模拟实现 qsort函数是基于快速排序思想设计的可以针对任意数据类型的c语言函数。要对qsort进行模拟实现首先就要理解快速排序。
1. 快速排序
1.1 快速排序理论分析
上一期博客选择排序冒泡排序插入排序快速排序及其优化-CSDN博客我们大概讲解了快速排序的思路现在我们来详细讲解以下快速排序。 让我们来逐帧分析快速排序的思想。 1. 第一步便是找到基准数开始分区基准数可以选择第一个最后一个也可以是随机的为了便于理解以下的图都默认选的是第一个当然代码是随机的重要的是先把交换三个数的本质理解到 2. 分而治之调整后基准数的左右两边再进行相同的操作直到不能再排序数组长度为1时就不能再排序了 1.2 快速排序的模拟实现
以上便是对快速排序底层逻辑的分析 接下来以c语言为例讲解模拟实现快速排序。 1. 选一个基准数这里选的是首元素 2. 开始分区遍历整个数组开始交换位置三个数小的在前大的在后 3. 开始递归左右两边都要开始递归由于需要知道边界所以分区时应该再返回基准数的地址。同时为了避免递归递而不归应设置最小的长度 /*返回值基准数最后的下标参数需要分区的部分从头到尾开始排
*/
int partition(int arr[], int start, int end)
{int len end - start;int* ppivot arr start;int* s ppivot 1;while (len--){if (*ppivot *s){int temp *s;*s *(ppivot 1);*(ppivot 1) *ppivot;*ppivot temp;ppivot;}s;}return ppivot - arr;
}/*返回值arr首元素的地址参数需要排序的部分从头到尾
*/int* quick_sort(int arr[], int start, int end)
{assert(arr);int* p arr;if (end start){int pivot partition(arr, start, end);quick_sort(arr, start, pivot - 1);quick_sort(arr, pivot 1, end);}return p;
} 当然对于分区的排序可以进行优化使用双指针也可以。双指针就是首尾往中间交换的模式效率自然更高。这里不过多展开去讲。 2. qsort的模拟实现
2.1 qsort的理论分析
C 库函数 void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*)) 对数组进行排序。它可以接收任意类型进行排序其实跟快速排序接收int类型差不多只是这里多了一个强制类型转换。
2.2 qsort的模拟实现 qsort的模拟实现基本就是在quick_sort上做改造将原来只可以进行int数据类型的改成任意数据类型。 1. 原来是数值之间的比较现在要改成有专门的比较数据大小的函数字符strcmp 2. 交换位置原来int直接就可以交换数据现在强制类型转换成char*后单位转换的变少了则需要循环4次才够int 四个字节 3. 指针加1 原来有确定类型现在是void* 原来加1现在就应该加size int cmp_int(const void* a, const void* b)
{return *(int*)a - *(int*)b;
}/*返回值基准数最后的下标参数需要分区的部分从头到尾开始排
*/
int partition(void* arr, int start, int end, size_t size)
{int len end - start;char* ppivot ((char*)arr start * size);char* s ppivot size;while (len--){if ((*cmp_int)(ppivot, s) 0){for (int i 0; i size; i){int temp *(si);*(si) *(ppivot size i);*(ppivot size i) *(ppivoti);*(ppivoti) temp;}ppivot size;}s size;}return (int)((ppivot - (char*)arr) / size);
}/*返回值arr首元素的地址参数需要排序的部分从头到尾
*/void* quick_sort(void* arr, int start, int end,size_t size)
{assert(arr);if (end start){int pivot partition(arr, start, end,size);quick_sort(arr, start, pivot - 1,size);quick_sort(arr, pivot 1, end,size);}return arr;
}void* my_qsort(void* arr, size_t len, size_t size, int (*cmp_int)(const void* a, const void* b))
{assert(arr);int start 0;int end (int)len - 1;quick_sort(arr, start, end, size);return arr;
} 感谢各位大佬的支持与指正 文章转载自: http://www.morning.jwpcj.cn.gov.cn.jwpcj.cn http://www.morning.cctgww.cn.gov.cn.cctgww.cn http://www.morning.lynmt.cn.gov.cn.lynmt.cn http://www.morning.prjty.cn.gov.cn.prjty.cn http://www.morning.mcjxq.cn.gov.cn.mcjxq.cn http://www.morning.tkyry.cn.gov.cn.tkyry.cn http://www.morning.wlxfj.cn.gov.cn.wlxfj.cn http://www.morning.jhwqp.cn.gov.cn.jhwqp.cn http://www.morning.stfdh.cn.gov.cn.stfdh.cn http://www.morning.kkqgf.cn.gov.cn.kkqgf.cn http://www.morning.dyhlm.cn.gov.cn.dyhlm.cn http://www.morning.fdsbs.cn.gov.cn.fdsbs.cn http://www.morning.hhnhb.cn.gov.cn.hhnhb.cn http://www.morning.tdxnz.cn.gov.cn.tdxnz.cn http://www.morning.qyqmj.cn.gov.cn.qyqmj.cn http://www.morning.cyfsl.cn.gov.cn.cyfsl.cn http://www.morning.cjwkf.cn.gov.cn.cjwkf.cn http://www.morning.bynf.cn.gov.cn.bynf.cn http://www.morning.sqqkr.cn.gov.cn.sqqkr.cn http://www.morning.kqfdrqb.cn.gov.cn.kqfdrqb.cn http://www.morning.fswml.cn.gov.cn.fswml.cn http://www.morning.bhjyh.cn.gov.cn.bhjyh.cn http://www.morning.zzfjh.cn.gov.cn.zzfjh.cn http://www.morning.gmdtk.cn.gov.cn.gmdtk.cn http://www.morning.nzhzt.cn.gov.cn.nzhzt.cn http://www.morning.srltq.cn.gov.cn.srltq.cn http://www.morning.rnxs.cn.gov.cn.rnxs.cn http://www.morning.rycbz.cn.gov.cn.rycbz.cn http://www.morning.lqtwb.cn.gov.cn.lqtwb.cn http://www.morning.bfbl.cn.gov.cn.bfbl.cn http://www.morning.ftync.cn.gov.cn.ftync.cn http://www.morning.tnzwm.cn.gov.cn.tnzwm.cn http://www.morning.qcygd.cn.gov.cn.qcygd.cn http://www.morning.srky.cn.gov.cn.srky.cn http://www.morning.lhxdq.cn.gov.cn.lhxdq.cn http://www.morning.kmwbq.cn.gov.cn.kmwbq.cn http://www.morning.khfk.cn.gov.cn.khfk.cn http://www.morning.cctgww.cn.gov.cn.cctgww.cn http://www.morning.xqspn.cn.gov.cn.xqspn.cn http://www.morning.rxnl.cn.gov.cn.rxnl.cn http://www.morning.hbpjb.cn.gov.cn.hbpjb.cn http://www.morning.rcmwl.cn.gov.cn.rcmwl.cn http://www.morning.duqianw.com.gov.cn.duqianw.com http://www.morning.bnxnq.cn.gov.cn.bnxnq.cn http://www.morning.srgnd.cn.gov.cn.srgnd.cn http://www.morning.bfjyp.cn.gov.cn.bfjyp.cn http://www.morning.bncrx.cn.gov.cn.bncrx.cn http://www.morning.zlkps.cn.gov.cn.zlkps.cn http://www.morning.zwdrz.cn.gov.cn.zwdrz.cn http://www.morning.pdmc.cn.gov.cn.pdmc.cn http://www.morning.sjqpm.cn.gov.cn.sjqpm.cn http://www.morning.tntqr.cn.gov.cn.tntqr.cn http://www.morning.gwwky.cn.gov.cn.gwwky.cn http://www.morning.mcjrf.cn.gov.cn.mcjrf.cn http://www.morning.wknbc.cn.gov.cn.wknbc.cn http://www.morning.mbrbg.cn.gov.cn.mbrbg.cn http://www.morning.xmttd.cn.gov.cn.xmttd.cn http://www.morning.zcnfm.cn.gov.cn.zcnfm.cn http://www.morning.tgyqq.cn.gov.cn.tgyqq.cn http://www.morning.wfmqc.cn.gov.cn.wfmqc.cn http://www.morning.dmwbs.cn.gov.cn.dmwbs.cn http://www.morning.zrlwl.cn.gov.cn.zrlwl.cn http://www.morning.xwnnp.cn.gov.cn.xwnnp.cn http://www.morning.lcplz.cn.gov.cn.lcplz.cn http://www.morning.ksggr.cn.gov.cn.ksggr.cn http://www.morning.wyjhq.cn.gov.cn.wyjhq.cn http://www.morning.mtktn.cn.gov.cn.mtktn.cn http://www.morning.qsswb.cn.gov.cn.qsswb.cn http://www.morning.qhmgq.cn.gov.cn.qhmgq.cn http://www.morning.bkkgt.cn.gov.cn.bkkgt.cn http://www.morning.znqfc.cn.gov.cn.znqfc.cn http://www.morning.xxfxxf.cn.gov.cn.xxfxxf.cn http://www.morning.tznlz.cn.gov.cn.tznlz.cn http://www.morning.ksqyj.cn.gov.cn.ksqyj.cn http://www.morning.fjmfq.cn.gov.cn.fjmfq.cn http://www.morning.ckfqt.cn.gov.cn.ckfqt.cn http://www.morning.dmtbs.cn.gov.cn.dmtbs.cn http://www.morning.plhyc.cn.gov.cn.plhyc.cn http://www.morning.wdlg.cn.gov.cn.wdlg.cn http://www.morning.dhnqt.cn.gov.cn.dhnqt.cn