Heap sorting Learning

Source: Internet
Author: User

A few nonsense first:

 

I recently studied data structures and algorithms. Why did I learn them? First, I have seen the bottleneck of my current learning, I 've been stuck in using other people's frameworks, algorithms, and other things, and have not studied programs in a deeper level. Second, if you want to take a longer time in the IT industry, the data structure must be a hurdle.

In addition, I have entered JavaScript since my previous blog, and I have not discussed or seldom discussed. NET platform related content.

 

Subject:

 

Arr. Heap-size

As for what is going on here, I am not talking about it anymore. You know this blog by default. If you are a Daniel, please ignore this blog or look for my fault. I am very welcome to it :)

Before heap sorting, we need to first know that a very basic thing is arr. Heap-size. I didn't pay much attention to it at the beginning. I thought it was not equal to ARR. length? In addition, it is not very useful to build a stack at the beginning of learning, but it plays a major role in optimizing performance in subsequent sorting. It defines the number of valid elements in an array arr. In other words, arr [1, arr. length] may have data, but in arr [1, arr. heap-size] Stores valid data.

Max heap

In some books, the definition of the largest heap is a bit cool (I think). To put it bluntly, apart from the root node, the root node of any child is the largest than that of its child. The smallest heap is its reverse direction. The smallest heap is used to construct a priority queue. I will not mention it here. After all, it is now, and tomorrow we have to get up early .. This will be explained in future blog posts. The nature of the maximum heap is what it defines. the time complexity of maintaining the maximum heap is log2 N. Here N is not a square !!

Let's take a look at how to maintain the nature of a heap:

The following code uses Js. After all, JavaScript is used every day, and all languages are the same:

var arr = [4,1,3,2,16,9,10,14,8,7],    heapLen = arr.length,    halfLen = heapLen / 2,    laster;function buidHeap(i){var left = 2 * i + 1,right = 2 * i + 2;if(left < heapLen && arr[left] > arr[i]){laster = left;}else{laster = i;}if(right < heapLen && arr[right] > arr[laster]){laster = right;}if(laster !== i){var temp;temp = arr[i];arr[i] = arr[laster];arr[laster] = temp;buidHeap(laster);}}

The main idea of maintaining the max heap is to always stick to its nature. We select the largest in arr [I], arr [left], and ARR [right, it also saves its subscript in Laster, but after the exchange, we can perform recursive calls considering that we still need to maintain its nature. The cost of maintaining a maximum heap is two: first, the replacement of ARR [I], arr [left], and ARR [right] is a constant time, the subtree of any child is less than or equal to 2/3 * n.

So its time t (n) = T (2/3 * n );

We are only maintaining the nature of the largest heap above. How can we build the heap?

var arr = [4,1,3,2,16,9,10,14,8,7],    heapLen = arr.length,    halfLen = heapLen / 2,    laster;function buidHeap(i){var left = 2 * i + 1,right = 2 * i + 2;if(left < heapLen && arr[left] > arr[i]){laster = left;}else{laster = i;}if(right < heapLen && arr[right] > arr[laster]){laster = right;}if(laster !== i){var temp;temp = arr[i];arr[i] = arr[laster];arr[laster] = temp;buidHeap(laster);}}function exe(cb){for(var i = heapLen;i >= 0;i--){buidHeap(i);}cb();}

The following EXE () function is a heap building process.

Now our Heap has been built. How can we sort the heap?

Implement heap sorting:

 

 

There are two ways to put the first element that has been flushed into another array, because in the maximum heap, the root node is the largest, therefore, every time we remove the root node, we need to maintain the maximum heap nature. However, copying to a new array consumes two times of space. Although this can achieve the ultimate goal, it is still not very good. The second method is to start with this blog post, not to mention heap-size. It is used here. It indicates a valid element. Since it is valid, after each selection, we put the largest element at the end, and heap-size-1; does this save space?

Code:

VaR arr = [4, 1, 3, 2, 16, 9, 10, 14, 8, 7], heaplen = arr. length, halflen = heaplen/2, Laster; function buidheap (I) {var left = 2 * I + 1, Right = 2 * I + 2; if (left 

Finally, I want to say:

 

This blog post is just a brief description of heap sorting. It has not been further explored about its time complexity. The first reason is that some mathematical symbols cannot be obtained, and the second is: my personal language is not expressive enough. You can Google it.

This blog post is original and can be reproduced, but please indicate the source of the original connection !!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.