现在的位置: 首页 > 综合 > 正文

C#冒泡排序☞goto实现

2012年08月27日 ⁄ 综合 ⁄ 共 1142字 ⁄ 字号 评论关闭

今天看一到哥们问了我个goto冒泡排序的问题,只是用goto实现下,没什么实用性:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DemoMsg
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = new int[] { 1, 5, 6, 9, 2, 45, 36 };
            foreach (int n in arr)
            {
                Console.WriteLine(n);
            }
            Console.WriteLine("排序后");
            GotoOrderBind(ref arr);
            foreach (int n in arr)
            {
                Console.WriteLine(n);
            }
            Console.ReadKey();

        }

    private static void GotoOrderBind(ref int[] arr)
        {
            int j = 0;
            for (int i = 0; i < arr.Length - 1; i++)
            {
                j = i + 1;
            id: if (arr[i] > arr[j])
                {
                    int t = arr[i];
                    arr[i] = arr[j];
                    arr[j] = t;

                    goto id;
                }
                else
                    if (j < arr.Length - 1)
                    {
                        j++;
                        goto id;
                    }
            }
        }
    }
}

goto的在MSDN(C#参考)上的解释:

goto 语句将程序控制直接传递给标记语句。

goto 的一个通常用法是将控制传递给特定的 switch-case 标签或 switch 语句中的默认标签。

goto 语句还用于跳出深嵌套循环

抱歉!评论已关闭.