14. Toad's data structure Advanced 14 sort implementation simple selection sort

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.