C#中的冒泡排序

2011-02-22  陈斌 

.c#冒泡排序:
/// <summary>
        /// 接受数字参数和排序方法参数,进行冒泡排序
        /// </summary>
        /// <param name="num">要进行排序的整数数组</param>
        /// <param name="sortedMethod">排序方法标识:1为升序,2为降序</param>
        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;
                        }
                    }
                }
            }
        }

    }
}

475°/4753 人阅读/0 条评论 发表评论

登录 后发表评论