参考:

快速排序

堆排序

各类排序

#include <iostream>
#include <vector>
#include <time.h>
#include <cstdlib>
//......自行添加其他必要的库
using namespace std;
#define num 100
//各种排序方法的函数声明,有兴趣选做注释部分排序算法
//template<class T>
//void BubbleSort(vector<T> &x);//冒泡排序
//template<class T>
//void SelectSort(vector<T> &x);//选择排序
//template<class T>
//void InsertSort(vector<T> &x);//插入排序
//template<class T>
//void ShellSort(vector<T> &x);//希尔排序
//template<class T>
//void CountedSort(vector<T> &x);//计数排序
//template<class T>
//void RadixSort(vector<T> &x);//基数排序
//template<class T>
//void BSTSort(vector<T> &x);//选做:二叉查找树排序 template<class T>
void QuickSort(vector<T> &x, int first, int end);//快速排序
template<class T>
void HeapSort(vector<T> &x);//堆排序
template<class T>
void Heapfy(vector<T> &x, int first, int end); //堆调整 template<class T>
void Display(vector<T> &x);
template<class T>
void ResetData(vector<T> &x,vector<T> &y);//使用y重置x
//......自行添加其他必要的函数声明
int main()
{
clock_t start,finish;
double totaltime; vector<int> a(num+),b(num+);
int i;
srand(time());//随机数种子初始化 for(i=;i<=num;i++)
{ a[i]=rand()%+; //随机生成1-10000内的数值作为排序对象
b[i]=a[i];
} //排序前显示数据
cout<<"排序前"<<endl;
Display(a);
//冒泡排序
// BubbleSort(a);
//cout<<"冒泡排序后"<<endl;
//Display(a);
//选择排序
// ResetData(a,b);
// SelectSort(a);
// cout<<"选择排序后"<<endl;
// Display(a);
//插入排序
// ResetData(a,b);
// InsertSort(a);
// cout<<"插入排序后"<<endl;
// Display(a);
//希尔排序
// ResetData(a,b);
// ShellSort(a);
// cout<<"希尔排序后"<<endl;
// Display(a);
//计数排序
// ResetData(a,b);
// CountedSort(a);
// cout<<"计数排序后"<<endl;
// Display(a);
//基数排序
// ResetData(a,b);
// RadixSort(a);
// cout<<"基数排序后"<<endl;
// Display(a);
//快速排序 ResetData(a,b);
start = clock();
QuickSort(a,,num);
finish = clock();
cout<< endl << "快速排序后"<<endl;
totaltime = (double)(finish-start)/CLOCKS_PER_SEC;
cout << "time: " << totaltime << "s" << endl;
Display(a); //堆排序
ResetData(a,b);
start = clock();
HeapSort(a);
finish = clock();
cout<< endl << "堆排序后"<<endl;
totaltime=(double)(finish-start)/CLOCKS_PER_SEC;
cout << "time: " << totaltime << "s" << endl;
Display(a);
cout << endl;
} template<class T>
void Display(vector<T> &x)
{
for(int i=;i<=num;i++)
{ cout<<x[i]<<" ";
if(i%==) cout<<endl;
}
} template<class T>
void ResetData(vector<T> &x,vector<T> &y)//使用y重置x
{
for(int i=;i<=num;i++)
{
x[i]=y[i];
}
} template<class T>
void QuickSort(vector<T> &x, int first, int end){
if( first<end ){
int i=first;
int j=end;
T t=x[i];
while( i<j ){
while( i<j && x[j] >= t ){
j--;
}
if( i<j ){
x[i++] = x[j];
}
while( i<j && x[i] <= t){
i++;
}
if( i<j ){
x[j--] = x[i];
}
}
x[i] = t;
QuickSort(x, first, i-);
QuickSort(x, i+, end);
}
} template<class T>
void Heapfy(vector<T> &x, int p, int len){
int father = p;
int child = *father;
while( child<=len ){
if( child+ <= len && x[child] < x[child+] ){
child++;
}
if( x[child] > x[father] ){
int t=x[child];
x[child] = x[father];
x[father] = t;
father = child;
child = *father;
}
else break;
}
} template<class T>
void HeapSort(vector<T> &x){
for(int i=num/; i>=; i--){ //构造最大堆,但子结点左右孩子大小不分
Heapfy(x, i, num);
}
for(int i=num; i>; i--){ //构造最小堆,使得最左孩子小于右孩子
int t=x[];
x[] = x[i];
x[i] = t;
Heapfy(x,,i-);
}
}

C++ 排序(未完)的更多相关文章

  1. c++稍微复杂桶排序(未完待续~)

    由于上次的桶排序占用空间太多,这次又有了一个新的办法 直接上代码: #include <bits/stdc++.h> using namespace std; int n; void bu ...

  2. 关于DOM的一些总结(未完待续......)

    DOM 实例1:购物车实例(数量,小计和总计的变化) 这里主要是如何获取页面元素的节点: document.getElementById("...") cocument.query ...

  3. Java开发中的23+2种设计模式学习个人笔记(未完待续)

    注:个人笔记 一.设计模式分三大类: 创建型模式,共五种:工厂方法模式.抽象工厂模式.单例模式.建造者模式.原型模式. 结构型模式,共七种:适配器模式.装饰器模式.代理模式.外观模式.桥接模式.组合模 ...

  4. odoo11 model+Recordset 基础未完待续

    Model 一个模型代表了一个业务对象 本质上是一个类,包含了同django flask一样的数据字段 所有定义在模型中的方法都可以被模型本身的直接调用 现在编程范式有所改变,不应该直接访问模型,而是 ...

  5. 数据库索引(Index)【未完待续】

    数据库索引是啥?有什么用?原理是什么?最佳实践什么? 索引是啥 一个索引是这样的数据结构:从数据上来说,不仅包含了从表中某一列或多列的数据拷贝,同时,还包含了指向这列数据行的链接: 从结构上来说,索引 ...

  6. 堆学习笔记(未完待续)(洛谷p1090合并果子)

    上次讲了堆,别人都说极其简单,我却没学过,今天又听dalao们讲图论,最短路又用堆优化,问懂了没,底下全说懂了,我???,感觉全世界都会了堆,就我不会,于是我决定补一补: ——————来自百度百科 所 ...

  7. javascript有用小功能总结(未完待续)

    1)javascript让页面标题滚动效果 代码如下: <title>您好,欢迎访问我的博客</title> <script type="text/javasc ...

  8. ASP.NET MVC 系列随笔汇总[未完待续……]

    ASP.NET MVC 系列随笔汇总[未完待续……] 为了方便大家浏览所以整理一下,有的系列篇幅中不是很全面以后会慢慢的补全的. 学前篇之: ASP.NET MVC学前篇之扩展方法.链式编程 ASP. ...

  9. 我的SQL总结---未完待续

    我的SQL总结---未完待续 版权声明:本文为博主原创文章,未经博主允许不得转载. 总结: 主要的SQL 语句: 数据操作(select, insert, delete, update) 访问控制(g ...

随机推荐

  1. python爬取“美团美食”汕头地区的所有店铺信息

    一.目的 获取美团美食每个店铺所有的评论信息,并保存到数据库和本地 二.实现步骤 获取所有店铺的poiId 首先观察详情页的url,后面是跟着一串数字的,而这一串数字代表着每个店铺特有的id号,我们称 ...

  2. boost tuple

    boost::tuple is a generalized version of std::pair. While std::pair can only store exactly two value ...

  3. _stdcall

    __cdecl __fastcall与__stdcall,三者都是调用约定(Calling convention),它决定以下内容:1)函数参数的压栈顺序,2)由调用者还是被调用者把参数弹出栈,3)以 ...

  4. supermarket

    题目链接 题意:给你n个商品,商品的利润和商品的过期时间,商品必须在过期时间内卖才能算利润,每天只能卖一件商品,问利润最大值. 思路:用并查集+贪心的思路,先给商品从大到小排序,然后选择过期时间的根节 ...

  5. Sqlachemy的警告SAWarning: The IN-predicate on "sns_object.BIZ_ID" was invoked with an empty sequence. This results in a contradiction, which nonetheless can be expensive to evaluate.

    我在使用db_session.query,查询的时候idlist是个空值时候,执行下面的语句就会出现警告.其中后面delete(synchronize_session=False)是删除前面的一堆查询 ...

  6. Java学习之多线程(线程安全问题及线程同步)

    一.线程安全问题产生前提:1.多线程操作共享数据2.线程任务中有多条代码 class Ticket implements Runnable { //2.共享数据 private int num = 1 ...

  7. linux远程管理器 - xshell和xftp使用教程(zhuan)

    准备好连接linux服务器的工具,推荐用xshell和xftp. xshell 是一个强大的安全终端模拟软件,它支持SSH1, SSH2, 以及Microsoft Windows 平台的TELNET ...

  8. k8s-启动、退出动作

    vim post.yaml apiVersion: v1 kind: Pod metadata: name: lifecycle-demo spec: containers: - name: life ...

  9. PAT甲级【2019年9月考题】——A1163 PostfixExpression【25】

    7-3 Postfix Expression (25 分) Given a syntax tree (binary), you are supposed to output the correspon ...

  10. 深度学习大规模MIMO中的功率分配

    摘要-本文使用深度学习的方法在大规模MIMO网络的下行链路中执行max-min和max-prod功率分配.更确切地说,与传统的面向优化的方法相比,训练深度神经网络来学习用户设备(UE)的位置和最优功率 ...