C/C++ basics: heap sort

When looking for the largest or smallest elements in a large amount of data, heap sorting is often very efficient. So how is heap sorting implemented? First of all, to sort by the heap, you must build a heap, and then you have to understand the ascending order, should you build a large heap or a small heap?

For heap sort, we must be clear about the following points:

1. Usually we build large piles in ascending order and small piles in descending order;

2. After the heap is built, it is time to sort the heap;

Take ascending order as an example: first build a large pile of this group of data, and then exchange the top of the pile with the last element (the top element must be the largest number in the heap), and the largest number will be transferred to the end. Then the heap that does not contain the last element is adjusted downwards from the root node, so that it still meets the characteristics of a large heap;

When the next adjustment is made, the top element of the heap and the last element are still exchanged, and the heap that does not contain the last element is adjusted downward from the root node... until the number of elements is reduced to 0.

First implement a large pile of ascending order:

#include<iostream>

#include

using namespace std;

void AdjustDown(int*a, size_t root, size_t size)

{

    size_t parent = root;

    size_t child = parent * 2+ 1;

    while(child < size)

    {

        if(child + 1< size && a[child] < a[child + 1])

        {

            ++child;

        }

        if(a[parent] < a[child])

        {

            swap(a[parent], a[child]);

            parent = child;

            child = parent*2+ 1;

        }

        else

        {

            break;

        }

    }

}



voidHeapSort(int* a,intsz)

{

    assert(a);

    for(inti=(sz-2)/2;i>=0;--i)  //建堆,下调

    {

        AdjustDown(a,i,sz);

    }

    /*int end=sz-1;

    while (end>0)

    {

    std::swap(a[0],a[end]);     //这里的end代表元素下标,为最后一个元素

    AdjustDown(a,0,end);       //这个end代表需要下调的元素个数,第一次交换后只需要调sz-1个元素,不包括最后一个元素

    --end;                     //注意每次交换元素后,下调元素个数都在减小(范围缩小)

    }*/

    for(size_t i=0;i<sz;++i) for=""i="0;i<sz;++i)"int=""pre=""return=""sz="sizeof(a)/sizeof(a[0]);"void=""><p><strong>由于降序与升序的实现类似,所以采用仿函数的方式,增加代码的复用性。</strong></p><pre class="brush:java;">#pragma once

#include<iostream>

#include

using namespace std;

//升序建大堆,降序建小堆

template<classt="">

struct UpOrder

{

    bool operator()(constT& l,constT& r)

    {

        returnl<r; t="">

struct DownOrder

{

    bool operator()(constT& l,constT& r)

    {

        returnl>r;

    }

};



template<classcompare="UpOrder<T">>

classHeapSort

{

public:

    voidSort(T* a,size_t size)

    {

        assert(a);

        for(inti=((int)size-2)/2;i>=0;--i)

        {

            AdjustDown(a,i,size);

        }

        for(size_t i=0;i<size;++i) 1=""child="parent*2+1;"compare=""else=""if=""parent="root;"pre=""protected:=""size=""size_t=""void="">



<strong>测试代码</strong>:<pre class="brush:java;">#include"HeapSort.h"



voidTest1()

{

    HeapSort<int> h;  

    inta[] = {10,11,13,12,16,18,15,17,14,19};

    intsz=sizeof(a)/sizeof(a[0]);

    h.Sort(a,sz);

    for(inti=0;i<sz;++i) int=""void="">> h1;

    inta[] = {10,11,13,12,16,18,15,17,14,19};

    intsz=sizeof(a)/sizeof(a[0]);

    h1.Sort(a,sz);

    for(inti=0;i<sz;++i) int=""pre=""return=""><p> </p>

</sz;++i)></sz;++i)></int></pre>

</size;++i)></class></r;></class></assert.h></iostream></pre>

</sz;++i)></assert.h></iostream>


In addition, if you want to better improve your programming ability, learn C language and C++ programming! Overtaking in a curve, one step faster! I may be able to help you here~

UP has uploaded some video tutorials on learning C/C++ programming on the homepage. Those who are interested or are learning must go and take a look! It will be helpful to you~

Sharing (source code, actual project video, project notes, basic introductory tutorial)

Welcome partners who change careers and learn programming, use more information to learn and grow faster than thinking about it yourself!

Programming learning:

Programming learning:

 

Guess you like

Origin blog.csdn.net/weixin_45713725/article/details/115207871