首页 > 编程语言 > 详细

Algorithm-选择排序

时间:2015-03-22 18:16:00      阅读:289      评论:0      收藏:0      [点我收藏+]
// ConsoleApplication21.cpp : 定义控制台应用程序的入口点。
// VS2013

#include "stdafx.h"
#include <iostream>
#include <algorithm>
void selectSort(int a[], const int n)
{
	int k, temp;
	for (int i = 0; i < n - 1; i++)
	{
		k = i;				//从arr[i]检查到arr[n-1],最小的数在a[k];
		for (int j = i; j < n; j++)
		{
			if (a[j] < a[k]) k = j;	//k指示当前找到的最小整数
		}
		if (i != k)			//交换a[i]与a[k]
		{
			temp = a[i];
			a[i] = a[k];
			a[k] = temp;
		}
	}
}

//测试selectSort
int _tmain(int argc, _TCHAR* argv[])
{
	int arr[]{3, 5, 8, 9, 10, 2, 30, 7};
	selectSort(arr, sizeof(arr)/sizeof(int));
	std::for_each(std::begin(arr), std::end(arr), [](int n)//c++11中的Lambdas(匿名函数)
	{
		std::cout << n << " ";
	});
	return 0;
}


Algorithm-选择排序

原文:http://my.oschina.net/hejunsen/blog/389991

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!