我正在尝试用新生成的数字列表替换数字列表。 Sort函数接受一个数字列表(我认为基数排序)。例如
std::list<int> list;
list contains 2,1;
当调用函数Sort时,将使用包含2和1的列表。甚至模块转到列表0和奇数列表1.之后我想覆盖我的列表以使用新列表。这个过程一直持续到除数(参考代码)大于我列表中的最大数字。一旦完成,我的列表将被排序。
Example list{2,1}
Sort(list)
divisor=1;
while (divisor <largest_item_in_list)
{
for(go through list and get the number)
n=(number/1)%2
if(n==0)
put into list0
else
put into list1
}
divisor=divisor*2;
put list together using splice function from std::List
2 gets put into list 0
1 gets put into list 1
since the divisor is still less than the largest int in the list the loop is ran again
when the loop is ran again the list produced is
2 goes into list 1
1 goes into list 0
put the list back to the original
List is now sorted list= 1,2
除数大于列表中的最大数后,我们退出循环 我生成的新列表现在是list = {1,2},现在已经排序了。
.
int getMax(std::list<int> list_Original)
{
int mx = list_Original.front();
for(std::list<int>::iterator i=list_Original.begin();i !=list_Original.end();i++)
{
if(*i>mx)
{
mx=*i;
}
}
return mx;
}
void Sort(std::list<int> list_Original)
{
int largest_number_in_list=getMax(list_Original);
int divisor=1;
std::list<int> list_0;//Hold even numbers
std::list<int> list_1;//Holds odd Numbers
std::list<int> new_original;//List of integer
new_original=list_Original;
while(divisor<=largest_number_in_list)
{
for(std::list<int>::iterator i=new_original.begin();i != new_original.end();i++)
{
int n=((*i/divisor)%2);
if(n==0)
{
list_0.push_back(*i);
}
else
{
list_1.push_back(*i);
// new_original.remove(*i);
}
// std::cout<<*i<<"="<<n<<"the divisor is"<<"="<<divisor<<std::endl;
}
divisor=2*divisor;
// new_original=test;
}
new_original.splice(new_original.begin(),list_1);
new_original.splice(new_original.begin(),list_0);
for(std::list<int>::iterator i=new_original.begin();i !=new_original.end();i++)
{
std::cout<<*i<<std::endl;
}
}
int main ()
{
int array[]={2,1};
// int n=sizeof(array)/sizeof(array[0]);
std::list<int> my_list(array,array+2);
//getMax(my_list);
Sort(my_list);
std::cout<<"hi";
}