日期:2014-05-20  浏览次数:20847 次

(C#) 谁能给一个冒泡排序的例子啊
(C#) 谁能给一个冒泡排序的例子

------解决方案--------------------
http://www.cnblogs.com/luminji/archive/2011/08/18/2144160.html
http://blog.163.com/panjunlai/blog/static/5819030120104207755314/
示例
------解决方案--------------------
http://www.cnblogs.com/canflywolf/archive/2010/12/25/1916134.html
------解决方案--------------------
C# code

int[] numbers = new int[]{3,34,12,33,1,23,12};
            for(int i = 0; i < numbers.Length; i++)
            {
                for(int j = 0; j < numbers.Length;j++)
                {
                    if(numbers[i] < numbers[j])
                    {
                        numbers[i] = numbers[i] + numbers[j];
                        numbers[j] = numbers[i] - numbers[j];
                        numbers[i] = numbers[i] - numbers[j];
                    }
                            
                }
            }

------解决方案--------------------
public static void ExecuteSortedMethod(int[] num, int sortedMethod)
{
if (sortedMethod == 1) //升序排列
{
for (int i = 0; i < num.Length -1; i++)
{
for (int j = 0; j < num.Length - 1 - i; j++)
{
if (num[j] > num[j + 1])
{
int temp = num[j];
num[j] = num[j + 1];
num[j + 1] = temp;
}
}
}
}
if (sortedMethod == 2) //降序排列
{
for (int i = 0; i < num.Length - 1; i++)
{
for (int j = 0; j < num.Length - 1 - i; j++)
{
if (num[j] < num[j + 1])
{
int temp = num[j];
num[j] = num[j + 1];
num[j + 1] = temp;
}
}
}
}
}

}


------解决方案--------------------
楼上把sortedMethod变成一个枚举会更好。