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