选择排序和冒泡排序
2014-07-15 14:50 图安 阅读(182) 评论(0) 编辑 收藏 举报static void Main(string[] args) { int[] arr = new int[] { 11,1, 9, 4, 6, 8, 6, 11, 30 }; printMM(arr); SelectSort(arr); printMM(arr); int[] arr1 = new int[] { 11, 1, 9, 4, 6, 8, 6, 11, 30 }; printMM(arr1); BoubleSort(arr1); printMM(arr1); } static void printMM(int[] inputarr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < inputarr.Length; i++) { sb.Append(inputarr[i]).Append(","); } Console.WriteLine(sb.ToString()); } /// <summary> /// 选择排序 /// </summary> /// <param name="inputArr"></param> static void SelectSort(int[] inputArr) { for (int i = 0; i < inputArr.Length-2; i++) { for (int j = i+1; j <= inputArr.Length-1; j++) { if(inputArr[i]>inputArr[j]) { int temp = inputArr[i]; inputArr[i] = inputArr[j]; inputArr[j] = temp; } } } } /// <summary> /// 冒泡排序 /// </summary> /// <param name="inputArr"></param> static void BoubleSort(int[] inputArr) { for (int i = 0; i < inputArr.Length - 1; i++) { bool isExchange = false; for (int j = inputArr.Length-2; j >=i; j--) { if (inputArr[j] > inputArr[j+1]) { int temp = inputArr[j+1]; inputArr[j+1] = inputArr[j]; inputArr[j] = temp; isExchange = true; } } if (!isExchange) break; } }