Do you really understand the Bubble sorting?

Source: Internet
Author: User

/* Do you really understand Bubble sorting? Or is it backed up? Is there really only one way to sort bubbles?
* Isn't it better to simply solve some complicated things?

* Although the methods are different, the thoughts are similar. I hope you can understand them carefully ......
**/

Using system;

Namespace sort
{
Public class sort

{

// Bubble sort 1

// Isn't it easy to understand? It doesn't matter. Take a look at the next method, which is absolutely easy to understand.

Public void bubblesort (INT [])
{

// Define a temporary variable. Should I be familiar with the C language I have learned in order to exchange locations?
Int TMP;
For (INT I = 0; I <A. Length-1; I ++)
{
Int idx = I; // Is it a way to use the index in the array?
For (Int J = idx + 1; j <A. length; j ++)
If (A [idx] <A [J])
Idx = J;// Actually, it is to find the index with the largest number.

// The following is the exchange process.
TMP = A [I];
A [I] = A [idx];
A [idx] = TMP;
}

}

// Bubble sort 2

// If you think this is hard for you, let's take a look at the following:

Public void bubblesort (INT [])
{

// define a temporary variable. Should I be familiar with the C language I have learned to exchange locations?
int TMP;
for (INT I = 0; I <. length-1; I ++)
{< br> // int idx = I; can this sentence be omitted?

// Is this easier to understand?
For (Int J = I + 1; j <A. length; j ++)
{

// If you are bigger than me, I will exchange with you. If you are younger than me, you will continue to find something bigger than me. If you cannot find it, I will be the biggest, right?
If (A [I] <A [J])
{
TMP = A [J];
A [J] = A [I];
A [I] = TMP;
}
}

}

}

 

// RecursionAlgorithm, Simulate bubble three

// A little bit around. It would be easier if you understood the first two methods.

Public void bubblesort (INT [])
{

// Call the following method
Bubble (A, 0, A. Length );
}

Void bubble (INT [] A, int start, int end)
{

// Note: Infinite recursion is possible without this judgment condition.
If (Start> = end)
{
Return;
}
Int TMP;

// Similar to the two methods above

For (INT I = start; I <End-1; I ++)
{
If (A [start] <A [I + 1])
{
TMP = A [start];
A [start] = A [I + 1];
A [I + 1] = TMP;
}
}

// Recursion

Bubble (A, start + 1, end );
}

}

}

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.