using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int[] array = { 1, 2, 3, 4, 5, 6 }; bubble_sort(array, SortType.rise); for (int i = 0; i < array.Length; i++) { Console.WriteLine(array[i]); } Console.ReadKey(); } //冒泡算法 static void bubble_sort(int[] unsorted, SortType type) { if (type == SortType.rise) { for (int i = 0; i < unsorted.Length; i++) { for (int j = i; j < unsorted.Length; j++) { if (unsorted[i] > unsorted[j]) { int temp = unsorted[i]; unsorted[i] = unsorted[j]; unsorted[j] = temp; } } } } else if(type == SortType.drop) { for (int i = 0; i < unsorted.Length; i++) { for (int j = i; j < unsorted.Length; j++) { if (unsorted[i] < unsorted[j]) { int temp = unsorted[i]; unsorted[i] = unsorted[j]; unsorted[j] = temp; } } } } } } public enum SortType { drop, rise } }
原文地址:http://www.cnblogs.com/kkun/archive/2011/11/23/2260280.html