[Python Code] Python Bubble Sort (sắp xếp nổi bọt)

Python Bubble Sort

Tìm hiểu về thuật toán nổi bọt sử dụng ngôn ngữ lập trình python

Code mẫu:

def bubble_sort(list_data):
    for i in range(len(list_data)):
        is_swapped = False
        for j in range(len(list_data) - i - 1):
            if list_data[j] > list_data[j+1]:
                list_data[j], list_data[j+1] = list_data[j+1], list_data[j]
                is_swapped = True
        
        if is_swapped == False:
            break


def main():
    array = [1,4, 6, 8, 2, 9, 0]
    bubble_sort(array)

    print("Sorted List:")
    for i in range(len(array)):
        print("%d" % array[i], end=" ")

if __name__ == "__main__":
    main()

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.