当前位置:网站首页>堆排序和快速排序原理实现
堆排序和快速排序原理实现
2022-06-24 19:28:00 【翁炜强】
"I love computer technology"
----2021.8.25
堆排序:
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include <assert.h>
using namespace std;
template<typename T>
class MaxHeap
{
private:
T* data;
int _count;
int capacity;
public:
MaxHeap(T cap)
{
data = new T[cap + 1];
_count = 0;
this->capacity = cap;
}
~MaxHeap()
{
delete[]data;
}
int size()
{
return _count;
}
bool isEmpty()
{
return _count == 0;
}
void insert(T item)
{
//避免数组长度越界,若越界则不执行
assert(_count + 1 <= capacity);
data[_count + 1] = item;
_count++;
//进行插入操作后 可能破坏堆的结构 所以进行上溯操作
percolate_up(_count);
}
//考虑到索引位置的问题,k值必须大于1才能有父节点
void percolate_up(int k)
{
while (k > 0 && data[k / 2] < data[k])//与其父节点进行比较,确保父节点都是大的
{
swap(data[k / 2], data[k]);
k = k / 2;
}
}
T extractMax()
{
assert(_count > 0);
T ret = data[1];
swap(data[1], data[_count]);//删除第一个 让它和第一个进行交换
_count--;
percolate_down(1);
return ret;
}
void percolate_down(int k)
{
while (2 * k <= _count)
{
int j = 2 * k;
//选取左右节点中最大的
if (j + 1 <= _count && data[j + 1] > data[j])
{
j = j + 1;
}
if (data[k] >= data[j])
{
break;
}
//比它小就交换
swap(data[k], data[j]);
k = j;
}
}
};
//推排序实现
template<typename T1>
void heapSort(T1 arr[], int n)
{
MaxHeap<T1> maxHeap = MaxHeap<T1>(n);
for (int i = 0; i < n; i++)
{
maxHeap.insert(arr[i]);
}
for (int j = n - 1; j >= 0; j--)
{
arr[j] = maxHeap.extractMax();
}
}
int main()
{
int a[10] = { 3,5,2,4,9,10,11,13,16,2 };
//heapSort(a, 10);//输入你的容量
heapSort(a, 10);
for (int i = 1; i <= 10; ++i)
{
cout << a[i] << " ";
}
}快速排序:
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
template<class T>
void quickSort(int left, int right, vector<T>& v)
{
if (left >= right) { return; }
int i = left, j = right;
int base = v[left];
while (i < j)
{
//假如key值设置为最左侧时 一定要先从右往左 反之若key值在右侧 则从右往搜索
while (v[j] >= base && i < j)//若递减则改为while(v[j]<=base&&i<j){j--;}
{
j--;
}
while (v[i] <= base && i < j)//若递增则改为whike(v[j]>=base&&i<j){i++}
{
i++;
}
swap(v[i], v[j]);
}
v[left] = v[i];
v[i] = base;
quickSort(left, i - 1, v);
quickSort(i + 1, right, v);
}
int main()
{
vector<int> v = { 6,1,2,7,9,3,4,5,10,8 };
quickSort(0, v.size() - 1, v);
for (auto it : v)
{
cout << it << " ";
}
}
边栏推荐
- 【Camera基础(二)】摄像头驱动原理和开发&&V4L2子系统驱动架构
- Wireshark packet capturing skills summarized by myself
- EasyBypass
- Apple mobile phone can see some fun ways to install IPA package
- TDengine可通过数据同步工具 DataX读写
- Mysql优化查询速度
- Call process of package receiving function
- 使用Adb连接设备时提示设备无权限
- SAP接口debug设置外部断点
- 介绍BootLoader、PM、kernel和系统开机的总体流程
猜你喜欢

应用实践 | 海量数据,秒级分析!Flink+Doris 构建实时数仓方案

CondaValueError: The target prefix is the base prefix. Aborting.

虚拟机CentOS7中无图形界面安装Oracle(保姆级安装)

BPF_ PROG_ TYPE_ SOCKET_ Filter function implementation

Pattern recognition - 9 Decision tree

Auto. JS to automatically authorize screen capture permission

【论】A deep-learning model for urban traffic flow prediction with traffic events mined from twitter

自己总结的wireshark抓包技巧

188. the best time to buy and sell stocks IV

【Camera基础(一)】Camera摄像头工作原理及整机架构
随机推荐
TCP Jprobe utilization problem location
Implementation of adjacency table storage array of graph
Structured interview of state-owned enterprises and central enterprises employment of state-owned enterprises Modou Interactive Employment Service steward
[product design and R & D collaboration tool] Shanghai daoning provides you with blue lake introduction, download, trial and tutorial
Functional analysis of ebpf tracepoint
Datakit 代理实现局域网数据统一汇聚
Introduce the overall process of bootloader, PM, kernel and system startup
Make tea and talk about heroes! Leaders of Fujian Provincial Development and Reform Commission and Fujian municipal business office visited Yurun Health Division for exchange and guidance
Functional analysis of ebpf sockops
HCIA assessment
socket(1)
Memcached comprehensive analysis – 5 Memcached applications and compatible programs
Blender's simple skills - array, rotation, array and curve
[精选] 多账号统一登录,你如何设计?
Unity about conversion between local and world coordinates
传输层 udp && tcp
Dynamic routing protocol rip, OSPF
Static routing experiment
VSCode无网环境快速迁移开发环境(VIP典藏版)
leetcode_1470_2021.10.12