[Project Combat] Bubble Sort of Sorting Algorithm

1. What is bubble sort?

Bubble sort (Bubble Sort) is a simple sorting algorithm.
Bubble Sort (Bubble Sort) repeatedly traverses the array to be sorted, compares two elements at a time, and swaps them if they are in the wrong order. The work of traversing the array is repeated until there is no need to exchange, that is to say, the array has been sorted. The name of this algorithm comes from the fact that the smaller elements will slowly "float" to the top of the sequence through exchange.

Second, the steps of bubble sort

The steps of bubble sort are as follows:

Compare adjacent elements. If the first is greater than the second (ascending sort), swap them both.
Do the same for each pair of adjacent elements, from the first pair at the beginning to the last pair at the end. After this step is done, the last element will be the largest number.
Repeat the above steps for all elements except the last one.
Continue repeating the above steps for fewer and fewer elements each time until there are no pairs of numbers to compare.
This is an unstable sorting algorithm (for example, identical numbers that were in adjacent positions may swap positions during the sorting process).

3. Bubble sorting algorithm design idea

By comparing two adjacent elements, the smaller element is placed in front and the larger element is placed in the back, so as to achieve the purpose of sorting.

Guess you like

Origin blog.csdn.net/wstever/article/details/129927167