冒泡排序的基本思想:
冒泡排序是依次走访两个相邻的数,进行比较(除最后一个数),直到排序完成 。
例:
arr = [49,38,04,97,76,13,27,49,55,65],交换
arr = [38,49,04,97,76,13,27,49,55,65],交换
立即学习“Python免费学习笔记(深入)”;
arr = [38,04,49,97,76,13,27,49,55,65],依次走访直到排序完成
代码:
def bubble_sort(lists): #冒泡排序 count = len(lists) while count > 0: for i in range(count - 1): #最后一位数不进行比较 key = lists[i+1] if lists[i] > key: lists[i], lists[i+1] = key, lists[i] count -= 1 return lists
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号