快速排序
2015-09-26 22:42
246 查看
#include<cstdio>
#include<ctime>
#include<cstdlib>
=====================================================
void Swap(int& a,int& b){
if(a!=b){
a^=b;b^=a;a^=b;
}
}
=====================================================
int Partition(int *A,int p,int r)
{
int x,i;
x=A[r];
i=p-1;
for(int j=p;j<=r-1;++j)
{
if(A[j]<=x)
i++;//x为最后一个数,小于x, 不动(交换)本身(未有大于x的)
Swap(A[i],A[j]);
}
Swap(A[++i],A[r]);
return i;
}
//已有大于x的时,再遇小于x:交换
//i为最后一个小于等于x元素的下标。
=====================================================
void QuickSort(int *A,int p,int r)
{
if(p<r)
{
int q = Partition(A,p,r);
QuickSort(A,p,q-1);
QuickSort(A,q+1,r);
}
}
#include<cstdio>
#include<ctime>
#include<cstdlib>
inline void Swap(int &a, int &b)
{
if(a!=b)
{
a^=b;
b^=a;
a^=b;
}
}
int Partition(int *A,int front,int end)
{
int key = A[end];
int i = front - 1;
for(int current = front;current < end;++current)
{
if(A[current]<=key)
Swap(A[++i],A[current]);
}
Swap(A[++i],A[end]);
return i;
}
void QuickSort (int *A,int front,int end)
{
if(front < end)
{
int midPosition = Partition(A,front,end);
QuickSort(A,front,midPosition-1);
QuickSort(A,midPosition+1,end);
}
}
#include<ctime>
#include<cstdlib>
=====================================================
void Swap(int& a,int& b){
if(a!=b){
a^=b;b^=a;a^=b;
}
}
=====================================================
int Partition(int *A,int p,int r)
{
int x,i;
x=A[r];
i=p-1;
for(int j=p;j<=r-1;++j)
{
if(A[j]<=x)
i++;//x为最后一个数,小于x, 不动(交换)本身(未有大于x的)
Swap(A[i],A[j]);
}
Swap(A[++i],A[r]);
return i;
}
//已有大于x的时,再遇小于x:交换
//i为最后一个小于等于x元素的下标。
=====================================================
void QuickSort(int *A,int p,int r)
{
if(p<r)
{
int q = Partition(A,p,r);
QuickSort(A,p,q-1);
QuickSort(A,q+1,r);
}
}
#include<cstdio>
#include<ctime>
#include<cstdlib>
inline void Swap(int &a, int &b)
{
if(a!=b)
{
a^=b;
b^=a;
a^=b;
}
}
int Partition(int *A,int front,int end)
{
int key = A[end];
int i = front - 1;
for(int current = front;current < end;++current)
{
if(A[current]<=key)
Swap(A[++i],A[current]);
}
Swap(A[++i],A[end]);
return i;
}
void QuickSort (int *A,int front,int end)
{
if(front < end)
{
int midPosition = Partition(A,front,end);
QuickSort(A,front,midPosition-1);
QuickSort(A,midPosition+1,end);
}
}
相关文章推荐
- C语言函数指针的例子
- Altium Designer 出现错误提示(警告)adding items to hidden net GND/VCC
- Altium Designer 出现错误提示(警告)adding items to hidden net GND/VCC
- UNIX环境高级编程——第三章
- Android dex文件通用自动脱壳器
- POJ-1083 Moving Tables
- hdu4778状态压缩dp 博弈
- Java:详解传值和传引用
- 分页代码
- shell 判断文件、目录是否存在
- 用JAVA做一个用户登入系统
- 软件架构设计系列总结
- 关于软件的自动化测试能否代替软件的手工测试
- 2015第39周六
- LINUX DHCP学习
- BestCoder Round #57 (div.2)HDU 5480 Conturbatio
- 【UIKit-93-1】#import <UIKit/UIScrollView.h>
- leetcode:Add Digits
- [题解+总结]20150926
- Java:Integer的值问题