使用選擇排序對一維陣列進行排序
阿新 • • 發佈:2018-12-04
實現效果:
實現原理:
實現程式碼:
static void Main(string[] args) { int[] int_born = {2,4,1,8,6,5,7,3,0,6,4}; Program pro = new Program(); pro.outList(int_born); pro.outList(pro.sory(int_born)); } //定義選擇排序方法 public int[] sory(int[] intArray) { int min; for (int i = 0; i < intArray.Length - 1;i++ ) { min = i; //儲存最小值下標 for (int j = i + 1; j < intArray.Length;j++ ) { if (intArray[j] < intArray[min]) min = j; } int temp = intArray[min]; intArray[min] = intArray[i]; intArray[i] = temp; } return intArray; } //定義遍歷輸出方法 public void outList(int[] arr) { string str=""; foreach(int s in arr) str+=(s+" "); Console.WriteLine("陣列元素為:\n" + str); }