注意在排完序之后,对我们认定的 下标i 和作为排序用的 下标minOrmax 进行判断是否相同
void workerManager::Sort_Emp()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或记录为空!" << endl;
system("pause");
system("cls");
}
else
{
cout << "请选择排序方式: " << endl;
cout << "1、按职工号进行升序" << endl;
cout << "2、按职工号进行降序" << endl;
int selete = 0;
cin >> selete;
//选择排序算法
for (int i = 0; i < this->m_EmpNum; i ++)
{
int minOrmax = i; //声明一个最小值 或者 最大值的下标
//二次循环,第i + 1个元素,i后面的元素
for (int j = i + 1; j < this->m_EmpNum; j++)
{
if (selete == 1)
{
//人为假定minOrmax下标是大的那个,但是我们需要minOrmax是小的那个所以:
if (this->m_EmpArray[minOrmax]->m_Id > this->m_EmpArray[j]->m_Id)
{
minOrmax = j;
}
else
{
//jiang降序:最大下标更新
if (this->m_EmpArray[minOrmax]->m_Id < this->m_EmpArray[j]->m_Id)
{
minOrmax = j;
}
}
}
//判断一开始认定的 最小值或最大值(i) 是不是 计算的最小值或最大值(minOrmax);
//如果不是,说明上述的--minOrmax--和-j-已经做了升序或降序的交换;
//这里就需要交换更新数据
if (i != minOrmax)
{
//我们数组中的元素都是父类指针
Worker * temp = this->m_EmpArray[i];
this->m_EmpArray[i] = this->m_EmpArray[minOrmax];
this->m_EmpArray[minOrmax] = temp;
}
}
}
cout << "排序成功,排序后结果为:" << endl;
this->save();
this->Show_Emp();
}
}