Talk about HeapSort HEAPSORT thought, and personal optimization. (Old thing)

I hear you're rafts HeapSort billion data it?

Introduction: Come, today we say it is a basis of comparison for row sorting large amounts of data used in it ~

Note: Read this article to learn new skills requirements for the front: to understand what is binary array and nature, if not meet the requirements of the students please read the top right corner of Tu Momo points like × = - = ~

Before that we first look at a map

Ah, is not it fun? This stuff is called HeapSort (heap sort)

And we are concerned that today, like the legend as to how we operate it sort shown in ~

HeapSort called it, that is, has been doing one thing, it points with elegant term is called: HeapAdjust (adjust the maximum heap)

Question Before explaining the maximum stack first, what is heap it?
Sand, mound, heap, what are the characteristics of these things is it?
Like the pyramids, the content is decreasing from the bottom to the top. And I said maximum heap, that is to say in a heap
and the maximum heap contents (sand or stone known as Key) showing its value (value) maximum top of the heap, and then down progressively diminishing.

So, how did this maximum heap it?

Below I posted some code from Baidu Encyclopedia bar ~
Baidu Encyclopedia - heapsort

// array 是待调整的堆数组, i 是待调整的数组元素的位置, length 是数组的长度
// 本函数功能是:根据数组 array 构建大根堆
void HeapAdjust(int array[], int i, int length)
{
    for(int Child, Temp; 2 * i + 1 < length; i = Child)
    {
        //子结点的位置 = 2 *(父结点位置)+ 1
        Child = 2 * i + 1;
        //得到子结点中较大的结点
        if(Child + 1 < length && array[Child + 1] > array[Child]) ++Child;
        //如果较大的子结点大于父结点那么把较大的子结点往上移动,替换它的父结点
        if(array[i] < array[Child]) Temp = array[i], array[i] = array[Child], array[Child] = Temp; 
        //否则退出循环
        else break;
    }
}
//堆排序算法
void HeapSort(int array[], int length)
{
    int i;
    // 调整序列的前半部分元素,调整完之后第一个元素是序列的最大的元素
    // length / 2 - 1 是最后一个非叶节点,此处"/"为整除
    for(i = length / 2 - 1; i >= 0; --i) HeapAdjust(array, i, length);
    // 从最后一个元素开始对序列进行调整,不断的缩小调整的范围直到第一个元素
    for(i = length - 1; i > 0; --i)
    {
        // 把第一个元素和当前的最后一个元素交换,保证当前的最后一个位置的元素都是在现在的这个序列之中最大的
        array[i] ^= array[0], array[0] ^= array[i], array[i] ^= array[0];
        // 不断缩小调整heap的范围,每一次调整完毕保证第一个元素是当前序列的最大值
        HeapAdjust(array, 0, i);
    }
}

All work to be done in there, appears to be quite streamlined I think = - =.

That is how we say it works it ~

First HeapSort this function to do the first time preprocessing, the purpose of the work is to bring this array is adjusted to the maximum heap

That reverse execution HeapAdjust

A penultimate but not started, but the presence of the inverse of the tree node child nodes in the second layer begins (length / 2 - 1).

What does it mean?

This is because HeapAdjust operating principle is:

First, the left and right nodes selected larger nodes, and then compared with the parent node, if the sum is greater than the Swap (in this case the maximum stack construct).

Then perform the above operation is repeated for the current sub-node.

Well, when the reverse order to zero, the maximum heap on the success of this built up, what now need to do?

Now known conditions, the maximum heap top of this, then, the maximum value and the last element of the array exchange (boundary minus one),
but this time the maximum heap and need to maintain, and then how to do it?

This time we must immediately make a HeapAdjust adults, you have to tell it you want to adjust the position of the element and its borders.

Make this element the current re-adjusted to the maximum, ah, now, thanks to the Almighty HeapAdjust adults.

Well, it's all there thinking of the heap sort.

So far, this chapter has ended, the following is my personal hidden content (/w \)(some people will really see here? No kidding me, right ~)

To see the students here are basic in love with classmate computer, since it can see here, let me point details of the issues now -
in fact, the code provides very LOW Baidu Encyclopedia, the following will release my Code it ~

    void HeapAdjust(int arr[], int pos, int len)
    {
        int keypos = (pos << 1) + 1, KeyElement = arr[pos];//keypos 为其左子节点位置, KeyElement 为当前需要调整位置的元素.
        while(keypos < len)//检查左子结点是否越界.
        {
            if (keypos + 1 < len && arr[keypos] < arr[keypos + 1]) keypos++;//若右结点存在且比左结点更大则替换
            if (KeyElement > arr[keypos]) break;//若不存在比KeyElement更大的的子结点则中断调整位置
                       arr[pos] = arr[keypos], pos = keypos, keypos = (pos << 1) + 1;//将当前节点覆盖其父节点,同时更新当前结点为其子节点
        }
        arr[pos] = KeyElement;//最后确定位置后归位
    }  

Here is my improved version, then, there is the expense of further optimization of space N.

    while (true)
    {
        //因数组数据特殊性,当树结点单枝时一定存在左结点(pos + 1 >= max),则pos不变动
        if(arr[pos] < arr[pos + 1] && pos + 1 < max) pos++;//若右结点存在且大于则替换为右结点
        //将进一步移动到其子节点
        arr[pos - 1 >> 1] = arr[pos], pos = (pos << 1) + 1;
        //left and right node all is zero to break;
        if (pos > max || (!arr[pos] && !arr[pos + 1]))
        {
            //assume zero is side element
            arr[pos - 1 >> 1] = 0;
            break;
        }    
    } 

I have yet to find someone to do the same and to improve eh hey, improved principle is:
When the top of the heap element pulled out, then the following sub-nodes around the larger elements will be put up, and finally to the end (boundary), assigned to it with 0, after this, it will interrupt further adjustment when it comes to their child nodes of two child nodes 0 (side element) operation.

Theoretically, this sacrifice space than the traditional practice of N (lots of repeated HeapAdjust) more economical operation, the price goes just a waste of space to store N data.

Little mention here what is now the C ++ STL sort ()
It uses QuickSort, HeapSort, InsertionSort binding
general term introsort (introsort).

Finally, sincere thanks LYC squad Y students study day and night with me and toss well to modify, that there is no participation with him would say I feel very boring, as the students then go play machine L, and previously Astar him to play bad , or give him the freedom to play better space = - = ~

I wanted to make improved code published to the wiki, but now that I think still remain in this space did not wait until people are concerned about it one day and then spend it available = - = ~

Two years later, ah, that year I learned are some hell!

Guess you like

Origin www.cnblogs.com/juwan/p/11449003.html