职工管理系统_201026(思路详解版-第六步排序职工 )

注意在排完序之后,对我们认定的 下标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();
	}


}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

扳手的海角

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值