To sort 10 integers with a selection method

Source: Internet
Author: User

------------------------------------------------------------------------------------------------------

Select sort: is to select the smallest (or largest) element of the data element to be sorted each time, and store it at the beginning of the sequence until all the data elements to be sorted are exhausted. Select Sort is an unstable sort method (for example, sequence [2, 2, 0] swaps the first [2] with [0] for the first time, causing the first 2 to move back to the second 2).

------------------------------------------------------------------------------------------------------

The C language code is as follows:

# include <stdio.h># include <stdlib.h># include <string.h> Void sort (int a[], int x) {     int m, n, k,  ret=0;     for  (m = 0; m < x - 1; m++              /* Nested loops implement a number compared to the remaining number one by one */      {          k = m;           for  (n = m + 1; n  < x; n++)           {                if  (A[n]<a[k])                 {                     k = n;                }           }          ret = a[k];                          /* the minimum and first number exchange positions */          a[k] =  a[m];          a[m] = ret;      }}int main () {     int arr[10] = { 3,6,13,9,2,1,18,30,20,10};     int len = 10;      int i;          printf ("Sequence before sorting:");      for  (i = 0; i < 10; i++)      {           printf ("%d ",  arr[i]);     }      printf ("\ n");          sort (Arr,len);      printf ("Sorted sequence:");     for  (i = 0; i < 10;  i++)      {          printf ("% d  ", Arr[i]);      }     printf (" \ n ");       system ("pause");      return 0;}

----------------------------------------------------------------------------------------

little knowledge of dry goods: The return value of GetChar () is int, which is generally used (C=getchar ()!=eof) to determine whether the input ends, and the char type does not necessarily hold the value of EOF, so the return value is accepted with Int. C99:char is generally used to store the basic execution character set, whose value is positive, and other values stored in char are defined by the reality. The value of EOF is defined in <stdio.h>, as long as it is different from the character set, C99 defines a macro whose value is negative for an int, and some defines EOF as-1, and if your compiler char type is unsigned, the return value is 1 and cannot be received with Char , and if it is signed, it can be received. However, in order to improve the portability of the program, the return value of char is received with the int type.

----------------------------------------------------------------------------------------

This article is from the "Nobody" blog, please be sure to keep this source http://814193594.blog.51cto.com/10729329/1702555

Sort 10 integers with a selection method

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.