【 Program37]
Question: Sorting 10 numbers
1. Program Analysis: You can use the selection method, that is, from the last nine comparison processes, select a minimum element to exchange with the first element,
Next time, compare and exchange the second element with the last eight.
2. Program Source code:
# Define N 10
Main ()
{Int I, j, Min, TEM, A [n];
/* Input data */
Printf ("Please input ten num:/N ");
For (I = 0; I <n; I ++)
{
Printf ("A [% d] =", I );
Scanf ("% d", & A [I]);}
Printf ("/N ");
For (I = 0; I <n; I ++)
Printf ("% 5d", a [I]);
Printf ("/N ");
/* Sort ten num */
For (I = 0; I <N-1; I ++)
{Min = I;
For (j = I + 1; j <n; j ++)
If (A [Min]> A [J]) min = J;
TEM = A [I];
A [I] = A [Min];
A [Min] = TEM;
}
/* Output data */
Printf ("after sorted/N ");
For (I = 0; I <n; I ++)
Printf ("% 5d", a [I]);
}
========================================================== ======================================
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.