当前位置:网站首页>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 << " ";
}
}
边栏推荐
猜你喜欢

Blender FAQs

You are using pip version 21.1.2; however, version 22.1.2 is available

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

【吴恩达笔记】机器学习基础

Application practice | massive data, second level analysis! Flink+doris build a real-time data warehouse scheme

想当测试Leader,这6项技能你会吗?

C语言-关键字1

煮茶论英雄!福建省发改委、市营商办领导一行莅临育润大健康事业部交流指导

【吴恩达笔记】多变量线性回归

权限想要细化到按钮,怎么做?
随机推荐
[theory] deep learning in the covid-19 epic: a deep model for urban traffic revitalization index
Drag drag drag
网络层 && IP
suspense组件和异步组件
如何化解35岁危机?华为云数据库首席架构师20年技术经验分享
手动事务的几个类
虚拟机CentOS7中无图形界面安装Oracle(保姆级安装)
leetcode-201_2021_10_17
leetcode:1504. 统计全 1 子矩形的个数
LeetCode-513. 找树左下角的值
CV2 package guide times could not find a version that satisfies the requirement CV2 (from versions: none)
【吴恩达笔记】机器学习基础
Bld3 getting started UI
权限想要细化到按钮,怎么做?
leetcode-201_ 2021_ 10_ seventeen
03--- antireflective film
[精选] 多账号统一登录,你如何设计?
leetcode_ 1470_ 2021.10.12
建木持续集成平台v2.5.0发布
多线程收尾