Bubble sort python optimized version

       1. Bubble sorting is the most basic sorting method. It is done using two for loops. The inner loop compares the sizes of two adjacent elements. After one loop, find the largest element and put it at the end. How many waits are in the outer loop? Arrange the elements and loop how many times, so that the two-level loop is completed and the disordered list is sorted. The first loop is 8 largest and placed at the end, the second 6 largest is placed at the end, and the third 5 largest is placed at the end. Until the sorting is finished

Insert picture description here

       code show as below:
def bubble_sort_v1(array):
    #判断传过来的参数是否为数组
    if isinstance(array,list):
        for i in range(len(array)-1):
            for j in range(len(array)-i-1):
                if array[j] > array[j+1]:
                    array[j],array[j+1] = array[j+1],array[j]
    else:
        print("参数为数组")

list1 = [234,5,3,23,56,666,888]
bubble_sort_v1(list1)
print(list1)

结果:
[3, 5, 23, 56, 234, 666, 888]
       2. The above is the most basic version of bubble sorting. Is there anything that can be optimized in this version? If there is a situation like this, you only need to change the position of the last two elements to sort them, and the outer loop will sort them once, but the above bubble sorting code still needs to be traversed 5 times, so the efficiency is not Is there any way to solve this problem?

Insert picture description here

       The code is as follows: set a flag, and set the flag to False every time there is an element exchange position in the inner loop. If there is no exchange position in the inner loop, it proves that all the elements are in order and jump out of the big loop.
def bubble_sort_v1(array):
    #判断传过来的参数是否为数组
    if isinstance(array,list):
        for i in range(len(array)-1):
            flag = True
            for j in range(len(array)-i-1):
                if array[j] > array[j+1]:
                    array[j],array[j+1] = array[j+1],array[j]
                    flag = False
            if flag:
                break
            
    else:
        print("参数为数组")

list1 = [234,5,3,23,56,666,888]
bubble_sort_v1(list1)
print(list1)

结果:
[3, 5, 23, 56, 234, 666, 888]
       3. There is another situation, 7, 8, 9, 10 are already in order, you only need to arrange the front in order, the inner loop will put 6 in front of 7 for the third time, but still have to compare 7 and 8, 8 and 9, knowing that the inner loop is over, how to optimize this situation?

Insert picture description here

The code is as follows: add two variables
sort_border: the number of inner loops
last_exchange_index: Index of the exchange position of the last loop element in the inner layer
Put 6 in front of 7, last_exchange_index = 3, so that the number of inner loops will be reduced each time, solving the above problems
def bubble_sort_v1(array):
    #判断传过来的参数是否为数组
    if isinstance(array,list):
        #内层循环边界
        sort_border = len(array) - 1
        #内层最后循环元素交换位置下标
        last_exchange_index = 0
        for i in range(len(array)-1):
            flag = True
            for j in range(sort_border):
                if array[j] > array[j+1]:
                    array[j],array[j+1] = array[j+1],array[j]
                    flag = False
                    last_exchange_index = j
            sort_border = last_exchange_index
            if flag:
                break

    else:
        print("参数为数组")

list1 = [234,5,3,23,56,666,888]
bubble_sort_v1(list1)
print(list1)

结果:
[3, 5, 23, 56, 234, 666, 888]
       The above is the optimized version of bubble sort. If it is helpful to you, please like it.

Guess you like

Origin blog.csdn.net/weixin_43697214/article/details/113940875