Heap sorting program (there are several errors when writing the program. For details, see comments. It has been debugged through dev c ++)

Source: Internet
Author: User

# Include <stdio. h>
# Include <conio. h>
Void HeapAdjust (int a [], int s, int n) // large top heap
{
Int temp = a [s];
For (int j = 2 * s; j <= n; j * = 2)
{
If (j <n & a [j] <a [j + 1]) j ++;
If (temp> a [j]) break; // This error has plagued me for a long time. Temp is written as a [s]. If you forget that s [s] will change.
A [s] = a [j]; // reversed at first
S = j;
}
A [s] = temp;

}
Void HeapSort (int a [], int len)
{
For (int I = len/2; I> 0; -- I)
{
HeapAdjust (a, I, len );
} // Create an initial heap
For (int I = len; I> 1; -- I)
{
Int temp = a [1];
A [1] = a [I];
A [I] = temp;

HeapAdjust (a, 1, I-1 );
}
}
Int main ()
{
Int a [11] = {, 0 };
Printf ("Initial Sequence ");
For (int I = 1; I <11; I ++)
{
Printf ("% d", a [I]);
}

HeapSort (a, 10 );
Printf ("sorted sequence ");
For (int I = 1; I <11; I ++)
{
Printf ("% d", a [I]);
}
Getch ();
Return 0;

}

From: coder blog that does not like to stay up late

Related Article

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.