数据结构之选择排序

思路

将待排序的数分为两部分,一部分是已排序,另一部分是未排序。

将未排序部分中最小的数放在已排序部分后。

过程

代码

#include<iostream>
using namespace std;
void selectSort(int a[], int len)
{
	int	smallestIndex;
	int i, j, temp;
	for(i = 0; i < len - 1; i++)
	{
		smallestIndex = i;
		for(j = i+1; j < len; j++)
		{
			if(a[j] < a[smallestIndex])
			{
				smallestIndex = j;
			}
		}
		if(smallestIndex != i)
		{
			temp = a[i];
			a[i] = a[smallestIndex];
			a[smallestIndex] = temp;
		}
	}
}
int main()
{
	int a[6]={5,2,4,6,1,3};
	int len = sizeof(a) / sizeof(a[0]);
	selectSort(a, len);
	for(int i = 0; i < len; i++)
	{
		cout<<a[i]<<' ';
	}
	return 0;
}
全部评论

相关推荐

2024-12-25 09:09
四川师范大学 运营
想和你交朋友的潜伏者要冲国企:先去沃尔玛亲身感受标准化流程体系,一两年后再跳槽国内任何零售行业,可以有更大选择权吧?
点赞 评论 收藏
分享
mq2:我倒是觉得这种敞亮一点好。能接受就去不能就不去呗。 完了跟现在“正常”公司一样,hr说的天花乱坠,进去一看根本就是996核动力牛马,想走又没应届生身份了。岂不是更糟。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务