Sorting Algorithm 04: heap sorting

Source: Internet
Author: User

I recently reviewed data structure and algorithm analysis and found that the heap sorting algorithm is incomplete. Let's take a look.

Reference http://www.cnblogs.com/dolphin0520/archive/2011/10/06/2199741.html

# Include <stdio. h> # define N 10 // The number of elements in the binary heap is N, so the last non-leaf node is a [n/2-1], during Adjustment, int A [n] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0} must be exchanged from the last non-leaf node. Void swap (int * X, int * Y) {int TMP; TMP = * X; * x = * Y; * Y = TMP;} void print () {printf ("Swap :"); for (INT I = 0; I <n; I ++) {printf ("% d", a [I]) ;}printf ("\ n ");} // recursively adjust the sequence void heapadjust (int m, int t) {int leftchild = 2 * m + 1; int rightchild = 2 * m + 2; if (leftchild <t & A [m] <A [leftc Hild]) {swap (& A [m], & A [leftchild]); print ();} if (rightchild <t & A [m] <A [rightchild]) {swap (& A [m], & A [rightchild]); print ();} if (leftchild <t) heapadjust (leftchild, T); If (rightchild <t) heapadjust (rightchild, T); // print ();} void buildheap () {for (INT mid = n/2-1; Mid> = 0; Mid --) {heapadjust (MID, n);} printf ("buildheapfinished \ n ");} void heapsort () {// first arrange the order buildheap () for the initial heap; // then cycle based on the number of remaining elements. A maximum value is deleted in each loop, The maximum value is exchanged with the top element of the heap. // When only one element is cyclically stopped, this element is the smallest value for (INT I = n-1; I> = 1; I --) {printf ("--------------------- \ n"); // exchange the first and last swap (& A [0], & A [I]); print (); heapadjust (0, I) ;}} int main () {print (); heapsort (); print (); Return 0 ;}

In fact, it is easy to get started by clarifying the Train of Thought. The code above is not too concise. If it is good, no problems are found in the test, and the results of each exchange are also listed, as shown in:

 

To meet the requirements of the input file, change the code above and pass the arrays A [] and N as parameters to the function.

# Include "heapsort. H "# include" print. H "Void swap (int * X, int * Y) {int TMP; TMP = * X; * x = * Y; * Y = TMP ;} // recursively adjust the sequence void heapadjust (int A [], int N, int M, int t) {int leftchild = 2 * m + 1; int rightchild = 2 * m + 2; If (leftchild <t & A [m] <A [leftchild]) {swap (& A [m], & A [leftchild]); // print (A, n);} If (rightchild <t & A [m] <A [rightchild]) {swap (& A [m], & A [rightchild]); // print (A, n);} If (leftchild <t) heapadj Ust (A, N, leftchild, T); If (rightchild <t) heapadjust (A, N, rightchild, T); // print ();} void buildheap (int A [], int N) {for (INT mid = n/2-1; Mid> = 0; Mid --) {heapadjust (A, N, mid, n);} // printf ("buildheapfinished \ n");} void heapsort (int A [], int N) {// sort the order buildheap (, n); // The number of remaining elements is used for loop. A maximum value is deleted in each loop. The maximum value is exchanged with the top element of the heap. // When only one element is cyclically stopped, this element is the smallest value for (INT I = n-1; I> = 1; I --) {// printf ("--------------------- \ n"); // exchange the first and last swap (& A [0], & A [I]); // print (A, n); heapadjust (A, N, 0, I );}}

Time consumed = 4694 Ms.

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.