📄 quicksort.txt
字号:
快速排序法!_C++题集_C++基础_C++语言_C 语言之家☉ 设为首页 | 加入收藏 | 广告服务.
网站首页C语言C++语言Windows编程接口&驱动汇编语言编程资料软 件其它文章等级考试辅导专栏
当前位置:网站首页>>C++语言>>C++基础>>C++题集双击自动滚屏
快速排序法!
发表日期:2004年6月13日 出处:cangzhu 作者:cangzhu 已经有185位读者读过此文
/// E-mail:cangzhu@163.com
//快速排序法
//基本的思想:通过一趟排序将待排的记录分割成独立的两部分,
//其中前一部分的 记录的关键字均比另一部分记录的关键字小,
//再分别对两组记录进行递归分割,达到排序的目的
//平均时间复杂度为 O(log2(n))
#include "iostream.h"
#include "stdlib.h"
//交换两变量值
void swap(int &a,int &b)
{
int c;
c=a;a=b;b=c;
}
//将数组分成两部分,前一部分的值均比后一部分值小
//返回分界点
int Partition(int data[],int low,int high)
{
int pivokey;
pivokey=data[low];
while(low<high)
{
while(low<high&&data[high]>=pivokey)
high--;
swap(data[low],data[high]);
while(low<high&&data[low]<=pivokey)
low++;
swap(data[low],data[high]);
}
return low;
}
//进行的递归的调用,达到排序的目的
void QSort(int data[],int low,int high)
{
if(low<high)
{
int pivokey=Partition(data,low,high);
QSort(data,low,pivokey-1);
QSort(data,pivokey+1,high);
}
}
void main()
{
int i;
int mydata[50];
for(i=0;i<50;i++)
{
//每行显示 10 个数据
if(i%10==0)
cout<<endl;
mydata[i]=rand()%100;
cout<<mydata[i]<<" ";
}
QSort(mydata,0,49);
cout<<endl<<endl;
for(i=0;i<50;i++)
{
//每行显示 10 个数据
if(i%10==0)
cout<<endl;
cout<<mydata[i]<<" ";
}
}
发表评论
相关专题:
相关信息:
相关评论:
没有相关评论
更多评论
将本信息发给好友 打印本页
设为首页 | 加入收藏 |广告服务| 联系我们.
版权所有 Copyright © 2002~2004 C语言之家
........................................................................................................
7
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -