数据结构之选择排序

思路

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

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

过程

代码

#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;
}
全部评论

相关推荐

邦邦我真的好喜欢你啊:我恭喜你😋😍我🦈了你😤😡我恭喜你😋😍我🦈了你😤😡我恭喜你😋😍我🦈了你😤😡我恭喜你😋😍我🦈了你😤😡我恭喜你😋😍我🦈了你😤😡
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务