使用列表 C++ 实现选择排序
Implementing selection sort using a list C++
我想实现选择排序来对航班的起飞时间进行排序,但它没有向我打印结果(而且我不确定它是否正确)。抱歉这个冗长而愚蠢的问题,我是编程新手。这是我现在制作的代码:
// 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;
}
}
此声明
SelectionSort();
创建类型为 SelectionSort
的临时对象,仅此而已。
您实际上没有读取文件中的任何内容,您没有要排序的向量,您没有调用排序函数。
我想实现选择排序来对航班的起飞时间进行排序,但它没有向我打印结果(而且我不确定它是否正确)。抱歉这个冗长而愚蠢的问题,我是编程新手。这是我现在制作的代码:
// 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;
}
}
此声明
SelectionSort();
创建类型为 SelectionSort
的临时对象,仅此而已。
您实际上没有读取文件中的任何内容,您没有要排序的向量,您没有调用排序函数。