bubble_sort

  • 2022-12-14
  • 浏览 (542)

bubble_sort.py 源码

import random
from typing import List

# 冒泡排序


def bubbleSort1(data: List[int]):
    length = len(data)
    if length <= 1:
        return

    for i in range(length):
        for j in range(length - i - 1):
            if data[j] > data[j + 1]:
                data[j], data[j + 1] = data[j + 1], data[j]


# 另一种写法
def bubbleSort2(data: List[int]):
    length = len(data)
    if length <= 1:
        return

    for i in range(length):
        swap = False
        for j in range(length - i - 1):
            if data[j] > data[j + 1]:
                data[j], data[j + 1] = data[j + 1], data[j]
                swap = True
        if not swap:
            break

你可能感兴趣的文章

insertion_sort

merge_sort

quick_sort

0  赞