算法系列之排序(二):选择排序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fx_odyssey/article/details/80109751

go on~

何为选择(不要吐槽这句话)?我也不懂,只知道怎么做(滑稽,非手动)。选择就是在数组中,挑出最大(小)的数,放在右(左)端,然后在剩下的数中,挑出第二大(小)的数放在上一个挑出数的左(右)端,依次类推。

废话结束,上代码(不要问我要Java、OC、Swift、Python、汇编或者PHP之类的语言,我不会!非傲娇脸):

#include <stdio.h>

int main()
{
	int a[10] = {2, 5, 9, 1, 4, 8, 6, 7, 3, 0};
	for (int i = 0; i < 10; i++)
	{
		printf("%d\t", a[i]);
	}
	printf("\n");
	for (int i = 0; i < 9; i++)
	{
		int t = a[i];
		int no = i;
		for (int j = i; j < 9; j++)
		{
			if (t > a[j+1])
			{
				t = a[j+1];
				no = j+1;
			}
		}
		a[no] = a[i];
		a[i] = t;
	}
	for (int i = 0; i < 10; i++)
	{
		printf("%d\t", a[i]);
	}
	printf("\n");
	getchar();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/fx_odyssey/article/details/80109751
今日推荐