
Bubble sort algorithm static List BubbleSort1(List list) In connection with this kind of thinking, it is conceivable that the bubble sort should be to let the big number gradually rise, until it rises to the front of the larger number, and it is broken.Īccording to this kind of thinking, there is roughly a process formed in the mind, take a look at the code: (below FigureIt's pretty imagery, so I stole it) This bubble will gradually become larger as it rises (caused by the smaller and smaller water pressure). Seeing the magic, the next step is to have a deeper understanding of the idea and implementation of the algorithm.įirst, let’s explain the bubbling. Judging from the above results, quick sort is not a bit faster than bubble sort.
#SELECTION SORT VS BUBBLE SORT VS QUICKSORT CODE#
This code is taken from the reference link, I am lazy, and the bubble sort algorithm is still very simple, so I won't write it myself Swap if the previous number is greater than the next if (list > list) The first loop: Indicates the number of comparisons, such as the number of unt, which must be compared count-1 times for ( int i = 0 i i: The subscript from back to front must be greater than the subscript from front to back, otherwise it will be exceeded. Bubble sort algorithm static List BubbleSort(List list) Insert 2k random numbers into the array for ( int j = 0 j single).ToList() Ĭonsole.WriteLine( " \nQuick sorting takes time: " + watch.ElapsedMilliseconds) Ĭonsole.WriteLine( " Before the output is ten numbers: " + string.Join( ", ", result.Take( 10 ).ToList())) Ĭonsole.WriteLine( " \nBubble sorting takes time: " + watch.ElapsedMilliseconds) Five comparisons for ( int i = 1 i list = new List ()
