#include<iostream>
#include<time.h>
using namespace std;
void sift( int [ ] ,int ,int ) ;
void BuildMaxHeap( int [ ] ,int ) ;
void Heap_sort( int [ ] ,int ) ;
void Heap_sort_old( int [ ] ,int ) ;
int main( void ) {
srand ( time ( NULL ) ) ;
int heap[ 13 ] ;
for ( int i= 0 ; i< 13 ; i++ )
heap[ i] = rand ( ) % 100 ;
cout << "Heap sort前:" ;
for ( int i= 0 ; i< 13 ; i++ )
cout << heap[ i] << " " ;
cout << endl;
Heap_sort( heap,13 ) ;
cout << "Heap sort後:" ;
for ( int i= 0 ; i< 13 ; i++ )
cout << heap[ i] << " " ;
cout << endl;
system ( "pause" ) ;
return 0 ;
}
void BuildMaxHeap( int heap[ ] ,int n) {
int i;
for ( i= n/ 2 - 1 ; i>= 0 ; i-- )
sift( heap,i,n) ;
}
void sift( int a[ ] ,int head,int n) {
int temp,j;
while ( head* 2 + 1 < n) {
j= 2 * head+ 1 ;
if ( head* 2 + 1 == n- 1 ) {
if ( a[ head] > a[ j] )
break ;
}
else {
if ( a[ j+ 1 ] > a[ j] )
j++ ;
if ( a[ head] > a[ j] )
break ;
}
temp= a[ head] ;
a[ head] = a[ j] ;
a[ j] = temp;
head= j;
}
}
void Heap_sort( int heap[ ] ,int n) {
int temp;
for ( int i= ( n- 2 ) / 2 ; i>= 0 ; i-- )
sift( heap,i,n) ;
for ( int i= n- 1 ; i>= 1 ; i-- ) {
temp= heap[ i] ;
heap[ i] = heap[ 0 ] ;
heap[ 0 ] = temp;
sift( heap,0 ,i) ;
}
}
void Heap_sort_old( int heap[ ] ,int n) {
int temp;
for ( int i= ( n- 2 ) / 2 ; i>= 0 ; i-- )
sift( heap,i,n) ;
while ( n> 1 ) {
temp= heap[ n- 1 ] ;
heap[ n- 1 ] = heap[ 0 ] ;
heap[ 0 ] = temp;
n-- ;
for ( int i= 0 ; i<= n- 1 ; i++ )
sift( heap,0 ,n) ;
}
}
I2luY2x1ZGU8aW9zdHJlYW0+CiNpbmNsdWRlPHRpbWUuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnZvaWQgc2lmdChpbnQgW10saW50LGludCk7CnZvaWQgQnVpbGRNYXhIZWFwKGludCBbXSxpbnQpOwp2b2lkIEhlYXBfc29ydChpbnQgW10saW50KTsKdm9pZCBIZWFwX3NvcnRfb2xkKGludCBbXSxpbnQpOwoKaW50IG1haW4odm9pZCl7CiAgICAKICAgIHNyYW5kKHRpbWUoTlVMTCkpOwogICAgCiAgICAKICAgIGludCBoZWFwWzEzXTsKICAgIAogICAgZm9yKGludCBpPTA7aTwxMztpKyspCiAgICAgICBoZWFwW2ldPXJhbmQoKSUxMDA7CiAgICAKICAgICAgY291dDw8IkhlYXAgc29ydOWJjToiOwogICAgICBmb3IoaW50IGk9MDtpPDEzO2krKykKICAgICAgY291dDw8aGVhcFtpXTw8IiAiOyAKICAgICAgY291dDw8ZW5kbDsKICAgIAogICAgSGVhcF9zb3J0KGhlYXAsMTMpOwogICAgCiAgICBjb3V0PDwiSGVhcCBzb3J05b6MOiI7CiAgICBmb3IoaW50IGk9MDtpPDEzO2krKykKICAgICBjb3V0PDxoZWFwW2ldPDwiICI7IAogICAgIGNvdXQ8PGVuZGw7CiAgICAKICAgIHN5c3RlbSgicGF1c2UiKTsgCiAgICAgcmV0dXJuIDA7ICAKfQoKCnZvaWQgQnVpbGRNYXhIZWFwKGludCBoZWFwW10saW50IG4pewogICAgICAgIGludCBpOwogICAgICAgIGZvcihpPW4vMi0xO2k+PTA7aS0tKQogICAgICAgICBzaWZ0KGhlYXAsaSxuKTsKICAgICAKICAgICB9CiAgICAgCiAgICAgCnZvaWQgc2lmdChpbnQgYVtdLGludCBoZWFkLGludCBuKXsKICAgICAKICAgICAKICAgICBpbnQgdGVtcCxqOwogICAgCiAgICAKICAgICAKICAgICAKICAgICB3aGlsZShoZWFkKjIrMTxuKXsKICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICBqPTIqaGVhZCsxOyAKICAgICAgICAgICAgCiAgICAgICAgICAgIGlmKGhlYWQqMisxPT1uLTEpewogICAgICAgICAgICAKICAgICAgICAgICAgIAogICAgICAgICAgICBpZihhW2hlYWRdPmFbal0pCiAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgIAogICAgICAgICAgICAgCiAgICAgICAgICAgIH0KICAgICAgICAgICAKICAgICAgICAgICAgZWxzZXsKICAgICAgICAgICAgICAgICBpZihhW2orMV0+YVtqXSkKICAgICAgICAgICAgICAgICAgICAgaisrOwogICAgICAgICAgICAgCiAgICAgICAgICAgIGlmKGFbaGVhZF0+YVtqXSkKICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgCiAgICAgICAgICAgIHRlbXA9YVtoZWFkXTsKICAgICAgICAgICAgYVtoZWFkXT1hW2pdOyAgICAKICAgICAgICAgICAgYVtqXT10ZW1wOwogICAgICAgICAgICBoZWFkPWo7CiAgICAgICAgICAgICAKICAgICAgICAgICAgCiAgICAgfQogICAgIAogICAgIAogICAgIH0KICAgICAKICAgICAKICAgICAKICAgICAKICAgICAKICAgICAKdm9pZCBIZWFwX3NvcnQoaW50IGhlYXBbXSxpbnQgbil7CiAgICAgaW50IHRlbXA7CiAgICAgICAgCiAgICAgZm9yKGludCBpPShuLTIpLzI7aT49MDtpLS0pCiAgICAgICAgIHNpZnQoaGVhcCxpLG4pOwogICAgCiAgICAKICAgICBmb3IoaW50IGk9bi0xO2k+PTE7aS0tKXsKICAgICAgCiAgICAgIHRlbXA9aGVhcFtpXTsKICAgICAgaGVhcFtpXT1oZWFwWzBdOwogICAgICBoZWFwWzBdPXRlbXA7CiAgICAgIAogICAgICBzaWZ0KGhlYXAsMCxpKTsKICAgIAogICAgICB9CiAgICAgfQogICAgIAogICAgIAogICAgIAogICAgIAp2b2lkIEhlYXBfc29ydF9vbGQoaW50IGhlYXBbXSxpbnQgbil7CiAgICAgaW50IHRlbXA7CiAgICAgCiAgICAgZm9yKGludCBpPShuLTIpLzI7aT49MDtpLS0pCiAgICAgICAgIHNpZnQoaGVhcCxpLG4pOwogICAgIAogICAgIAogICAgIHdoaWxlKG4+MSl7CiAgICAgdGVtcD1oZWFwW24tMV07CiAgICAgaGVhcFtuLTFdPWhlYXBbMF07CiAgICAgaGVhcFswXT10ZW1wOwogICAgIG4tLTsKICAgICAKICAgICAKICAgICBmb3IoaW50IGk9MDtpPD1uLTE7aSsrKQogICAgIHNpZnQoaGVhcCwwLG4pOwogICAgIAogICAgIAogICAgIH0KICAgICAKICAgICAKICAgICB9