Heap sorting of algorithms and data structures

Source: Internet
Author: User

1. A linear array can be seen as a two-difference heap. The key code relationship between the parent node and the left child in the heap is: Left = parent * 2 + 1;

Heap sorting thought: each cycle moves the shiftup largest element from the binary heap to the root node, and then exchanges the root node with the last element at the end. Leave the end element aside, so that you can continue to find the next largest element and swap it with the end element. Repeat the largest element here and switch.

1. Heap sorting is similar to insert sorting.

2. the time complexity is nlog2 (N );

Int heap [] = {53,17, 78,9, 45,65, 87,23}; void swap (int * a, int * B) {int temp = * A; * A = * B; * B = temp;} void getmax (INT Cus, int end) {While (CuS> = 0) {int left = Cus * 2 + 1; int max = left; if (max <End & heap [left]  

 

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.