#include<iostream>
#include<time.h>
#include<iomanip>
#include<stdlib.h>
using namespace std;
#define size 10
#define start 0
void Swap(int &a,int &b)
{
int buf;
buf = a;
a = b;
b = buf;
}
void SelectionSort(int *array)
{
int min;
for(int i = start;i<size;i++)
{
min = i;
for(int j = i+1;j<size;j++)
{
if( array[j] < array[min] )
min = j;
}
Swap(array[i],array[min]);
}
}
int main()
{
srand(time(0));
int *array = new int[size];
int time1,time2;
cout<<"Oringinal List: ";
for(int i = start;i<size;i++)
{
array[i] = rand()%20+1;
cout<<setiosflags(ios::right)<<setw(5)<<array[i];
}
cout<<endl<<endl;
time1 = time(0);
SelectionSort(array);
time2 = time(0);
cout<<"After List: ";
for(int j = start;j<size;j++)
{
cout<<setiosflags(ios::right)<<setw(5)<<array[j];
}
cout<<endl<<endl;
cout<<"Time: "<<time2-time1<<" Seconds"<<endl<<endl;
return 0;
}
執行結果:
留言列表