自己用C++把算法书上的伪算法改写了一下,供大家参考
#include <iostream>
bool merge( int a[], int start1, int start2, int m_size)
{
int m_temp = m_size -start1;
int *temp_start = new int[m_temp];
int * m_ptemp = temp_start;
int m_tempstart1 = start2;
int tempstart = start1;
while( start1<m_tempstart1&& start2< m_size )
{
*temp_start++ = (a[start1]>a[start2])?a[start2++]:a[start1++] ;
}
while( start1< m_tempstart1)
{
*temp_start++ = a[start1++];
}
while( start2< (m_size) )
{
*temp_start++ =a[start2++];
}
将合并后的有序数组,复制到
int i = 0;
while( i<m_temp )
{
a[tempstart] = m_ptemp[i];
i++;
tempstart++;
}
delete m_ptemp;
return true;
}
bool bottomsort( int a[], int m_size)
{
int length = 1; //size表示合并的数组的大小
while( length<m_size ) //length表示合并后数组的长度
{
int size = length;
length = 2 * size;
int i = 0;
while(i+length <= m_size)
{
merge( a, i, i+size, i+length);
i = i+length;
}
if( i+size< m_size)
merge(a, i, i+size, m_size );
}
return true;
}
int main()
{
int m_array[] ={ 14,2,58,24,95,17,979879,45,477,255,15,2587,4877,987};
int m_size = (sizeof( m_array))/sizeof(m_array[0]);
bottomsort(m_array, m_size);
for( int i =0 ;i<m_size; i++)
{
std::cout<<m_array[i]<<" " ;
}
std::cout<<std::endl;
return 0;
}