C # Bubble Sorting☞Goto implementation

Source: Internet
Author: User

Today, I saw my buddy asked me a question about goto Bubble sorting, but it is useless to implement it with 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 ("sorted ");
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's explanation on MSDN (C # reference:

The goto statement directly passes program control to the mark statement.

A common usage of goto is to pass the control to a specific switch-case label or the default label in the switch statement.

The goto statement is also used to jump out of a deep nested loop.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.