合并排序源代码

xlfddlfd 2007-04-23 03:47:26
这是偷来的合并排序代码,2000W个整数大概10秒左右,我想问下问什么我把数组增加到1E排序完了会死机啊???
难道是偷来的东西就用不成???!!!
const DWORD size = 20000000;

int arry [ size ] ;

int _tmain(int argc, _TCHAR* argv[])
{
srand( GetTickCount( ) ) ;

for( DWORD i = 0 ; i<size ; i++)
arry[ i ] = rand( ) ;

DWORD lasttick = GetTickCount( ) ;
MergeSort( arry ,size ) ;
lasttick = GetTickCount( ) - lasttick ;

cout<<"Merge Sort times : "<<lasttick <<endl;
return 0;
}
template< class type >
void MergeSort( type * arry , int size ) ;

template< class type >
void Merge( type * p1 , type * p2 ) ;

template< class type >
void Merge( type * p1 , type * p2 , int size ) ;


template< class type >
void Merge( type * p1 , type * p2 )
{
#if _DEBUG
assert( p1 && p2 && p1 != p2 ) ;
#endif
int size = p2 - p1 ;
type * p1temp = p1 ;
type * p2temp = p2 ;
type * arry = new type[ size * 2 ] ;
type * parry = arry ;
#if _DEBUG
assert( arry ) ;
#else
if( !arry )
return ;
#endif
do
{
if( *p1temp <= *p2temp )
*parry ++ = *p1temp ++ ;
else
*parry ++ = *p2temp ++ ;
if( p1temp == p2 )
{
while( p2temp != p2 + size )
*parry ++ = *p2temp ++ ;
break ;
}
else if( p2temp == p2 + size )
{
while( p1temp != p2 )
*parry ++ = *p1temp ++ ;
break ;
}
}while( true ) ;
parry = arry ;
p1temp = p1 ;
do
{
*p1temp ++ = *parry ++ ;
}while( p1temp < p2 + size ) ;
delete [ ] arry ;
}

template< class type >
void Merge( type * p1 , type * p2 , int size )
{
#if _DEBUG
assert( p1 && p2 ) ;
#else
if( !p1 || !p2 )
return ;
#endif
int count = p2 - p1 ;
type * arry = new type[ count + size ] ;
#if _DEBUG
assert( arry ) ;
#else
if( !arry )
return ;
#endif
type * parry = arry ;
type * p1temp = p1 ;
type * p2temp = p2 ;
do
{
if( *p1temp <= *p2temp )
*parry ++ = *p1temp ++ ;
else
*parry ++ = *p2temp ++ ;
if( p1temp == p2 )
{
while( p2temp != p2 + size )
*parry ++ = *p2temp ++ ;
break ;
}
else if( p2temp == p2 + size )
{
while( p1temp != p2 )
*parry ++ = *p1temp ++ ;
break ;
}
}while( true ) ;
parry = arry ;
p1temp = p1 ;
do
{
*p1temp ++ = *parry ++ ;
}while( p1temp < p2 + size ) ;
delete [ ] arry ;
}



template< class type >
void MergeSort( type * arry , int size )
{
if( !arry || size <= 1 )
return ;

type * pend = arry + size ;
int i ;
for( i = 2 ; i <= size ; i *= 2 )
{
type * pbeg = arry ;
do
{
Merge( pbeg , pbeg + i / 2 ) ;
pbeg += i ;
}while( pbeg + i <= pend ) ;
if( pbeg == pend )
continue ;
else if( pbeg + i / 2 >= pend )
continue ;
else
Merge( pbeg , pbeg + i / 2 , pend - pbeg - i / 2 ) ;
}
if( i != size )
Merge( arry , arry + i / 2 , size - i / 2 ) ;
}
...全文
766 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
xlfddlfd 2007-04-23
  • 打赏
  • 举报
回复
msdn或者bcb的help文件,难道你都没有?
另外,用STL的话,最好手边放本<STL源码剖析>
--------------------------
谢谢了,在MSDN找到了。MERGE



死机?机器死掉么?还是程序挂掉
--------------------
蓝屏死机^-^
sirguan 2007-04-23
  • 打赏
  • 举报
回复
死机?机器死掉么?还是程序挂掉
taodm 2007-04-23
  • 打赏
  • 举报
回复
msdn或者bcb的help文件,难道你都没有?
另外,用STL的话,最好手边放本<STL源码剖析>
xlfddlfd 2007-04-23
  • 打赏
  • 举报
回复
error C3861: “mergesort”: 找不到标识符

哪里有库函数手册下载啊?知道库里有好多函数可以用,不知道名字还是白搭。。。
taodm 2007-04-23
  • 打赏
  • 举报
回复
mergesort呀
xlfddlfd 2007-04-23
  • 打赏
  • 举报
回复
请问楼上,stl用哪个函数啊???
taodm 2007-04-23
  • 打赏
  • 举报
回复
C++要偷代码干嘛,合并排序在STL库里是现成的。

65,213

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧