C++数据结构--快速排序
来源:互联网 发布:dwg看图软件 mac 编辑:程序博客网 时间:2024/09/21 09:25
#include<stdio.h>
typedef int InfoType;
#define n 8 //假设的文件长度,即待排序的记录数目
typedef int KeyType; //假设的关键字类型
typedef struct { //记录类型
KeyType key; //关键字项
InfoType otherinfo;//其它数据项,类型InfoType依赖于具体应用而定义
} RecType;
typedef RecType SeqList[n+1]; //SeqList为顺序表类型,表中第0个单元一般用作哨兵
int Partition(SeqList R,int i,int j);
void main()
{
void QuickSort(SeqList R,int low,int high);
int i;
SeqList R;
printf("请输入欲排序的数:");
for (i=1;i<=n;i++)
scanf("%d",&R[i].key);
printf("排序前:");
for (i=1;i<=n;i++)
printf("%d ",R[i].key);
printf("\n");
QuickSort(R,1,n);
printf("排序后:");
for (i=1;i<=n;i++)
printf("%d ",R[i].key);
printf("\n");
}
//对R[low..high]快速排序
void QuickSort(SeqList R,int low,int high)
{
int pos=0;
if(low<high)
{
pos= Partition(R,low,high);//pos为基准下标
QuickSort(R,low,pos-1);
QuickSort(R,pos+1,high);
}
}
//调用Partition(R,low,high)时,对R[low..high]做划分,并返回基准记录的位置
int Partition(SeqList R,int i,int j)
{
RecType base=R[i];
while(i<j)
{
while(i<j&&R[j].key>=base.key)
j--;
R[i]=R[j];
while(i<j&&R[i].key<=base.key)
i++;
R[j]=R[i];
}
R[i]=base;
return i;
}
运行结果:
快速排序简化版本:
#include<iostream>
using namespace std;
#define n 6
int main()
{
void QuickSort(int arr[],int i,int j);
int arr[n]={12,43,2,76,32,54};
QuickSort(arr,0,n-1);
for(int i=0;i<n;i++)
cout<<arr[i]<<"\t";
cout<<endl;
}
int division(int arr[],int i,int j);
void QuickSort(int arr[],int i,int j)
{
int pos;
if(i<j)
{
pos=division(arr,i,j);
QuickSort(arr,i,pos-1);
QuickSort(arr,pos+1,j);
}
};
int division(int arr[],int i,int j)
{
int base=arr[i];
while(i<j)
{
while(i<j&&arr[j]>=base)
j--;
arr[i]=arr[j];
while(i<j&&arr[i]<=base)
i++;
arr[j]=arr[i];
arr[i]=base;
}
return i;
}
typedef int InfoType;
#define n 8 //假设的文件长度,即待排序的记录数目
typedef int KeyType; //假设的关键字类型
typedef struct { //记录类型
KeyType key; //关键字项
InfoType otherinfo;//其它数据项,类型InfoType依赖于具体应用而定义
} RecType;
typedef RecType SeqList[n+1]; //SeqList为顺序表类型,表中第0个单元一般用作哨兵
int Partition(SeqList R,int i,int j);
void main()
{
void QuickSort(SeqList R,int low,int high);
int i;
SeqList R;
printf("请输入欲排序的数:");
for (i=1;i<=n;i++)
scanf("%d",&R[i].key);
printf("排序前:");
for (i=1;i<=n;i++)
printf("%d ",R[i].key);
printf("\n");
QuickSort(R,1,n);
printf("排序后:");
for (i=1;i<=n;i++)
printf("%d ",R[i].key);
printf("\n");
}
//对R[low..high]快速排序
void QuickSort(SeqList R,int low,int high)
{
int pos=0;
if(low<high)
{
pos= Partition(R,low,high);//pos为基准下标
QuickSort(R,low,pos-1);
QuickSort(R,pos+1,high);
}
}
//调用Partition(R,low,high)时,对R[low..high]做划分,并返回基准记录的位置
int Partition(SeqList R,int i,int j)
{
RecType base=R[i];
while(i<j)
{
while(i<j&&R[j].key>=base.key)
j--;
R[i]=R[j];
while(i<j&&R[i].key<=base.key)
i++;
R[j]=R[i];
}
R[i]=base;
return i;
}
运行结果:
快速排序简化版本:
#include<iostream>
using namespace std;
#define n 6
int main()
{
void QuickSort(int arr[],int i,int j);
int arr[n]={12,43,2,76,32,54};
QuickSort(arr,0,n-1);
for(int i=0;i<n;i++)
cout<<arr[i]<<"\t";
cout<<endl;
}
int division(int arr[],int i,int j);
void QuickSort(int arr[],int i,int j)
{
int pos;
if(i<j)
{
pos=division(arr,i,j);
QuickSort(arr,i,pos-1);
QuickSort(arr,pos+1,j);
}
};
int division(int arr[],int i,int j)
{
int base=arr[i];
while(i<j)
{
while(i<j&&arr[j]>=base)
j--;
arr[i]=arr[j];
while(i<j&&arr[i]<=base)
i++;
arr[j]=arr[i];
arr[i]=base;
}
return i;
}
- 快速排序----C语言数据结构
- 数据结构(C数据)--快速排序
- 数据结构之快速排序(C语言)
- 数据结构 C语言实现快速排序
- 数据结构之快速排序(c++)
- 数据结构--快速、冒泡、选择排序C语言实现
- 数据结构 - 快速排序(Quick Sort) 详解 及 代码(C++)
- 快速排序的C语言实现【严蔚敏--数据结构】
- 数据结构严蔚敏版快速排序算法c语言实现
- 《数据结构与算法分析(c描述》—— 快速排序
- C语言-数据结构-快速排序及优化-源代码
- 数据结构与算法(C语言版)__快速排序
- 数据结构 快速排序(用C语言实现)
- 数据结构之快速排序
- 数据结构练习:快速排序
- 数据结构之快速排序
- 数据结构之------快速排序
- 快速排序<参考数据结构>
- macfee 防范规则有用集成
- 协议栈中绑定流程的一点认识
- 正则 记忆总结 一一四 模式的逻辑或运算:
- 带有clob字段的自动分区表
- oracle中时间日期的处理
- C++数据结构--快速排序
- Ubuntu升级,清理系统垃圾
- 插值算法
- LISP 简单的数据库 3.1 CD与记录
- javascript:Window.Open详解
- win7 229补丁导致开机登录界面出现 交互式登录进程初始化失败
- 三部排序 - 蓝桥杯
- 配置Spring数据源c3p0与dbcp
- win release/debug 删除文件夹