快速排序思想及实现
2014-11-30 23:50
197 查看
一趟快速排序(划分)是指将待排序记录按照枢轴(通常为第一个记录)分为两个子序列,使得一个子序列的值<=pivot<=第二个子序列
#include<iostream> using namespace std; void swap(int *a1, int *a2) { int tmp; tmp = *a1; *a1 = *a2; *a2 = tmp; } int Partition(int a[], int low, int high) { int pivotkey = a[low]; while(low<high) { while(low<high&&a[high]>=pivotkey) high--; swap(a[low], a[high]); while(low<high&&a[low]<=pivotkey) low++; swap(a[low],a[high]); } return low; } void QSort(int a[], int low, int high) { if(low<high) { int pivotloc = Partition(a,low,high); QSort(a,low,pivotloc-1); QSort(a,pivotloc+1,high); } } int main() { int a[] = {38, 2, 33, 45, 4}; /* cout << a[0] << ", " << a[1] << endl; swap(a[0], a[1]); cout << a[0] << ", " << a[1] << endl; */ QSort(a,0,sizeof(a)/sizeof(int)-1); for(int i=0; i<sizeof(a)/sizeof(int); i++) { cout << a[i] << " "; } return 1; }
aa: a.o g++ -o aa a.o a.o: a.cpp g++ -c a.cpp clean: rm a.o
相关文章推荐
- 快速排序的算法思想及Python版快速排序的实现示例
- 快速排序之“挖坑填数+分治”思想实现
- 数据结构之二分法查找、快速排序思想与实现
- 快速排序的算法思想及Python版快速排序的实现示例
- 【算法】快速排序——基于分治思想的实现
- 快速 排序的思想并实现一个快排?
- 快速排序基本思想以及代码实现
- 数据结构之二分法查找、快速排序思想与实现
- 二分法查找、快速排序思想与实现
- 快速排序_算法基本思想及实现
- 爱创课堂每日一题第二十七天-2017/9/29 快速 排序的思想并实现一个快排?
- 数据结构之二分法查找、快速排序思想与实现
- C快速排序-思想、原理与实现-简笔
- 数据结构之二分法查找、快速排序思想与实现
- 排序算法之快速排序的思想以及Java实现
- 快速排序的基本思想及Java实现
- 快速排序二叉树实现思想(个人看到二分法排序想到的)
- 快速排序Java实现
- List 采用delegate快速实现排序、查找等操作
- C语言实现 排序源程序(包括直接插入、希尔、冒泡、快速、简单选择、堆排序)