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

Source: Internet
Author: User

I. Sort one-dimensional arrays by selection and Bubble sorting,

/* Personal understanding of sorting:
In the first loop, select the largest 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
*/

/* Personal understanding of Bubble Sorting

In the first inner loop, place the two adjacent numbers on the right to obtain the maximum value of the last edge.

The External Loop will carry out the next internal loop and place the maximum number of times to the second to last position.

The number of sorting results obtained by executing n-1 in an external loop.

*/

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, 9, 1}; // 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) {# region: // In the first loop, select the maximum value, get the value and subscript // the outer loop switches the location of the maximum value to the first position of the array // filter the value from the second position of the array and the second position of the array. // until the array is filtered out. // * // tb_new.Clear (); // clear the existing display // int tem = 0; // used to store the maximum number of bytes. // int num = 0; // used to store subscript // for (int j = 0; j <array. length-1; j ++) // Save the obtained maximum number to the position and specify it as the switching position without the last digit. // {// tem = array [j]; // used to store the maximum number // num = j; // used to store the subscript of the maximum number // 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]; // Save the maximum number. // num = I + 1; // store the subscript of the maximum number //} // array [num] = array [j]; // place the first vertex in the largest position of the value. // array [j] = tem; // place the largest vertex 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 () + ""; //} # endregion # region Bubble Sorting/* place the greater value of the adjacent two numbers in the first internal loop to the right, in this way, the value of the last edge is the largest external loop, place the maximum number of times in the second to the last position and execute n-1 times in the external loop to obtain the number of sorted times */for (int j = 0; j <array. length-1; j ++) // make sure that the maximum number is placed on the right for (int I = 0; I <array. length-1; I ++) // compare two adjacent numbers, and place a large number on the right, so that the last number is the largest {if (array [I]> array [I + 1]) // determine whether the left side is greater than the right side {int tem = array [I]; // used to store a large number of arrays [I] = array [I + 1]; // place a small number to the left array [I + 1] = tem; // place a large number on the right }}for (int I = 0; I <array. length; I ++) // display the array in the text box {tb_new.Text + = array [I]. toString () + "" ;}# endregion }}}

 

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.