<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;
}
排序算法之选择排序
最新推荐文章于 2023-02-12 20:42:34 发布