C#排序方法
//冒泡排序法 static void BubbleSort(int[] w) { int hang=w.Length ; int temp; for(int i=0;i<hang-1;i++) { for (int j = i + 1; j < hang;j++ ) if ( w[i] > w[j]) { temp = w[j]; w[j] = w[i]; w[i] = temp; } } } //選擇排序法 public static void selectionSort(int[] list) { int min; for (int i = 0; i < list.Length - 1; i++) { min = i; for (int j = i + 1; j < list.Length; j++) { if (list[j] < list[min]) min = j; } int t = list[min]; list[min] = list[i]; list[i] = t; } } //插入排序法 public static void insertSort(int[] list) { for (int i = 1; i < list.Length; ++i) { int t = list[i]; int j = i; while ((j > 0) && (list[j - 1] > t)) { list[j] = list[j - 1]; j--; list[j] = t; } } } static void Main(string[] args) { //int[][] arr = new int[2][]; //arr[0] = new int[5] { 1, 3, 5, 7, 9 }; //arr[1] = new int[4] { 2, 4, 6, 8 }; //for (int i = 0; i < arr.Length; i++) //{ // System.Console.Write("Element({0}):", i); // for (int j = 0; j < arr[i].Length; j++) // { // System.Console.Write("{0}{1}", arr[i][j]," "); // } // System.Console.WriteLine(); //} // Console.ReadLine(); int[] iArray = { 1, 5, 3, 6, 10, 55, 9, 2, 87 }; Console.WriteLine("排序前:"); foreach (int x in iArray) Console.Write("{0}", x); Console.WriteLine(); Console.WriteLine("排序后:"); selectionSort(iArray); foreach (int x in iArray) Console.Write("{0}", x); Console.ReadLine(); }
好的代码就和美食一样,都是需要时间烹饪出来的!