标签:blog os io for ar 2014 cti div
/********************************************************************
@file Main_practise.cpp
@date 2014-8-19
@author Tiger
@brief 範例:選擇排序法( Selection Sort )
@details 找到第一小的數字,放在第一個位置;再找到第二小的數字,
放在第二個位置。一次找一個數字,如此下去就會把所有數值
按照順序排好了。
********************************************************************/
#include <iostream>
const int SIZE = 5;
void SelectionSort(int arr[], int n);
void Swap(int&a, int&b);
int main(int argc, const char* argv[])
{
int arr[SIZE] = {3, 6, 9, -8, 1};
SelectionSort(arr, SIZE);
for (int i=0; i<SIZE; ++i)
{
std::cout << arr[i] << " ";
}
std::cout << std::endl;
system("pause");
return 0;
}
void SelectionSort(int arr[], int n)
{
for (int i=0; i<n; ++i)
{
int nMinIndex = i;
for (int j=i+1; j<n; ++j)
{
if (arr[j] < arr[nMinIndex])
{
nMinIndex = j;
}
}
Swap(arr[i], arr[nMinIndex]);
}
}
void Swap(int&a, int&b)
{
int nTemp = a;
a = b;
b = nTemp;
}
标签:blog os io for ar 2014 cti div
原文地址:http://www.cnblogs.com/roronoa-zoro-zrh/p/3921631.html