当前位置:网站首页>bubble sort

bubble sort

2022-06-26 03:48:00 Just call me ah Jie

Bubble sort is first from 1 Bit heel 2 A bit more , Move forward if you are older , The maximum value will be placed at the end of the loop once , Cycle twice , The second largest value will be placed in the penultimate number , And so on , Loop the entire list length n You can get a sequence table .

class Solution:
    def search(self,list_num):
        n =len(list_num)
        for j in range(n): #  loop n Time , Repeat the maximum value to the back ,  Until each is sorted 
            for i in range(n-1): # This loop can get a maximum value after the list 
                if list_num[i] > list_num[i+1]:
                    list_num[i] ,list_num[i+1] = list_num[i+1] ,list_num[i]
        return list_num

原网站

版权声明
本文为[Just call me ah Jie]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180546015971.html