冒泡排序
冒泡排序
Aristore把大的元素往后挪,最大的元素向右移到已排序的区域就像浮出水面,也就是 “冒泡”。
实践
1 | def BubbleSort(nums): |
流程图
graph TD Start["Start: Initialize nums as a copy of input"] --> A["Iterate over nums, from 0 to len(nums) - 1"] A --> B["Set flag = False"] B --> C["Iterate over nums from 0 to len(nums) - i - 1"] C --> D{"Is nums[j] > nums[j + 1]?"} D -->|No| E["No swap, continue to next pair"] D -->|Yes| F["Swap nums[j] and nums[j + 1]"] F --> G["Set flag = True"] E --> H["Continue iterating"] G --> H H --> C C --> I{"Is flag False?"} I -->|Yes| J["Exit the loop and return sorted nums"] I -->|No| A J --> F_end["End: Return sorted nums"]
评论
匿名评论隐私政策