Computer Classic algorithm--bubble sort (descending sort)
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 6 namespacesorttest7 {8 class Program9 {Ten /* One * Descending Order A */ - Static voidSort (int[] iNum) - { the for(inti =0; I < inum.length;i++ ) - { - for(intj=i+1; j<inum.length;j++) - { + inttemp =0; - if(inum[i]<Inum[j]) + { Atemp =Inum[i]; atInum[i] =Inum[j]; -INUM[J] =temp; - } - } - } - } in Static voidPrintnumber (int[] inums) - { toConsole.Write ("Order of the arrays:"); + foreach(varINuminchinums) - { theConsole.Write (inum+" "); * } $ Console.WriteLine ();Panax Notoginseng } - Static voidMain (string[] args) the { + int[] Inumber = {9,6,7,8}; A Printnumber (inumber); the Sort (inumber); + Printnumber (inumber); - Console.readkey (); $ } $ } -}
2. Bubble sort (Ascending sort)
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceascendingsort{classProgram {/************************************************************************/ /*Ascending order*/ /************************************************************************/ Static voidAscsort (int[] iNum) { for(inti =0; I < inum.length;i++ ) { for(intj = inum.length-1; J > i; j--) { if(inum[i]>Inum[j]) { inttemp =0; Temp=Inum[i]; Inum[i]=Inum[j]; INUM[J]=temp; } } } } Static voidPrintnumber (int[] inums) {Console.Write ("Order of the arrays:"); foreach(varINuminchinums) {Console.Write (INum+" "); } Console.WriteLine (); } Static voidMain (string[] args) { int[] Inumber = {9,6,7,8 }; Printnumber (Inumber); Ascsort (Inumber); Printnumber (Inumber); Console.readkey (); } }}
Another way to sort ascending order
/// <summary> ///Ascending order/// </summary> /// <param name= "INum" ></param> Static voidBubble (int[] iNum) { inttemp =0; for(inti = inum.length; i >0; i--) { for(intj =0; J < i-1; J + +) { if(Inum[j] > inum[j +1]) {temp=Inum[j]; INUM[J]= Inum[j +1]; Inum[j+1] =temp; } } } }
Bubble sort--algorithm