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()
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
浙公网安备 33010602011771号