python: Bubble Sort

 

ubuntu 升级至python 3.11

sudo add-apt-repository ppa:deadsnakes/ppa

sudo apt update

sudo apt install python3.11

python3.11 --version

 root@ubuntu:/home/pycharm/pycharm-2023.2.1/bin# sh pycharm.sh

 

# encoding: utf-8
# 版权所有 2023 涂聚文有限公司
# 许可信息查看:
# 描述:
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2023.1 python 311
# Datetime  : 2023/9/21 21:55
# User      : geovindu
# Product   : PyCharm
# Project   : EssentialAlgorithms
# File      : SortingAlgorithms.py
# explain   : 学习

import tkinter as tk
from tkinter import ttk
import itertools
import math
import sys
import os


class SortingAlgorithms(object):
    """
    排序算法
    """


    def BubbleSort(array:list):
        """
        1。Bubble Sort冒泡排序法
        :return:
        """
        # loop to access each array element
        for i in range(len(array)):

            # loop to compare array elements
            for j in range(0, len(array) - i - 1):

                # compare two adjacent elements
                # change > to < to sort in descending order
                if array[j] > array[j + 1]:
                    # swapping elements if elements
                    # are not in the intended order
                    temp = array[j]
                    array[j] = array[j + 1]
                    array[j + 1] = temp

    def BubbleSort2(array:list):
        """
        1。Bubble Sort冒泡排序法
        :return:
        """

        # loop through each element of array
        for i in range(len(array)):

            # keep track of swapping
            swapped = False

            # loop to compare array elements
            for j in range(0, len(array) - i - 1):

                # compare two adjacent elements
                # change > to < to sort in descending order
                if array[j] > array[j + 1]:
                    # swapping occurs if elements
                    # are not in the intended order
                    temp = array[j]
                    array[j] = array[j + 1]
                    array[j + 1] = temp

                    swapped = True

            # no swapping means the array is already sorted
            # so no need for further comparison
            if not swapped:
                break

  

 

# encoding: utf-8
# 版权所有 2023 涂聚文有限公司
# 许可信息查看:
# 描述:
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2023.1 python 311
# Datetime  : 2023/9/21 22:00
# User      : geovindu
# Product   : PyCharm
# Project   : EssentialAlgorithms
# File      : SortingExample.py
# explain   : 学习


import ChapterOne.SortingAlgorithms


class Example(object):
    """"
    实例
    """

    def Bubble(self):
        data = [-2, 45, 0, 11, -9]
        ChapterOne.SortingAlgorithms.SortingAlgorithms.BubbleSort(data)
        print('冒泡排序法 Sorted Array in Ascending Order:')
        print(data)

  

调用:

    exm=BLL.SortingExample.Example()
    exm.Bubble()

  

 

posted @ 2023-09-21 22:44  ®Geovin Du Dream Park™  阅读(11)  评论(0)    收藏  举报