14. Toad's data structure Advanced 14 sort implementation simple selection sort
This famous saying:"That is, we are a candle should also be " The candle into a gray tear began to dry " even if we were just a match It's going to shine at a critical moment , even if we die, and the bones rot. Know that it will become wildfire to burn in the wilderness. -- ai Qing "
Continue to see what is the simple selection sort.
Welcome reprint, Reproduced please indicate the source:
1. Simple selection of sorting
Sets the number of records for the sorted sequence to n. I take the,..., n-1, and from All N-i+1 records (ri,ri+1,..., Rn) to find the smallest record of the sort code, exchange with the first record. The sequencing of the record sequence is completed after the n-1 is executed.
2. Code
Code is relatively simple, look at the source.
Following execution:
3. Source Code
#include "Stdio.h"
/**
* minimum value of the array
*
* @return int the key value of the array
*/
int Selectminkey (inta[],intn, int i)
{
int k= i;
for (intj=i+1;j<n; ++j) {
if (a[k]> a[j]) k = j;
}
returnK;
}
/**
* Select Sort
*
*/
void selectsort (inta[],intn) {
intkey, tmp;
for (int i= 0; i< n; ++i) {
key= Selectminkey (a,n, i); // Select the smallest element
if (key!= i) {
tmp= a[i]; a [I] = a[key]; a [Key]= tmp; // the minimum element and the first I position Element Interchange
}
}
}
int Main () {
intA[8] = {3,1,5,7,2,4,9,6};
printf (" initial value:");
for (intj= 0; j<8; j + +) {
printf ("%d", A[j]);
}
printf ("\ n sort:\ n");
Selectsort (a,8);
for (intj= 0; j<8; j + +) {
printf ("%d", A[j]);
}
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
14. Toad's data structure Advanced 14 sort implementation simple selection sort