冒泡排序(C#)

从小到大

/// <summary>
/// 冒泡排序(从小到大)
/// </summary>
/// <param name="list"></param>
void Sort(List<int> list)
{
    for (int i = list.Count - 1; i > 0; --i)
    {
        for (int j = 0; j < i; j++)
        {
            if (list[j] > list[j + 1])
            {
                int temp = list[j];
                list[j] = list[j + 1];
                list[j + 1] = temp;
            }
        }
    }
}

 

从大到小

/// <summary>
/// 冒泡排序(从大到小)
/// </summary>
/// <param name="list"></param>
void Sort(List<int> list)
{
    for (int i = list.Count - 1; i > 0; --i)
    {
        for (int j = 0; j < i; j++)
        {
            if (list[j] < list[j + 1])
            {
                int temp = list[j];
                list[j] = list[j + 1];
                list[j + 1] = temp;
            }
        }
    }
}

 

posted @ 2014-05-06 10:35  疯人愿  阅读(498)  评论(2编辑  收藏  举报