递归和冒泡算法
递归:
View Code
1 /// <summary> 2 /// 递归算法 3 /// </summary> 4 /// <param name="i">第几位数</param> 5 /// <returns>返回的第几位数的值</returns> 6 /// 一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第30位数是多少 7 /// 用递归算法实现 8 /// Console.WriteLine("\n第30位数是{0}", TestQuikeSorte(30)); 9 public static int TestQuikeSorte(int i) 10 { 11 if (i <= 0) 12 return 0; 13 else if (i > 0 && i <= 2) 14 return 1; 15 else return TestQuikeSorte(i - 1) + TestQuikeSorte(i - 2); 16 }
冒泡:
View Code
1 //冒泡排序算法 2 public void maobao() 3 { 4 //冒泡排序算法(从小到大的顺序排列) 5 int[] array = { 23, 45, 16, 7, 42, 1, 58, 12, 66, 26, 16 }; 6 bool isExchanged = false; 7 for (int i = 0; i < array.Length - 1; i++) 8 { 9 isExchanged = false; 10 for (int j = i + 1; j < array.Length; j++) 11 { 12 if (array[j] < array[i]) 13 { 14 int temp = array[j]; 15 array[j] = array[i]; 16 array[i] = temp; 17 isExchanged = true; 18 } 19 } 20 //一遍比较过后如果没有进行交换则退出循环 21 if (!isExchanged) 22 { 23 break; 24 } 25 } 26 foreach (int i in array) 27 { 28 Console.WriteLine(i); 29 } 30 }