C # Bubble Sorting

Source: Internet
Author: User

The basic concept of bubblesort is to compare two adjacent numbers in sequence, put decimal places in front, and put large numbers in the back. That is, first compare the numbers of 1st and 2nd, and put the decimal places before and after the large numbers. Then compare the numbers of 2nd and 3rd, place the decimal places before and after the large number, and continue until the last two digits are compared. Place the decimal places before and after the large number. Repeat the above process and compare it from the first logarithm (because of the exchange of 2nd numbers and 3rd numbers, the number of 1st numbers is no longer less than 2nd, always compare to the adjacent number before the maximum number. Place the decimal number before, place the large number, and end the second row. In the second to last number, a new maximum number is obtained. So on until the sorting is completed.

Because the sorting process always places decimal places forward and large numbers backward, it is equivalent to bubbles rising, so it is called Bubble sorting.

It is implemented using a double loop. The External Loop Variable is set to I and the inner loop variable is set to J. The External Loop repeats 9 times, and the inner loop repeats 9, 8,..., 1 time in sequence. The two elements for each comparison are related to the inner loop J, which can be identified by a [J] And a [J + 1] respectively, the values of I are 1, 2 ,..., 9. For each I, j values are 1, 2 ,... 10-i.

Bubble Sorting is stable.

Generate

In many program designs, we need to sort a sequence to facilitate statistics. Common sorting methods include Bubble sorting, binary tree sorting, and selective sorting. Bubble Sorting has always been favored by its concise ideas and methods and high efficiency.

Sorting Process

Imagine the sorted array R [1 .. n] vertical erect, each data element is considered as a bubble with weight, according to the principle that light bubbles cannot be under heavy bubbles, scanning array R from bottom up, when a light bubble that violates this principle is scanned, it is made to "float" up, so it is repeated until the last two bubbles are both light and heavy.

 

C # code:

 

         /// <summary>
/// Bubble Sort
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="array"></param>
public static void BubbleSort<T>(T[] array) where T : IComparable
{
int length = array.Length;
for (int i = 0; i < length; i++)
{
T temp = array[i];
for (int j = i; j < length; j++)
{
if (temp.CompareTo(array[j]) >0)
{
temp = array[j];
array[j] = array[i];
array[i] = temp;
}
}
}
}

Test code:

 

             int[] array = new int[] {23,15,27,90,69,66,158,45,32,1,45,22,66,77,44};
Console.WriteLine("before bubble sort");
foreach (int i in array)
{
Console.Write(i+"->");
}
Console.WriteLine();
SortHelper.BubbleSort<int>(array);
Console.WriteLine("after bubble sort");
foreach (int i in array)
{
Console.Write(i + "->");
}
Console.WriteLine();
Console.Read();

 

 

Running result:

 

 before bubble sort
23->15->27->90->69->66->158->45->32->1->45->22->66->77->44->
after bubble sort
1->15->22->23->27->32->44->45->45->66->66->69->77->90->158->

 

Related Article

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.