简体   繁体   中英

Implementing selection sort using a list C++

I want to implement selection sort to sort the departure time of flights but it doesn't print me the result (And I am not sure if it will be correct). Sorry for the long and silly question, I am new in programming. Here is the code that I made by now:

// Sort class
class Sort
{
protected:
    // number of comparisons performed in sort function
    unsigned long num_cmps;

public:
    // main entry point
    virtual void sort(std::vector<Flight>& data) = 0;
    // returns false if not sorted true otherwise
    bool testIfSorted(const std::vector<Flight>& data);
    // returns number of comparisons
    unsigned long getNumCmps();
    // resets the number of comparisons
    void resetNumCmps();
};

// SelectionSort class
class SelectionSort : public Sort
{
public:
    // main entry point
    void sort(std::vector<Flight>& data);
};

// BubbleSort class
class BubbleSort : public Sort
{
public:
    // main entry point
    void sort(std::vector<Flight>& data);
};

#include "Sort.h"

using namespace std;


unsigned long Sort::getNumCmps()
{
    return num_cmps;
}


void Sort::resetNumCmps()
{
    num_cmps = 0;
}

void Menu::selection_sort(){
    system("cls");
    ifstream in("inputFileExample.txt");
    if (!in)
    {
        cerr << "ERROR: wrong input file name!";
        exit(-1);
    }
    SelectionSort();
}

void SelectionSort::sort(std::vector<Flight>& data){
    for (int i = 0; i < (num_cmps - 1); i++)
    {
    int smallest = i;
        for(int j = (i+1); j<num_cmps; j++)
        {
            if(data[j] < data[smallest])
            {
                smallest = j;
            }
        }
    num_cmps++;
    cout << num_cmps;
    }
}

This statement

SelectionSort();

creates a temporary object of type SelectionSort , and that's it.

You don't actually read anything from the file, you don't have a vector to sort, you don't call the sorting function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM