欢迎光临散文网 会员登陆 & 注册

使用函数/方法 调用冒泡排序

2023-03-23 22:26 作者:此城过客  | 我要投稿

class Program

{

        private static void Sort(int[] sortArray)//定义一个冒泡排序的方法

        {

              bool swapped = true;

              do {

                          swapped = false;

                          for (int i = 0; i < sortArray.Length - 1; i++)

                          {

                                 if (sortArray[i] > sortArray[i + 1])

                                 {

                                        int temp = sortArray[i];

                                        sortArray[i] = sortArray[i + 1];

                                        sortArray[i + 1] = temp;

                                       swapped = true;

                                  }

                           }

                    } while (swapped);

          }



        static void Main(string[] args)

        {

               int[] sortArray = { 22,5,63,91,2,48,151,6,49,73 };

               Sort(sortArray);//调用冒泡排序


               foreach (int temp in sortArray)

               { Console.WriteLine(temp); }

        }

}


使用函数/方法 调用冒泡排序的评论 (共 条)

分享到微博请遵守国家法律