Code implementation of Python bubble sort

Introduction to Bubble Sort

        Bubble Sort (Bubble Sort) is also a simple and intuitive sorting algorithm. It iteratively walks through the array to be sorted, comparing two elements at a time, and swapping them if they are in the wrong order. The work of visiting the sequence is repeated until there is no need to exchange, that is to say, the sequence has been sorted. The name of this algorithm comes from the fact that the smaller elements will slowly "float" to the top of the array through exchange.

        As one of the simplest sorting algorithms, bubble sorting gives me the same feeling as Abandon appeared in the word book. It is the first on the first page every time, so it is the most familiar. Bubble sorting also has an optimization algorithm, which is to set a flag. When elements are not exchanged during a sequence traversal, it proves that the sequence is already in order. But this improvement is for improving performance.

        If the initial state of the record sequence is "positive sequence", the bubble sorting process only needs to be sorted once, and only n-1 comparisons need to be performed during the sorting process, and the record is not moved; on the contrary, if the initial state of the record sequence For "reverse order", n(n-1)/2 comparisons and record moves are required. Therefore, the total time complexity of bubble sorting is O(n*n).


Algorithm steps and principles

        Compare adjacent elements. If the first is bigger than the second, 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.


Bubble sort animation demo


Questions and Answers about Bubble Sort

when is the fastest

        When the input data is already in positive order.

when is the slowest

        When the input data is in reverse order.


Python code implementation 

def bubbleSort(arr):
    for i in range(1, len(arr)):
        for j in range(0, len(arr)-i):
            if arr[j] > arr[j+1]:
                arr[j], arr[j + 1] = arr[j + 1], arr[j]
    return arr

Summary - Deficiencies and improvement methods of bubble sorting method

        First, in the sorting process, after the final sorting is performed, although the data has been completely sorted, the program cannot judge whether the sorting is complete. In order to solve this problem, a flag bit can be set and its initial value can be set to non- 0, indicating that the sorted table is an unordered table, set the flag value to 0 before each sorting, and modify the flag to be non-0 during data exchange. At the beginning of a new round of sorting, check this flag, if this flag is 0, it means that no data exchange has been done last time, then end the sorting; otherwise, sort;

        Second, when the sorted data is large, the sorting time will be significantly prolonged. Improvement method: quick sort: specific method: select a record arbitrarily (usually the first record), compare its keyword with the keywords of all records, and put all records with keywords smaller than it in front of it, All records larger than it are stored behind it, so that after one sorting, all records can be divided into two parts by the dividing point where the record is located, and then the two parts are quickly sorted until the sorting is completed.

Improvement of bubble sort by local bubble sort algorithm:

        In bubble sorting, there may be no data exchange in one scan, or there may be one or more data exchanges. In the traditional bubble sorting algorithm and some improved algorithms in recent years, only one scan is recorded. The exchanged information does not process the location information where the data exchange takes place. In order to make full use of this information, local bubble sorting can be performed on each reverse sequence data pair in a global scan, which is called local bubble sorting.

        The local bubble sort has the same time complexity as the bubble sort algorithm, and the number of comparisons and moves of the required keys are exactly the same in the case of forward order and reverse order. Since the number of data movements of partial bubble sort and bubble sort is always the same, and the number of key comparisons required for partial bubble sort is often less than that of bubble sort, this means that local bubble sort is likely to be in the average number of comparisons Bubble sorting has been improved on the above, when the advantage of fewer comparisons is not enough to offset the additional overhead brought by its program complexity, and when the amount of data is large, the time performance of local bubble sorting is significantly better than that of bubble sorting. Bubble sort.

Guess you like

Origin blog.csdn.net/leyang0910/article/details/130469773