C语言数据结构 快速排序实例详解
2019/7/10 22:43:25
本文主要是介绍C语言数据结构 快速排序实例详解,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
C语言数据结构 快速排序实例详解
一、快速排序简介
快速排序采用分治的思想,第一趟先将一串数字分为两部分,第一部分的数值都比第二部分要小,然后按照这种方法,依次对两边的数据进行排序。
二、代码实现
#include <stdio.h> /* 将两个数据交换 */ void swap(int* Ina , int* Inb) { int temp = *Ina; *Ina = *Inb; *Inb = temp; } /* 进行一趟的快速排序,把一个序列分为两个部分 */ int getPartion(int* InArry,int InBegin,int InEnd) { /* 刚开始的分隔线是第一个 */ int part = InBegin; int index = 0; if(InEnd >= InBegin) { part = InBegin; for(index = InBegin+1; index <= InEnd; index++) { if(InArry[InBegin] >= InArry[index]) { /* 交换位置 */ swap(&InArry[part+1],&InArry[index]); part++; } } /* 把第一个数放到part处去 */ swap(&InArry[InBegin],&InArry[part]); return part; } } /* 快速排序函数 * InArry:输入的数组 * InBegin:数组的开始 * InEnd:数组的结束 */ void quickSort(int* InArry,int InBegin,int InEnd) { if(InArry == NULL || InEnd <= InBegin) { return; } int part = 0; part = getPartion(InArry,InBegin,InEnd); /* 递归调用 */ quickSort(InArry,0,part-1); quickSort(InArry,part+1,InEnd); } int main() { int a[] = {49,38,65,97,76,13,27}; int index = 0; int len = sizeof(a)/sizeof(int); /* 先遍历打印一下数组的元素 */ for(index = 0; index < len; index++) { printf("%d ",a[index]); } printf("\n"); /* 调用快速排序函数 */ quickSort(a,0,len-1); /* 再遍历打印一下数组的元素 */ for(index = 0; index < len; index++) { printf("%d ",a[index]); } printf("\n"); return 0; }
以上就是使用C语言数据结构 快速排序的实例详解,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站 的支持!
这篇关于C语言数据结构 快速排序实例详解的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-29Elasticsearch慢查询日志配置
- 2024-05-29揭秘华为如此多成功项目的产品关键——Charter模板
- 2024-05-29海外IDC业务拓展的7大挑战
- 2024-05-29InLine Chat功能优化对标Github Copilot,CodeGeeX带来更高效、更直观的编程体验!
- 2024-05-29CodeGeeX 智能编程助手 6 项功能升级,在Visual Studio插件市场霸榜2周!
- 2024-05-29AutoMQ 生态集成 Apache Doris
- 2024-05-292024年IDC行业的深度挖掘:机遇、挑战与未来展望
- 2024-05-29五款扩展组件齐发 —— Volcano、Keda、Crane-scheduler 等,邀你体验
- 2024-05-29AutoMQ 对象存储数据高效组织的秘密: Compaction
- 2024-05-29活动预告|来 GIAC 大会听大数据降本利器:AutoMQ 基于云原生重新设计的 Kafka