/************************************************************************/
/* Select Sort */
/************************************************************************/
#include <iostream>
#include <time.h>
#include "Timer.h"
using namespace std;
#define Swap(x, y) {int temp = x; x = y; y = temp;}
const int MAX = 100000;
void Input(int* numbers)
{
srand((unsigned)time(NULL));
for (int i = 0; i < MAX; i++)
{
numbers[i] = rand() % (MAX * 10);
}
}
void Output(int* numbers)
{
for (int i = 1; i <= MAX; i++)
{
cout << numbers[i-1] << " ";
if (0 == i % 10)
{
cout << endl;
}
}
cout << endl;
}
void SelectSort(int* numbers)
{
for (int i = 0; i < MAX-1; i++)
{
int k = i;
for (int j = i+1; j < MAX; j++)
{
if (numbers[k] > numbers[j])
{
k = j;
}
}
if (i < k)
{
Swap(numbers[i], numbers[k]);
}
}
}
void main()
{
//int num[MAX];
int* num = new int[MAX];
Input(num);
cout << "Bubble before: " << endl;
//Output(num);
Timer timer;
SelectSort(num);
cout <<"\nTime Elapsed: " << timer.GetElapsedTime() << "s" << endl;
cout << "\nBubble after: " << endl;
//Output(num);
delete[] num;
}
测试数据:10万个int数据。
运行结果如下: