VC++ implementation of selection sorting algorithm simple example

  • 2020-04-02 02:34:46
  • OfStack

In this paper, a very simple example is given to illustrate the implementation method of VC++ selection sort algorithm, n-1 simple selection sort for n records, in the unordered area to select the minimum record.

The specific implementation code is as follows:


#include<iostream>
using namespace std;
//Simple selection sort
void SelectSort(int r[ ], int n)
{ 
int i;
int j;
int index;
int temp;
for (i=0; i<n-1; i++) // right n Individual recording n-1 Trip to Simple selection sort
{ 
index=i; 
for (j=i+1; j<n; j++) //Select the minimum record in the unordered area
if (r[j]<r[index])
index=j;
if (index!=i) 
{
temp=r[i];
r[i]=r[index];
r[index]=temp;
}
}
for(i=0;i<n;i++)
cout<<r[i]<<" ";
cout<<"n";
}

Related articles: