Instance 365 (13) --------- classic array sorting method ------ select sorting method

Source: Internet
Author: User

I. Sort the one-dimensional arrays by the selection sorting method,

/* Personal understanding of sorting:
First filter, select the maximum value, and obtain the value and subscript.
The outer loop exchanges the position of the maximum value with the first position of the array.
Filter again from the second position of the array
Swap the location of the largest value with the second location of the array
Until the array is filtered
*/

Ii. Code
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; namespace selectSortArray {public partial class Form1: Form {public Form1 () {InitializeComponent ();} int [] array = {0, 5, 8, 7, 4, 6, 3, 2, 1, 9}; // create an array private void Form1_Load (object sender, EventArgs e) {for (int I = 0; I <array. length; I ++) // display the array in the text box {tb_numfirst.Text + = array [I]. toString () + "" ;}} private void btn_score_Click (object sender, EventArgs e) {tb_new.Clear (); // clear existing display int tem = 0; // used to store the maximum number of int num = 0; // used to store the subscript for (int j = 0; j <array. length-1; j ++) // Save the maximum number to the position and specify it as the switching position without the last {tem = array [j]; // used to store the maximum number num = j; // used to store the subscript for (int I = j + 1; I <array. length-1; I ++) // obtain the maximum number in a group {if (array [I + 1]> tem) {tem = array [I + 1]; // store the maximum number num = I + 1; // store the subscript of the maximum number} array [num] = array [j]; // place the first place in the largest position of the value array [j] = tem; // place the largest number in the first place} for (int I = 0; I <array. length; I ++) // display the array in the text box {tb_new. text + = array [I]. toString () + "";}}}}

 

Related Article

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.