Sorting Algorithm - Bubble Sort .net achieve

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/Leaderxin/article/details/102604972

Sorting Algorithm - Bubble Sort .net achieve

The basic idea

I thought bubble sort is the time to compare two adjacent elements, if they put them in the wrong order switching position until all the elements in the correct position.

Legend algorithm

For example, we need to 1,235,991,876 this sort in descending number 5.
First, comparing the first and second place, it found that 12 smaller than 35, then the need to exchange position, the exchange result was 3512991876;
The method then compare just the second and third, 12 to 99 change position requires smaller, the result is the exchange 3599121876;
cycle continues according to the above method of comparison third and fourth and fifth and four, the final result is 3599187612After this round of four, we find the minimum number of 12 has been successfully in place.
Comparing each time two adjacent numbers, if the number is larger than the latter in front of the number, the switch positions of the two numbers. It has been relatively down until the last two numbers the comparison, the smallest number in the last one. As if it were a bubble, one step back "rolling" until the last one. So this sort of approach has a very good name "bubble sort."
Here Insert Picture Description
Then we repeat the process for the second round compared sequentially comparing the first and second, second and third, third and fourth place, (fourth and fifth place do not compare, because The first round has been determined that the fifth minimum value), the result is the final adjustment
3599761812.
Comparing the third round of the same logic, the final result is adjusted 9976351812.
The fourth round of the comparison is 9976 351 812, i.e. to complete the order.

Coding

Waste not say, directly on the code

public class SortHelper
    {
        public static T[] MP_Sort<T>(T[] array) where T : IComparable
        {
            for (int i = 0; i < array.Length - 1; i++)    //外层循环需要n-1次
            {
                for (int j = 0; j < array.Length - 1 - i; j++)
                {
                    if (array[j].CompareTo(array[j + 1]) < 0) //如果array[j]比array[j+1]小,那么需要互换位置
                    {
                        T temp = array[j];
                        array[j] = array[j + 1];
                        array[j + 1] = temp;
                    }
                }
            }
            return array;
        }
    }

Call execution results are as follows:
Here Insert Picture Description

Analyzed

The core part of the bubble sort is a double nested loop, the advantage of relatively simple algorithm clear thinking, only about ten lines of code, suitable for beginners, also obvious shortcoming, time complexity is O (N 2 ), less efficient. Next will bring more common and more efficient quick sort everyone.
~ If you have questions or blog newcomers error, we also hope to make corrections in the comments

Next: Sorting algorithms - quick sort .net achieve .

Guess you like

Origin blog.csdn.net/Leaderxin/article/details/102604972