Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement heap sorting on C++

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to achieve heap sorting on C++". In daily operation, I believe many people have doubts about how to achieve heap sorting on C++. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how to achieve heap sorting in C++". Next, please follow the editor to study!

Overview:

Heap sorting uses the method of building a "heap" to determine the data element with the maximum value and exchange that element with the element at the last position. Any sequence composed of n data elements can be arranged in layers from left to right to form a complete binary tree corresponding to the sequence.

A complete binary tree is a heap if and only if the root value of each subtree of a complete binary tree is ai ≥, the root value of its left subtree is a2i, and the root value of ai ≥ 's right subtree is a2i + 1 (10Tet -) {/ / adjustment heap, where jack0 swap (array [0], array [j]) is not required; HeapAdjust (array,0,j) / / because after each exchange, the maximum value is taken out (no longer participating in adjusting the heap), the third parameter should be written j instead of length Print (array,length);}}

Complete code:

/ / Heap sort # include using namespace std;void Print (int array [], int length) {/ / print the sequence for every time it is executed

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report