冒泡排序c#
阿新 • • 發佈:2018-10-16
function tin space color write lin pan style 數組
using System; namespace c練習五 { class Program { public static void Main(string[] args) { string str=Console.ReadLine();//請用戶輸入一個數 string[] strArray =str.Split(‘ ‘);//用空格分隔,得到字符串數組 int[] numArray=new int[strArray.Length];//將string類型轉化成int類型for (int i = 0; i < strArray.Length; i++) { int temp=Convert.ToInt32(strArray[i]); numArray[i]=temp; } //Array.Sort(numArray);//Array.Sort排序法;使用clr排序法,其實是快速排序法 //for (int i = 0; i < numArray.Length; i++) {//Console.Write(numArray[i]+""); //} for (int j = 0; j < str.Length-1; j++){//外層循環來控制子for循環執行的次數 //讓下面的for循環執行length-1次 for (int i = 0; i < numArray.Length-1; i++) { //numArray[i] <numArray[i+1]作比較把最大的放在後面 if (numArray[i+1]<numArray[i]) { int temp=numArray[i]; numArray[i]=numArray[i+1]; numArray[i+1]=temp; } } } for (int i = 0; i < numArray.Length; i++) { Console.Write(numArray[i]+""); } // TODO: Implement Functionality Here Console.Write("Press any key to continue . . . "); Console.ReadKey(true); } } }
冒泡排序c#