冒泡排序算法,就像它的名字一樣生動形象,這個算法需要內外循環兩次,外循環是控制整體的排序次數,外排次數=數組長度-1,關於這個也很好理解,比如10個數只要其他的9個位置確定了最后一個位置也就確定了,所以外排序的次數就是數組總個數減去1.內排序主要是控制每次的排序比較,之所以稱之為冒泡就是因為每次排序的開始都是從數組的最后一位依次跟前一位比,就像氣泡從水底冒出來一樣,所以稱之為冒泡排序
2,12,16,32,34,43,49,54,67,87
static void Main(string[] args) { int temp; int[] shuju = new int[10]{12,43,2,34,87,54,32,16,67,49}; for (int i = 0; i < shuju.Length; i++) { for (int j = i + 1; j < shuju.Length ; j++) { if (shuju[j] < shuju[i]) { temp = shuju[j]; shuju[j] = shuju[i]; shuju[i] = temp; } } } for (int j = 0; j < shuju.Length; j++) { Console.Write("{0}"+" ",shuju[j]); } Console.ReadLine(); } //(2) //static List<int> list = new List<int>() { 12, 43, 2, 34, 87, 54, 32, 16, 67, 49 }; //static void Main(string[] args) //{ // Bubble(); // PrintList(); // Console.ReadLine(); //} //static void Bubble() //{ // int temp = 0; // for (int i = list.Count; i > 0; i--) // { // for (int j = 0; j < i - 1; j++) // { // if (list[j] > list[j + 1]) // { // temp = list[j]; // list[j] = list[j + 1]; // list[j + 1] = temp; // } // } // //PrintList(); // //Console.Write(string.Format("{0} ", list)); // } //} //private static void PrintList() //{ // foreach (var item in list) // { // Console.Write(string.Format("{0} ", item)); // } // Console.WriteLine(); //} //(3) //int[] arr = { 12, 43, 2, 34, 87, 54, 32, 16, 67, 49 }; //int tmp = 0; //for (int i = 0; i < arr.Length - 1; i++) //{ // for (int j = 0; j < arr.Length - 1; j++) // { // if (arr[j] > arr[j + 1]) // { // tmp = arr[j]; // arr[j] = arr[j + 1]; // arr[j + 1] = tmp; // } // } //} //for (int j = 0; j < arr.Length; j++) //{ // Console.Write("{0} ", arr[j]); //} //Console.ReadLine(); }