选择排序

/*
* selectSort.cpp
* 选择排序,算法导论P.16 习题2.2-2
* Created on: 2011-12-15
* Author: LiChanghai
*/
#include <vector>
#include <iterator>
using namespace std;

template<class T>
void selectSort(vector<T> &array)
{
T temp;
typename vector<T>::iterator iter1, iter2;
//假设共有n个元素,遍历前 n-1个元素
for(iter1=array.begin(); iter1 != array.end()-1; ++iter1)
{
//内层循环将iter1指向的元素与后面的元素比较,找出最小的与其交换
for(iter2=iter1+1; iter2 != array.end(); ++iter2)
if(*iter2 < *iter1)
{
temp = *iter1;
*iter1 = *iter2;
*iter2 = temp;
}
}
}
posted @ 2011-12-22 21:22  oceany  阅读(179)  评论(0编辑  收藏  举报