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

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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

  

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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)

  

调用:

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

  

 

posted @   ®Geovin Du Dream Park™  阅读(7)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2022-09-21 CSharp: Bridge Pattern
2022-09-21 Java: Immutable Pattern
2016-09-21 csharp: Oracle Stored Procedure DAL using ODP.NET
2014-09-21 Create a soft keyboard
2012-09-21 SQL 生成公曆和農曆對照數據續--创建萬年曆查找各種周期性節日數據
2010-09-21 Csharp,Javascript 获取显示器的大小的几种方式
2009-09-21 谷歌地图API学习
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示