如何在C ++中打印排序函数的迭代?

时间:2015-09-25 21:18:09

标签: c++ sorting

我正在尝试打印函数的迭代,因为它经过循环,代码之后的文本是输出应该是什么样的,这是我第一次问我不知道我做得对。<登记/>     // Abdulfattah Abutaha的代码请仅供参考。
    #包括     #包括     #包括     #include

using namespace std;

void SelectionSort(vector<int>& A)

{
    int temp,pos_min;
    int n=A.size();
    for (int i=0; i < n-1; i++)
    {
        pos_min = i;
        for (int j=i+1; j < n; j++)
        {
            if (A[j] < A[pos_min])
            {
                pos_min=j;
            }
        }
        if (pos_min != i)
        {
            temp = A[i];
            A[i] = A[pos_min];
            A[pos_min] = temp;
        }
    }
}

int main(){
    num="";
    nums.clear();
    cout<<endl;
    cout<<"Enter the next element (Enter 'q'to stop):"<<endl;

    while(num!="q")
    {
        cin>>num;
        if(num == "q")
        {
            continue;
        }
        cout<<"Enter the next element (Enter 'q'to stop):"<<endl;
        numbers=stoi(num);
        nums.push_back(numbers);
    }
    SelectionSort(nums);
    cout<<"Sequence: ";
    for(int i=0;i<nums.size();i++)
    {
        cout<<nums[i]<<" ";
    }

    return 0;
}

输出:

===Selection Sort====================================
Min -78,swap with 90:-78 -8 34 90 34 235 9 -12 653 
Min -12,swap with -8:-78 -12 34 90 34 235 9 -8 653 
Min -8,swap with 34:-78 -12 -8 90 34 235 9 34 653 
Min 9,swap with 90:-78 -12 -8 9 34 235 90 34 653 
Min 34,swap with 34:-78 -12 -8 9 34 235 90 34 653 
Min 34,swap with 235:-78 -12 -8 9 34 34 90 235 653 
Min 90,swap with 90:-78 -12 -8 9 34 34 90 235 653 
Min 235,swap with 235:-78 -12 -8 9 34 34 90 235 653 
Sequence: -78 -12 -8 9 34 34 90 235 653

2 个答案:

答案 0 :(得分:0)

这样做(添加我标记的三行。)请注意,我还没试过。

void SelectionSort(vector<int>& A)
{
    cout << "===Selection Sort====================================" << endl; // <--
    int temp,pos_min;
    int n=A.size();
    for (int i=0; i < n-1; i++)
    {
        pos_min = i;
        for (int j=i+1; j < n; j++)
        {
            if (A[j] < A[pos_min])
            {
                pos_min=j;
            }
        }
        cout << "Min " << A[pos_min] << ", swap with " << A[i] << ":"; // <--
        if (pos_min != i)
        {
            temp = A[i];
            A[i] = A[pos_min];
            A[pos_min] = temp;
        }
        for (int k = 0; k < n; ++k) cout << " " << A[k]; cout << endl; // <--
    }
}

答案 1 :(得分:-1)

我对原始代码做了很多编辑。我试着用评论来注释所有这些。代码按要求工作。

更新的代码:

#include <iostream>
#include <vector> 
#include <sstream>
#include <string>

using namespace std;

void selectionSort(vector<int>& A) // Changed the function name to start with lower case here and in main
{
    int temp,pos_min;
    int n=A.size();
    cout << "===Selection Sort====================================" << endl; // Added print statements
    for (int i=0; i < n-1; i++)
    {
        pos_min = i;
        for (int j=i+1; j < n; j++)
        {
            if (A[j] < A[pos_min])
            {
                pos_min=j;
            }
        }
        cout << "Min " << A[pos_min] << ", swap with " << A[i] << ":"; // Added print statements
        if (pos_min != i)
        {
            temp = A[i];
            A[i] = A[pos_min];
            A[pos_min] = temp;
        }
        for (int k = 0; k < n; ++k) // Changed this from k <= n to k < n
        {
            cout << " " << A[k]; // Added print statements
        }
        cout << endl; // Added print statements
    }
}

int main()
{
    const int SENTINAL = 9999; // Used a numerical sentinal to exit user entry loop
    int num(0); // Declared data type of num and initialized
    vector<int> nums; // Declared nums as a vector of ints

    nums.clear(); // Redundant as nums was initialized as an empty vector but doesn't really hurt

    cout<<"Enter the next element (Enter '9999' to stop):"<<endl; // Changed the sentinal stop value
    cin>>num;

    while(num!=SENTINAL) // Used sentinal value here
    {
        nums.push_back(num);
        cout<<"Enter the next element (Enter '9999' to stop):"<<endl;
        cin>>num;
    }

    selectionSort(nums); // Calls selectionSort function on nums vector

    cout<<"Sequence: "; // Prints the sorted sequence at the end
    for(int i=0;i<nums.size();i++)
    {
        cout<<nums[i]<<" ";
    }

    return 0;
}

此代码的输出:

===Selection Sort====================================
Min -78, swap with 34: -78 9 34 90 -8 235 -12 653 34
Min -12, swap with 9: -78 -12 34 90 -8 235 9 653 34
Min -8, swap with 34: -78 -12 -8 90 34 235 9 653 34
Min 9, swap with 90: -78 -12 -8 9 34 235 90 653 34
Min 34, swap with 34: -78 -12 -8 9 34 235 90 653 34
Min 34, swap with 235: -78 -12 -8 9 34 34 90 653 235
Min 90, swap with 90: -78 -12 -8 9 34 34 90 653 235
Min 235, swap with 653: -78 -12 -8 9 34 34 90 235 653
Sequence: -78 -12 -8 9 34 34 90 235 653