一个待排序数组,每次只能将一个数放到数组最后面,请问多少次能将数组排序完成?
例如:
99 2 5 199
2次
2 5 199 99
2 5 99 199
int sortGG(vector<int> &s){
int count=0;
int maxs = s[0];
for (int i = 1; i < s.size(); i++)
{
if (maxs<s[i])
{
maxs = s[i];
}
else
{
int temp = maxs;
s.erase(s.begin()+i-1);
s.push_back(temp);
count++;
i=0;
maxs = s[0];
}
}
return count;
}

本文介绍了一种通过不断将当前最大值移动到数组末尾来完成排序的方法,并计算完成排序所需的步数。此算法适用于特定场景下的排序问题。
1451

被折叠的 条评论
为什么被折叠?



