------------------------------------------------------------------------------------------------------
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