选择排序------直接选择排序
#include <iostream> using namespace std; template <class T> void selectionSort(T A[], int n) { int smallIndex=0; for(int i=0;i<n-1;i++) { smallIndex=i; for(int j=i+1;j<n;j++) { if(A[j]<A[smallIndex]) smallIndex=j; } swap(A[i], A[smallIndex]); } }
int main() { int A[]={8, 6, 9, 7, 5, 0, 4, 1, 3, 2}; int n=10; selectionSort(A, n); for(int i=0;i<n;i++) cout<<A[i]<<" "; cout<<endl; return 0; }
直接选择排序, 从前到后, 依次选择出待排序数据的最小值并将其与待排序数据的首数据相交换。
本博客是博主个人学习时的一些记录,不保证是为原创,个别文章加入了转载的源地址,还有个别文章是汇总网上多份资料所成,在这之中也必有疏漏未加标注处,如有侵权请与博主联系。
如果未特殊标注则为原创,遵循 CC 4.0 BY-SA 版权协议。
posted on 2017-01-25 15:11 Angry_Panda 阅读(152) 评论(0) 编辑 收藏 举报