选择排序如何工作?

时间:2014-10-11 23:47:05

标签: c++ sorting

这是我尝试的一种选择,我不明白为什么它不起作用。我的理解是选择排序扫描向量的最小值,当它找到它时将它移动到向量的开头。这次预先形成另一次扫描,忽略第一个元素并重新执行,直到n - 1次,其中n是向量的长度。

#include <iostream>
#include <string>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <map>
using namespace std;





int main()
{
    vector <string> vName, vID, vClass;
    string sName, minValue, sID, sClass, sSearch, sQuestion, ssSearch, sSearchN, sSearchI;
    int iSize, iStudent;
    // Display initial vector size
    iSize = vName.size();

    cout << "Student list starts with the size:" << iSize << endl;

    // Get size of list from user

    cout << "How many students would you like to add?" << endl;
    cin >> iStudent;
    cin.ignore();
    // Get names, ids, and classes

    for (int i = 0; i < iStudent; i++)
    {
        cout << "Student" << i + 1 << ":\n";
        cout << "Please enter the student name: ";
        getline(cin, sName);
        vName.push_back(sName);

        cout << "Enter ID number ";
        getline(cin, sID);
        vID.push_back(sID);

        cout << "Enter class name ";
        getline(cin, sClass);
        vClass.push_back(sClass);

    }
    // Display header

    cout << "The list of students has the size of: " << iStudent << endl;
    cout << "The Student List" << endl;
    cout << "\n";
    cout << "Name:" << setw(30) << "ID:" << setw(38) << "Enrolled Class : " << endl;
    cout << "--------------------------------------------------------------------------";
    cout << "\n";

    // for loop for displying list
    for (int x = 0; x < vName.size() && vID.size() && vClass.size(); x++)
    {

        cout << vName[x] << "\t \t \t" << vID[x] << "\t \t \t" << vClass[x] << endl;
    }

    // Sorting function and for loop to display sorted names
    cout << "\n";
    cout << "The Student List after Sorting:" << endl;
    cout << "\n";


    //*************************************
    int startScan, minIndex;


    for (startScan = 0; startScan < vName.size() - 1; startScan++)
    {
        minIndex = startScan;
        minValue = vName[startScan];
        for (int index = startScan + 1; index < vName.size(); index++)
        {
            if (vName[index] < minValue)
            {
                minValue = vName[index];
                minIndex = index;
            }
        }
        vName[minIndex] = vName[startScan];
        vName[startScan] = minValue;
        vID[minIndex] = vID[startScan];
        vID[startScan] = minValue;
        vClass[minIndex] = vClass[startScan];
        vClass[startScan] = minValue;

    }


    //******************


    //sort(vName.begin(), vName.end());

    //for (int y = 0; y < vName.size(); y++)
    //{
    //  cout << vName[y] << endl;
    //}

    cout << "\n";

    // Search function uses a do while loop that loops so long as the user inputs a "y" or "Y"


    do
    {
        int iPick;
        cout << "Search Menu:" << endl;
        cout << "1. By Name\n";
        cout << "2. By ID\n \n";
        cin >> iPick;
        if (iPick == 1)
        {
            cout << "Please Enter a name to be searched:" << endl;
            getline(cin >> ws, sSearchN);
            if (binary_search(vName.begin(), vName.end(), sSearchN))
            {



                cout << sSearchN << " was found." << endl << endl;
            }


            else
            {

                cout << sSearchN << " was not found." << endl << endl;


            }

            cout << "Would you like to search another name?" << endl << endl;
            cout << vName[0];
            cout << "Please enter Y for Yes and N for No:" << endl << endl;
            getline(cin >> ws, sQuestion);
        }
        else
        {
            cout << "Please Enter an ID to be searched:" << endl;
            getline(cin >> ws, sSearchI);
            if (binary_search(vID.begin(), vID.end(), sSearchI))
            {
                cout << sSearchI << " " << "was found"  << endl << endl;
                cout << "Please enter Y for Yes and N for No:" << endl << endl;
                getline(cin >> ws, sQuestion);
            }
            else
            {
                cout << sSearchI << " " << "was not found." << endl << endl;
                cout << "Please enter Y for Yes and N for No:" << endl << endl;
                getline(cin >> ws, sQuestion);
            }
        }

    } while (sQuestion == "Y" || sQuestion == "y");


    cout << "Thank you for using this program!" << endl;

    return 0;

}

编辑:发布整个计划

所以问题是它根本没有排序,只是将vName没有排序。

1 个答案:

答案 0 :(得分:1)

好的,在进一步研究代码后,我想我已经弄明白了。

因此,查看正在排序的代码部分,它的工作方式实际上非常简单。 startScan是一个int增量,直到它等于for循环中向量的大小。在这种情况下,它是vName。 minIndex将保存最小的索引号,该索引号在初始化时将等于starScan。最后minValue是一个字符串(在本例中是因为我们有一个字符串向量),它充当vName [scanStart]元素的临时容器。在第二个for循环中,索引将通过向量递增,并将测试元素vName [index]是否小于我之前谈到的temproray容器。如果它是新的temp将是vName [index]之后,它将退出内部循环并更新并继续到vName中的下一个最小值。理解这一点很容易做到这一点,以便排序与其他2个向量发生。所有人必须做的就是为那些矢量创建容器,就像我在这里的minValueAminValueB一样。随着vName的改变,它们将随之改变。这样一切都按照vName的顺序排列。我希望能帮助别人!

int startScan, minIndex;
string minValue, minValueA, minValueB;


    for (startScan = 0; startScan < vName.size() - 1; startScan++)
    {
        minIndex = startScan;
        minValue = vName[startScan];
        minValueA = vID[startScan];
        minValueB = vClass[startScan];

        for (int index = startScan + 1; index < vName.size(); index++)
        {
            if (vName[index] < minValue)
            {
                minValue = vName[index];
                minValueA = vID[index];
                minValueB = vClass[index];
                minIndex = index;

            }
        }
        vName[minIndex] = vName[startScan];
        vName[startScan] = minValue; //values for vName are being added to the other ones.
        vID[minIndex] = vID[startScan];
        vID[startScan] = minValueA;
        vClass[minIndex] = vClass[startScan];
        vClass[startScan] = minValueB;

    }

    //******************


    sort(vName.begin(), vName.end());

    for (int y = 0; y < vName.size(); y++)
    {
        cout << vName[y] << "\t \t \t " << vID[y] << "\t \t \t " << vClass[y] << endl;
    }