Rationale: For a given array, the minimum number is obtained after the first round comparison, and the smallest number is exchanged with the first number, then the remaining number is looked up and swapped until only one number remains. The complexity of this algorithm is: O (n^2). (Reference programmer interview written book--He Hao)
The procedure is as follows:
#include <iostream>
using namespace std;
Select sort (simple sort)
void simple_sort (int *a,int n)
{
int temp=0;
int flag=0;
for (int i=0;i<n-1;i++)
{
temp=a[i];
flag=i;
for (int j=i+1;j<n;j++)
{
if (a[j]<temp)
{
temp=a[j];
Flag=j
}
}
if (Temp!=a[i])
{
a[flag]=a[i];
A[i]=temp
}
}} Output..
void Display (int *a,int n)
{for
(int i=0;i<n;i++)
cout<<a[i]<< " ";
cout<<endl;
}
int main ()
{
int a[]={1,3,5,8,9,2,4,6,9,7};
int length_a=sizeof (a)/sizeof (a[0]);
cout<< "sort before array:";
Display (a,length_a);
Simple_sort (a,length_a);
cout<< "Sort after array:";
Display (a,length_a);
System ("pause");
return 0;
}
The output results are: