"2017-02-28" Bubble Sort

Source: Internet
Author: User
Tags readline

The bubble sort is more than the size, if the former is greater than the latter, then the two Exchange Positions. Implemented with two for loop nesting

usingSystem;usingSystem.Collections.Generic;usingsystem.linq;usingsystem.text;namespaceArray {classprogram {Static voidMain (string[] Args) {            //Bubble Sort 3 5 1 2 4 (12345)//Idea: The first for loop executes the first time when the first number is taken out and compared to the next number, and if the first number is larger than the following number, the two Exchange positions. //the smallest number has been placed on top when the first for loop is Finished. //the first for Loop executes the second time, the second number is taken out and the number behind is compared, if the second number is larger than the number behind, then the two exchange position ....//and so on, complete the bubbling sort            int[] A =New int[] {3,5,2,1,4 };  for(inti =0; I < a.length-1; I++)//drag each number out, and the last number is not compared to the next empty One.             {                 for(intj = i +1; J < a.length; J + +)//the number used to compare, starting from I+1                {                    if(a[i] > A[j])//if the number of pulls is larger than the number to compare, the Two-digit exchange position                    {                        intf = a[i];//exchanging the values of the two with a third partya[i] =a[j]; a[j]=f; }                }            }             for(inti =0; I < a.length; i++) {console.writeline (a[i]);        } console.readline (); }    }}

Job Title:

string[] ss = new string[5]{"aaa", "a", "aa", "aaaaa", "aaaa"};
From big to small print out, from small to large print out

usingSystem;usingSystem.Collections.Generic;usingsystem.linq;usingsystem.text;namespaceBubble Sort {classprogram {Static voidMain (string[] Args) {            string[] SS =New string[5] {"AAA","a","AA","AAAAA","AAAA" };  for(inti =0; I < SS. length-1; i++)            {                 for(intj = i+1; J < SS. Length; J + +)                {                    if(ss[i]. Length >ss[j]. Length) {stringA =ss[i]; ss[i]=ss[j]; ss[j]=a; }                }            }             for(inti =0; I < SS. Length; i++) {console.writeline (ss[i]);                } console.readline ();  for(inti =0; I < SS. Length-1; i++)                {                     for(intj = i +1; J < SS. Length; J + +)                    {                        if(ss[i]. Length <ss[j]. Length) {stringA =ss[i]; ss[i]=ss[j]; ss[j]=a; }                    }                }                 for(inti =0; I < SS. Length; i++) {console.writeline (ss[i]);        } console.readline (); }    }}

"2017-02-28" Bubble Sort

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.