Several common sorting algorithms

Source: Internet
Author: User

1 Bubble Sort Method

For example, array 3,9,27,6,18,12,21,15

First round:

Sorting selects the first digit as the comparison object, then compares the subsequent number, if it is smaller than 3

       3, 9, 27,6,18,12,21,15

      3, 9,6,18,12,21,15

      3, 9,27,6,18,12,21,15

      3, 9,27,6,12,21,15

      3, 9,27,6,18,21,15

      3, 9,27,6,18,12,15

       3,9,27,6,18,12,21,

Second round: Select the second digit as the comparison object, followed by the number of comparisons

3,9, 27,6,18,12,21,15

3,9,6,18,12,21,15

3,6,9, 18,12,21,15 9:6 Large Exchange

3,6, 27,9,12,21,15

3,6, 27,9,18,21,15

3,6, 27,9,18,12,15

3,6, 27,9,18,12,21,

Third round: Select the third digit as the comparison object, followed by the number of comparisons

3,6,9, 18,12,21,15 27:9 in Exchange

3,6,9,18,12,21,15

3,6,9, 12,21,15,

3,6,9, 27,18,21,15

3,6,9, 27,18,12,15

3,6,9, 27,18,12,21,

  Fourth round: Select the fourth digit as the comparison object, followed by the number of comparisons

3,6,9,12,21,15,27:18, Exchange

3,6,9,21,15, 18:12,the Exchange

3,6,9,27,18, 15

3,6,9,27,18,21,

Fifth round: Select the fifth digit as the comparison object, followed by the number of comparisons

3,6,9,12,21,15,27:18, Exchange

3,6,9,12,15

3,6,9,12,27,21, 18:15 in Exchange

3,6,9,12,27,21,

Sixth round: Select the sixth digit as the comparison object, followed by the number of comparisons

3,6,9,12,15,18,27:21, Exchange,

3,6,9,12,15,21:18, the Exchange

3,6,9,12,15,,,

The seventh round: Select the number of digits as the comparison object, and then compare with the following number

3,6,9,12,15,18,27:21 in Exchange

3,6,9,12,15,18,21,27

Here's the code:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 7 namespace the8 {9     class ProgramTen     { One         Static voidMain (string[] args) A         { -             int[] arr =New int[] {3,9, -,6, -, A, +, the }; Defines a one-dimensional array and assigns values -             foreach(intMincharr)//iterate through the defined one-dimensional array and output the elements within it theConsole.Write (M +" "); - Console.WriteLine (); -             intJ, temp; Defines two variables of type int, respectively, to represent the following table in the array and to store the new array elements -              for(intI=0; I<arr. length-1; i++)//Iterate through the array elements based on the value of the data +{j = i +1; - ID://define an identity so that you can start executing statements from here +                 if(Arr[i] >arr[j])//To determine the size of the front and back two numbers A                 { attemp =Arr[i]; Assigns a larger element to the defined int variable -Arr[i] =Arr[j]; Assigns the value of the latter element to the previous element -ARR[J] =temp; Assigns the value of an element stored in an int variable to the latter element -                     GotoID; Returns the identity, continuing to determine the following element -                 } -                 Else in                     if(J<arr. length-1)//To determine whether to execute to the last element -                     { toJ + +; If not, then judge again. +                         GotoID; Returns the identity, continuing to determine the following element -                     } the             } *             foreach(intNincharr)//Iterate through the sorted array elements and output $Console.Write (n +" ");Panax Notoginseng Console.WriteLine (); - console.readline (); the              +  A               the         } +     } -}

Several common sorting algorithms

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.