Bubble Sort
来源:互联网 发布:无网络摄像头 编辑:程序博客网 时间:2024/04/29 22:05
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Sort{ /// /// http://en.wikipedia.org/wiki/Bubble_sort /// class BubbleSort { public BubbleSort() { Console.WriteLine("=========================================="); Console.WriteLine("O(n²)"); } /// 冒泡算法 /// /// /// 是否顺序排序 /// public int[] BubbleArithmetic(int[] abarray, bool IsAscending) { if (abarray.Length > 0) { for (int i = abarray.Length - 1; i >= 0; i--) { for (int j = i - 1; j >= 0; j--) { if (CheckAccordCondition(abarray[i], abarray[j], IsAscending)) { ExChangeValue(ref abarray[i], ref abarray[j]); } } } } return abarray; } /// /// 交换数据 /// /// /// private void ExChangeValue(ref int A, ref int B) { int Temp = A; A = B; B = Temp; } /// /// 是否符合条件 /// /// private bool CheckAccordCondition(double data1, double data2, bool IsAscending) { if (data1 > data2) { return IsAscending == true ? true : false; } else { return IsAscending == true ? false : true; } } }}