C#:数组冒泡排序法(一)

数组元素从小到大排列,每次循环把最小的元素提取到最前面

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication2

{

    class Program

    {

        static void Main(string[] args)

        {

            int[] arr = new int[] { 45, 62, 32, 11, 37, 31 };

            int j, temp;

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

            {

                j = i + 1;

            ID:

                if (arr[i] > arr[j])

                {

                    temp = arr[i];

                    arr[i] = arr[j];

                    arr[j] = temp;

                    goto ID;

                }

                else

                    if (j < arr.Length - 1)

                    {

                        j++;

                        goto ID;

                    }

            }

            foreach (int n in arr)

            {

                Console.Write(n + " ");

            }

        }

    }

}

虽然goto语句有一定的使用价值,但是目前对它的使用存在争议。有人建议避免使用它,有人建议把它用来作为排除错误的基本工具,各种观点截然不同,虽然许多人不用goto语句也能够编程,但是仍然有人使用它。所以要小心使用,同时一定要确保程序是可以维护的。

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容