// Selectsort.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<IOSTREAM>
#include<CSTDIO>
#include<CSTDLIB>
#include<CSTRING>
#include<CTIME>
using namespace std;
#define SIZE 10
void SelectSort( int *a, int len)
{
int i, j, h;
int temp;
for (i = 0; i < len - 1; i++)
{
for (j = i + 1; j < len; j++)
{
if (a[j] < a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
cout<<"sort "<<i<<" step result"<<endl;
for (h = 0; h < len; h++)
{
cout<<a[h]<<" ";
}
cout<<endl;
}
}
int main(int argc, char* argv[])
{
int array[SIZE], i;
srand(time(NULL));
for (i = 0;i < SIZE; i++)
{
array[i] = rand() / 1000 + 100;
}
cout<<"before sort -------------"<<endl;
for (i = 0; i < SIZE; i++)
{
cout<<array[i]<<" ";
}
cout<<endl;
SelectSort(array, SIZE);
cout<<"Sort: ------------------"<<endl;
for (i = 0; i < SIZE; i++)
{
cout<<array[i]<<" ";
}
cout<<endl;
getchar();
return 0;
}
before sort -------------
105 129 130 121 122 123 119 124 108 103
sort 0 step result
103 129 130 121 122 123 119 124 108 105
sort 1 step result
103 105 130 129 122 123 121 124 119 108
sort 2 step result
103 105 108 130 129 123 122 124 121 119
sort 3 step result
103 105 108 119 130 129 123 124 122 121
sort 4 step result
103 105 108 119 121 130 129 124 123 122
sort 5 step result
103 105 108 119 121 122 130 129 124 123
sort 6 step result
103 105 108 119 121 122 123 130 129 124
sort 7 step result
103 105 108 119 121 122 123 124 130 129
sort 8 step result
103 105 108 119 121 122 123 124 129 130
Sort: ------------------
103 105 108 119 121 122 123 124 129 130