快速排序C语言算法实现
2011-06-04 22:56
711 查看
#include "stdio.h" #include "stdlib.h" typedef struct { int key; }Record; typedef struct { Record *r; int length; }SqList; void Display(SqList L) { for(int i=1;i<L.length+1;i++) printf("%d ",L.r[i].key); printf("/n"); } int Partion(SqList &L,int low,int high) { L.r[0]=L.r[low]; int pivotkey=L.r[low].key; while (low<high) { while (low<high&&L.r[high].key>=pivotkey) { --high; } L.r[low]=L.r[high]; while (low<high&&L.r[low].key<=pivotkey) { ++low; } L.r[high]=L.r[low]; } L.r[low]=L.r[0]; return low; } void QSort(SqList &L,int low,int high) { int pivotloc; if (low<high) { pivotloc=Partion(L,low,high); QSort(L,low,pivotloc-1); QSort(L,pivotloc+1,high); } } void QuickSort(SqList &L) { QSort(L,1,L.length); } void CreateSqList(SqList &L) { printf("Please input the number of data:/n"); scanf("%d",&L.length); L.r=(Record*)malloc((L.length+1)*sizeof(Record)); printf("please input data:/n"); for(int i=1;i<=L.length;i++) scanf("%d",&L.r[i].key); L.r[0].key=0;//0号未用元素初始化为0 } void main() { SqList L; CreateSqList(L); Display(L); QuickSort(L); Display(L); }
输入输出如下:
Please input the number of data:
8
please input data:
49 38 65 97 76 13 27 49
49 38 65 97 76 13 27 49
13 27 38 49 49 65 76 97
Press any key to continue
如有不足请多指教!
相关文章推荐
- 【算法总结系列-1】快速排序-c语言实现
- Python 快速排序 堆排序——Python实现一些算法持续更新
- Java实现排序算法之快速排序
- 快速排序 php实现 算法学习
- 快速排序及C语言实现
- 快速排序的Java语言与C语言实现
- 快速排序的C语言实现及其时间复杂度
- python 算法 排序实现快速排序
- 插入排序,合并排序,堆排序,快速排序,计数排序的实现(算法导论)
- 算法导论快速排序python实现
- 快速排序quicksort-算法导论java实现
- C语言快速排序与二分查找算法示例
- 「算法」快速排序 Java 实现
- 快速排序_算法基本思想及实现
- 算法代码实现之快速排序,Golang(Go语言)实现
- 快速排序的递归实现算法。
- 笔试算法题(54):快速排序实现之单向扫描、双向扫描(single-direction scanning, bidirectional scanning of Quick Sort)
- 快速排序详解--C语言实现
- 快速排序的算法思想及Python版快速排序的实现示例
- 快速排序---C语言实现