经典算法回顾简单选择排序【选择排序】
算法思想:每次在未排序的序列中找最大的数,将其放在未排序序列的最后位置,重复该过程,知道所有元素排序完毕。
算法代码:
1 #include "stdafx.h"
2 #include <iostream>
3 #include <algorithm>
4 using namespace std;
5
6 template <typename T>
7 void SelectSort(T data[], int size)
8 {
9 int temp,max;
10 for(int i = size-1; i > 0 ; --i)
11 {
12 max = i;
13 for(int j=0; j < i ; j++)
14 {
15 if(data[j] > data[max])
16 max = j;
17 }
18 if(max != i)
19 swap(data[i],data[max]);
20 }
21 }
22 int main(int argc, char *argv[])
23 {
24 int data[] = {12,4,1,6,3,9,10};
25 SelectSort(data,7);
26 for(int i=0;i<7;i++)
27 cout << data[i] << " ";
28 return 0;
29 }