heap算法

// MyOwnHeap
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std ;

// 维护堆,其中某个元素parent的值发生改变
template< typename RandomAccessIterator >
void my_adjust_heap( RandomAccessIterator first, int parent, int length )
{
     typedef typename iterator_traits<RandomAccessIterator>::value_type value_type ;
     value_type tmp ;
     int left, right, max ;
     left = (parent << 1) + 1 ;
     right = (parent << 1) + 2 ;
     max = parent ;
     if ( left < length && *(first+left) > *(first+max) )
        max = left ;
     if ( right < length && *(first+right) > *(first+max) )
        max = right ;
     if ( max != parent )
     {
          tmp = *(first+parent) ;
          *(first+parent) = *(first+max) ;
          *(first+max) = tmp ;
          my_adjust_heap( first, max, length ) ;
      }
}

// 建立一个堆
template< typename RandomAccessIterator >
void my_make_heap( RandomAccessIterator first, RandomAccessIterator last )
{
     int length = last - first ;
     int parent = ( length - 2 ) / 2  ;
     for ( int i = parent ; i >= 0 ; -- i )
         my_adjust_heap( first, i, length ) ;
}

// 插入一个元素,元素已在最末尾
template< typename RandomAccessIterator >
void my_push_heap( RandomAccessIterator first, RandomAccessIterator last )
{
     typedef typename iterator_traits<RandomAccessIterator>::value_type value_type ;
     my_push_heap_aux( first, last - first, ( last - first ) - 1, value_type(0) ) ;
}
template< typename RandomAccessIterator, typename T >
void my_push_heap_aux( RandomAccessIterator first, int length, int newer, T )
{
     T old = *(first+newer) ;
     int parent = ( newer - 1 ) / 2 ;
     while ( newer > 0 &&  old > *(first+parent) )
     {
           *(first+newer) =  *(first+parent) ;
           newer = parent ;
           parent = ( newer - 1 ) / 2 ;
     }
     *(first+newer) = old ;
}

// 取出最大值,放到末尾,容器大小未变
template< typename RandomAccessIterator >
void my_pop_heap( RandomAccessIterator first, RandomAccessIterator last )
{    
     typedef typename iterator_traits<RandomAccessIterator>::value_type value_type ;
     my_pop_heap_aux( first, last - first, value_type(0) ) ;
}
template< typename RandomAccessIterator, typename T >
void my_pop_heap_aux( RandomAccessIterator first, int length, T )
{
     T tmp = *(first+length-1) ;
     *(first+length-1) = *first ;   
     *first = tmp ;                      //  交换
     // first ~ (first+length-1) 有一个值改变了
     my_adjust_heap( first, 0, length-1 ) ;
 }
 
template< typename RandomAccessIterator >
void my_sort_heap( RandomAccessIterator first, RandomAccessIterator last )
{
     my_make_heap(first, last) ;
     while ( last-first > 1 )
     {
           my_pop_heap( first, last-- ) ;
     }
}

int main()
{
    // test sort, my_make_heap, my_pop_heap, my_adjust_heap
    /*
    int n, j, x ;
    vector<int> vec ;
    for ( int i = 0 ; i < 3 ; ++ i )
    {
        cin >> n ;
        for ( j = 0 ; j < n ; ++ j )
        {
            cin >> x ;
            vec.push_back(x) ;
        }
        my_sort_heap( vec.begin(), vec.end() ) ;
        for ( int i = 0 ; i < vec.size() ; ++ i )
      cout << vec[i] << ' ' ;
        cout << endl ;
        vec.clear() ;
    }
    */
    // test my_push_heap
    /*
    int arr[] = { 0, 1, 2, 5, 10 } ;
    vector<int> tmp( arr, arr+5) ;
    my_make_heap( tmp.begin(), tmp.end() ) ;
    for ( int i = 0 ; i < tmp.size() ; ++ i )
        cout << tmp[i] << ' ' ;
    cout << endl ;
   
    tmp.push_back( 99 ) ;
    my_push_heap( tmp.begin(), tmp.end() ) ;
    for ( int i = 0 ; i < tmp.size() ; ++ i )
        cout << tmp[i] << ' ' ;
    cout << endl ;
   
    tmp.push_back( 30 ) ;
    my_push_heap( tmp.begin(), tmp.end() ) ;
    for ( int i = 0 ; i < tmp.size() ; ++ i )
        cout << tmp[i] << ' ' ;
    cout << endl ;
    */
   
    // test array
    int arr[100] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 } ;
    my_sort_heap( arr, arr+10 ) ;
    for ( int i = 0 ; i < 10 ; ++ i )
        cout << arr[i] << ' ' ;
    cout << endl ;
   
    arr[10] = 90 ;
    my_make_heap( arr, arr+11 ) ;
    for ( int i = 0 ; i < 11 ; ++ i )
        cout << arr[i] << ' ' ;
    cout << endl ;
   
    arr[11] = 8 ;
    my_make_heap( arr, arr+12 ) ;
    for ( int i = 0 ; i < 12 ; ++ i )
        cout << arr[i] << ' ' ;
    cout << endl ;
 
    system( "pause" ) ;
    return 0 ;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值