当前位置:网站首页>Implementation of heap sort and quick sort principle
Implementation of heap sort and quick sort principle
2022-06-24 21:58:00 【Weng Weiqiang】
"I love computer technology"
----2021.8.25
Heap sort :
#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)
{
// Avoid array length out of bounds , Do not execute if it exceeds the limit
assert(_count + 1 <= capacity);
data[_count + 1] = item;
_count++;
// After insertion May destroy the structure of the heap So do the backtracking operation
percolate_up(_count);
}
// Considering the index location ,k Value must be greater than 1 Can have a parent node
void percolate_up(int k)
{
while (k > 0 && data[k / 2] < data[k])// Compare with its parent node , Make sure that the parent nodes are all large
{
swap(data[k / 2], data[k]);
k = k / 2;
}
}
T extractMax()
{
assert(_count > 0);
T ret = data[1];
swap(data[1], data[_count]);// Delete the first Let it swap with the first one
_count--;
percolate_down(1);
return ret;
}
void percolate_down(int k)
{
while (2 * k <= _count)
{
int j = 2 * k;
// Select the largest of the left and right nodes
if (j + 1 <= _count && data[j + 1] > data[j])
{
j = j + 1;
}
if (data[k] >= data[j])
{
break;
}
// Exchange when you are younger than it
swap(data[k], data[j]);
k = j;
}
}
};
// Push sort implementation
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);// Enter your capacity
heapSort(a, 10);
for (int i = 1; i <= 10; ++i)
{
cout << a[i] << " ";
}
}
Quick sort :
#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)
{
// If key When the value is set to the leftmost Be sure to start from right to left Conversely, if key Value on the right Search from right
while (v[j] >= base && i < j)// If decreasing, change to while(v[j]<=base&&i<j){j--;}
{
j--;
}
while (v[i] <= base && i < j)// If increasing, change to 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 << " ";
}
}
边栏推荐
- Excel布局
- VSCode无网环境快速迁移开发环境(VIP典藏版)
- Réduire le PIP à la version spécifiée (mettre à jour le PIP avec pycharm avant de le réduire à la version originale)
- 并查集+建图
- 优雅的自定义 ThreadPoolExecutor 线程池
- [theory] deep learning in the covid-19 epic: a deep model for urban traffic revitalization index
- 排查到解决问题的过程:浏览器突然无法访问网页,错误代码:0x80004005,最终定位:“电脑打开热点,电脑就不能上网了”
- Redis+Caffeine两级缓存,让访问速度纵享丝滑
- 滤波数据分析
- 即构「畅直播」上线!提供全链路升级的一站式直播服务
猜你喜欢
Multithreaded finalization
滤波数据分析
想当测试Leader,这6项技能你会吗?
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
The collection of zero code enterprise application cases in various industries was officially released
二叉搜索树模板
Installing Oracle without graphical interface in virtual machine centos7 (nanny level installation)
面试官:你说你精通Redis,你看过持久化的配置吗?
Multiplexer select
openGauss内核:简单查询的执行
随机推荐
Suspend component and asynchronous component
降低pip到指定版本(通过cmp升级pip,在降低到原来版本)
leetcode_1365
【论】A deep-learning model for urban traffic flow prediction with traffic events mined from twitter
A field in the database is of JSON type and stores ["1", "2", "3"]
The most important thing at present
leetcode1720_2021-10-14
印刷行业的ERP软件的领头羊
After Firefox drag and drop, Baidu search will always be opened by default. If it is a picture, the picture will be opened.
Transport layer UDP & TCP
02---纵波不可能产生的现象
Several classes of manual transactions
985测试工程师被吊打,学历和经验到底谁更重要?
力扣每日一题-第25天-496.下一个更大元素Ⅰ
Object.defineProperty和Reflect.defineProperty的容错问题
Byte software testing basin friends, you can change jobs. Is this still the byte you are thinking about?
leetcode_1470_2021.10.12
在每个树行中找最大值[分层遍历之一的扩展]
STL+树
Bld3 getting started UI