Sort the odd

You have an array of numbers.
Your task is to sort ascending odd numbers but even numbers must be on their places.
Zero isn't an odd number and you don't need to move it. If you have an empty array, you need to return it.

std::vector<int> sortArray(std::vector<int> array)
{
    int currentMax=INT_MAX;
    int count=array.capacity();
    for(int i=0;i<count;i++)
    {
        if(array.at(i)%2==0)
        {
            continue;
        }else
        {
            currentMax=array.at(i);
        }
        for(int j=i+1;j<count;j++)
        {
            if(array.at(j)%2==0)
            {
                continue;
            }
            else
            {
                if(array.at(j)<currentMax)
                {
                    array[i]=array.at(j);
                    array[j]=currentMax;
                    currentMax=array[i];
                }
            }
        }
    }
    return array;
}

冒泡排序  vector 使用 capacity 赋值【】  获取 .at()

猜你喜欢

转载自blog.csdn.net/u014038245/article/details/80705775