Algorithm Pearl -- let's look at the implementation of Heap Sort

Source: Internet
Author: User

 


Heap Sort HeapSort is a tree-like sorting method. The characteristic of heap sorting is that during the sorting process, R [l .. n] As a Complete Binary Tree ordered storage structure, using the internal relationship between the parent node and the child node in the full Binary Tree, select the maximum (or minimum) keyword in the current unordered Area).

The worst time complexity of heap sorting is O (nlogn ). The average performance of heap sequence is closer to the worst performance.
Because the initial heap requires a large number of comparisons, the heap sorting is not suitable for files with a small number of records.
Heap sorting is local sorting, and the auxiliary space is O (1 ),
It is an unstable sorting method.


If you want to select a specific maximum and minimum values from a large amount of data, heap sorting is the best choice.

The basic steps for heap sorting are:

1. Initial heap Creation

2. Swap the heap top element with the first element in the ordered Area

3. Adjust the heap top element and jump to 2. Until all are sorted.

 


Declaration: in the implementation of the following algorithm, the storage of arrays is located in data [1] ------- data [n]

The most important algorithm in this algorithm is the heap adjustment algorithm:


[Cpp]
// Heap Adjustment
// Data [], array to be sorted
// Target, the position of the element to be adjusted
// N, array size
Void AdjustHeap (int data [], int target, int n)
{
Int nChild;
Int nTemp;

NTemp = data [target]; // temporary storage
While (target * 2 <= n)
{
NChild = target * 2; // nChild points to left child
If (nChild + 1 <= n & data [nChild] <data [nChild + 1])
{
NChild ++; // nChild points to a child with a large keyword (check whether there are left children. If yes, compare the left and right children)
}
If (nTemp <data [nChild]) // if the child node is larger than the parent node, move the child node to the parent node
{
Data [target] = data [nChild];
Target = nChild; // reprocess the byte points of the adjusted Node
}
Else break;
}
Data [target] = nTemp; // put the final elements to be adjusted in the appropriate position
}

// Heap Adjustment
// Data [], array to be sorted
// Target, the position of the element to be adjusted
// N, array size
Void AdjustHeap (int data [], int target, int n)
{
Int nChild;
Int nTemp;
 
NTemp = data [target]; // temporary storage
While (target * 2 <= n)
{
NChild = target * 2; // nChild points to left child
If (nChild + 1 <= n & data [nChild] <data [nChild + 1])
{
NChild ++; // nChild points to a child with a large keyword (check whether there are left children. If yes, compare the left and right children)
}
If (nTemp <data [nChild]) // if the child node is larger than the parent node, move the child node to the parent node
{
Data [target] = data [nChild];
Target = nChild; // reprocess the byte points of the adjusted Node
}
Else break;
}
Data [target] = nTemp; // put the final elements to be adjusted in the appropriate position
}
Overall implementation code:


[Cpp]
****************
* Heap Sorting Algorithm
* The subscript of the sort array starts from 1.
*/
# Include <stdio. h>
 
Enum {MAX = 1000 + 1 ,};

Int data [MAX];
Static inline swap (int x, int y)
{/*
X ^ = y;
Y ^ = x;
X ^ = y;
*/
Int tmp;
Tmp = data [x];
Data [x] = data [y];
Data [y] = tmp;
}
// Heap Adjustment
// Data [], array to be sorted
// Target, the position of the element to be adjusted
// N, array size
Void AdjustHeap (int data [], int target, int n)
{
Int nChild;
Int nTemp;

NTemp = data [target]; // temporary storage
While (target * 2 <= n)
{
NChild = target * 2;
If (nChild + 1 <= n & data [nChild] <data [nChild + 1])
{
NChild ++; // nChild points to a child with a large keyword
}
If (nTemp <data [nChild])
{
Data [target] = data [nChild];
Target = nChild; // reprocess the byte points of the adjusted Node
}
Else break;
}
Data [target] = nTemp; // put the final elements to be adjusted in the appropriate position
}
 
// Heap Sorting Algorithm
// Data, array to be sorted
// N, array size
Void HeapSort (int data [], int n)
{
Int I;
// Initial heap Creation
For (I = n/2; I> 0; -- I)
{
AdjustHeap (data, I, n );
}
// Swap the top element of the heap with the first element in the ordered area in each loop, and then adjust the heap
For (I = n; I> 1; -- I)
{
Swap (1, I );
AdjustHeap (data, 1, I-1 );
}
}
 
Int main ()
{
Freopen ("random", "r", stdin );
Freopen ("oder", "w", stdout );
Int I;
For (I = 1; I <= MAX; ++ I)
{
Scanf ("% d", & data [I]);
}
// Stderr ("start sorting \ n ");
HeapSort (data, MAX );
// Stderr ("sorting ends \ n ");
For (I = 1; I <= MAX; ++ I)
{
Printf ("% d \ n", data [I]);
}
Return 0;
}

/****************
* Heap Sorting Algorithm
* The subscript of the sort array starts from 1.
*/
# Include <stdio. h>

Enum {MAX = 1000 + 1 ,};
 
Int data [MAX];
Static inline swap (int x, int y)
{/*
X ^ = y;
Y ^ = x;
X ^ = y;
*/
Int tmp;
Tmp = data [x];
Data [x] = data [y];
Data [y] = tmp;
}
// Heap Adjustment
// Data [], array to be sorted
// Target, the position of the element to be adjusted
// N, array size
Void AdjustHeap (int data [], int target, int n)
{
Int nChild;
Int nTemp;
 
NTemp = data [target]; // temporary storage
While (target * 2 <= n)
{
NChild = target * 2;
If (nChild + 1 <= n & data [nChild] <data [nChild + 1])
{
NChild ++; // nChild points to a child with a large keyword
}
If (nTemp <data [nChild])
{
Data [target] = data [nChild];
Target = nChild; // reprocess the byte points of the adjusted Node
}
Else break;
}
Data [target] = nTemp; // put the final elements to be adjusted in the appropriate position
}

// Heap Sorting Algorithm
// Data, array to be sorted
// N, array size
Void HeapSort (int data [], int n)
{
Int I;
// Initial heap Creation
For (I = n/2; I> 0; -- I)
{
AdjustHeap (data, I, n );
}
// Swap the top element of the heap with the first element in the ordered area in each loop, and then adjust the heap
For (I = n; I> 1; -- I)
{
Swap (1, I );
AdjustHeap (data, 1, I-1 );
}
}

Int main ()
{
Freopen ("random", "r", stdin );
Freopen ("oder", "w", stdout );
Int I;
For (I = 1; I <= MAX; ++ I)
{
Scanf ("% d", & data [I]);
}
// Stderr ("start sorting \ n ");
HeapSort (data, MAX );
// Stderr ("sorting ends \ n ");
For (I = 1; I <= MAX; ++ I)
{
Printf ("% d \ n", data [I]);
}
Return 0;
}

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.