排序算法之选择排序

来源:互联网 发布:群晖 路由器开放端口 编辑:程序博客网 时间:2024/06/04 18:13
<pre name="code" class="cpp">// SelectSort.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "iostream"using namespace std;//   说明:排序算法 - 每一次迭代中都选择最小的数void SelectSort(double a[], int n){for (int i=0; i<n; i++){int iMin = i; for (int j=i + 1; j<n; j++){if (a[j] < a[iMin]){iMin = j;}}if (iMin != i){double dTemp = a[i];a[i] = a[iMin];a[iMin] = dTemp;}}}int _tmain(int argc, _TCHAR* argv[]){double a[] = {2.0, 5.4, 4.0, 8.0, 3.0, 5.0, 1.0, 9.0, 7.0};int n = sizeof(a) / sizeof(*a);cout<<"排序前:\n";for each (double x in a){cout<<x<<" ";}cout<<endl;SelectSort(a, n);cout<<"排序后:\n";for each (double x in a){cout<<x<<" ";}cout<<endl;return 0;}


                                             
0 0
原创粉丝点击