当前位置:网站首页>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 << " ";
}
}
边栏推荐
- leetcode_ 191_ 2021-10-15
- Transport layer UDP & TCP
- Kubernetes 集群中流量暴露的几种方案
- Redis+Caffeine两级缓存,让访问速度纵享丝滑
- Fuzhou business office of Fujian development and Reform Commission visited the health department of Yurun university to guide and inspect the work
- 降低pip到指定版本(通過PyCharm昇級pip,在降低到原來版本)
- 在每个树行中找最大值[分层遍历之一的扩展]
- How to resolve the 35 year old crisis? Sharing of 20 years' technical experience of chief architect of Huawei cloud database
- Bld3 getting started UI
- Visit Amazon memorydb and build your own redis memory database
猜你喜欢

直击“三夏”生产:丰收喜报频传 夏播紧锣密鼓

EasyBypass
![[camera Foundation (I)] working principle and overall structure of camera](/img/5d/c29d636a90d01e5c3852df2a0dd833.png)
[camera Foundation (I)] working principle and overall structure of camera

SAP接口debug设置外部断点

Several classes of manual transactions

【Camera基础(二)】摄像头驱动原理和开发&&V4L2子系统驱动架构
![[精选] 多账号统一登录,你如何设计?](/img/df/9b4fc11a6971ebe8162ae84250a782.png)
[精选] 多账号统一登录,你如何设计?

Réduire le PIP à la version spécifiée (mettre à jour le PIP avec pycharm avant de le réduire à la version originale)

socket(1)
Visit Amazon memorydb and build your own redis memory database
随机推荐
【吴恩达笔记】卷积神经网络
【OpenCV 例程200篇】209. HSV 颜色空间的彩色图像分割
【吴恩达笔记】机器学习基础
leetcode_191_2021-10-15
Installing Oracle without graphical interface in virtual machine centos7 (nanny level installation)
双链表实现
【无标题】
leetcode1863_2021-10-14
[featured] how do you design unified login with multiple accounts?
[untitled]
leetcode_1470_2021.10.12
Introduce the overall process of bootloader, PM, kernel and system startup
How to achieve energy conservation and environmental protection of full-color outdoor LED display
【无标题】
Collapse code using region
EasyBypass
想当测试Leader,这6项技能你会吗?
01--- conditions for interference of two trains of waves at the meeting place
CV2 package guide times could not find a version that satisfies the requirement CV2 (from versions: none)
C language - keyword 1