免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6447 | 回复: 3
打印 上一主题 下一主题

[算法] SGI STL中的list::sort是快速排序算法吧? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-08-12 11:15 |只看该作者 |倒序浏览
re
只是把快速排序的递归算法改成非递归的了。是吧?

[ 本帖最后由 dzbjet 于 2006-8-12 20:03 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2006-08-12 22:39 |只看该作者
看了一下代码,好巧妙~
确实是快速排序算法,而且是非递归实现,主要思想是.......懒得写了
我仿照写了一个验证思路的:

  1. #include <iostream>
  2. #include <math.h>

  3. using namespace std;

  4. void qsort(int array[], size_t size)
  5. {
  6.         // 3^0, 3^1, 3^2, 3^3, 3^4, 3^5, 3^6, 3^7, 3^8, 3^9; for convenience, counter[0] not used.
  7.         list<int> counter[10];
  8.         int index = 0;
  9.         while( index < size )
  10.         {
  11.                 int v = array[index];

  12.                 int i = 1;
  13.                 list<int> temp; temp.push_back(v);
  14.                 counter[i].merge(temp);


  15.                 if( counter[i].size() % 3 == 0 && counter[i].size() / 3 == i )
  16.                 {
  17.                         // need to up to the next level
  18.                         int j = i + 1;
  19.                         do {
  20.                                 counter[j].merge(counter[j-1]);
  21.                         } while ( counter[j].size() > pow(3,j++) );
  22.                 }
  23.                 ++index;
  24.         }

  25.         for( int k = 1; k < 10; ++k )
  26.         {
  27.                 counter[k].merge(counter[k-1]);
  28.         }

  29.         copy(counter[k-1].begin(), counter[k-1].end(), ostream_iterator<int>(cout, " "));
  30. }


  31. int main(int argc, char* argv[])
  32. {
  33.         int array[] = { 1, 5, 8, 3, 2, 9, 4, 41, 32, 0, 7, 6, 37};

  34.         qsort(array, sizeof(array)/sizeof(array[0]));
  35.        
  36.     return 0;
  37. }
复制代码

[ 本帖最后由 dzbjet 于 2006-8-13 13:53 编辑 ]

论坛徽章:
0
3 [报告]
发表于 2006-08-13 23:41 |只看该作者
template< typename T, typename Allocator >
void list<T, Allocator>::sort()
{
    if( empty() || m_header->next->next == m_header )
        return;

    self result[ 64 ];
    self carry;
    size_type fill = 0;

    while( !empty() )
    {
        carry.splice( carry.begin(), *this, begin() );
        size_type i = 0;
        while( (i < fill) && (!result[i].empty()) )
        {
            result[i].merge( carry );
            carry.swap( result[i] );
            ++i;
        }
        carry.swap( result[i] );
        if( i == fill )
            ++fill;
    }

    for( size_type j = 1; j < fill; ++j )
        result[j].merge( result[j - 1] );

    swap( result[fill - 1] );
}

论坛徽章:
0
4 [报告]
发表于 2006-08-14 09:46 |只看该作者
请不要贴STL的代码,这个网上可以找到,你贴在这里,有什么意义吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP