标签:
算法是编程的精髓,牢记这些经典!
冒泡排序,从小到大。
int[] array = new int[] { 1, 5, 2, 8, 3, 45, 32, 78, 214, 43, 16, 12, 43, 77, 89, 31, 12, 87, 76, 921, 836, 827 }; for (int i = 0; i < array.Length; i++) { int temp = 0; for (int j = i + 1; j < array.Length; j++) { if (array[i] > array[j]) { temp = array[j]; array[j] = array[i]; array[i] = temp; } } } for(int k=0;k<array.Length;k++) { Response.Write(array[k]+","); }
冒泡排序,从大到小
int[] array = new int[] { 1, 5, 2, 8, 3, 45, 32, 78, 214, 43, 16, 12, 43, 77, 89, 31, 12, 87, 76, 921, 836, 827 }; for (int i = 0; i < array.Length; i++) { int temp = 0; for (int j = i + 1; j < array.Length; j++) { if (array[i] < array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } } for(int k=0;k<array.Length;k++) { Response.Write(array[k]+","); }
标签:
原文地址:http://www.cnblogs.com/loyung/p/5666441.html