python: Enum

发布时间 2023-07-02 23:04:16作者: ®Geovin Du Dream Park™

 

# encoding: utf-8
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2023.1 python 11
# Datetime  : 2023/7/2 21:18
# User      : geovindu
# Product   : PyCharm
# Project   : pythonStudyDemo
# File      : CheckSort.py
# explain   : 学习

from enum import Enum

class CheckSort(Enum):
    """
    选择排序方式

    """

    Asc=1,
    """
    降序
    """
    Desc=2
    """
    升序
    """





# encoding: utf-8
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2023.1 python 11
# Datetime  : 2023/7/2 20:25
# User      : geovindu
# Product   : PyCharm
# Project   : pythonStudyDemo
# File      : TenSortAlgotrthms.py
# explain   : 学习 十种排序

from enum import Enum
import sortingalgorithms.CheckSort
import sortingalgorithms.duplicateChecking
class TenSortAlgotrthms(object):
    """

    """
    def bubbleSort(self,arr):
        """
        1。冒泡排序方法  从小至大 升序
        :param arr 整数数组  如 arr = [64, 34, 25, 12, 22, 11, 90]
        :param checkSort选择排序方式 Desc 降序 Asc 升序
        :return:
        """
        n = len(arr)
        swapped = False
        for i in range(n - 1):
            for j in range(0, n - i - 1):
                 if arr[j] > arr[j + 1]:
                    swapped = True
                    arr[j], arr[j + 1] = arr[j + 1], arr[j]

            if not swapped:
               return

    def bubbleSort(self,arr,checkSort):
        """
        1。冒泡排序方法  从小至大 升序
        :param arr 整数数组  如 arr = [64, 34, 25, 12, 22, 11, 90]
        :param checkSort选择排序方式 Desc 降序 Asc 升序
        :return:
        """
        n = len(arr)

        swapped = False
        if(checkSort.Asc==sortingalgorithms.CheckSort.CheckSort.Desc):
            for i in range(n - 1):
                for j in range(0, n - i - 1):
                    if arr[j] > arr[j + 1]:
                        swapped = True
                        arr[j], arr[j + 1] = arr[j + 1], arr[j]

                if not swapped:
                    return

        else:
            for i in range(n - 1):
                for j in range(0, n - i - 1):
                    if arr[j] < arr[j + 1]:
                        swapped = True
                        arr[j], arr[j + 1] = arr[j + 1], arr[j]

                if not swapped:
                    return



    def bubbleSortIndex(self,arr,checkSort,eqLeter:int):
        """
        1。冒泡排序方法  从小至大 升序
        :param arr 整数数组  如 arr = [64, 34, 25, 12, 22, 11, 90]
        :param checkSort选择排序方式 Desc 降序 Asc 升序
        :param eqLeter 找查指定的数字的下标
        :return:返回下标 元组
        """
        n = len(arr)
        fin = sortingalgorithms.duplicateChecking.DuplicateChecking()
        index = fin.findindex(arr, eqLeter)
        #print("index",index,eqLeter)
        swapped = False
        if(checkSort.Asc==sortingalgorithms.CheckSort.CheckSort.Desc):
            for i in range(n - 1):
                for j in range(0, n - i - 1):
                    if arr[j] > arr[j + 1]:
                        swapped = True
                        arr[j], arr[j + 1] = arr[j + 1], arr[j]

                if not swapped:
                    return index


        else:
            for i in range(n - 1):
                for j in range(0, n - i - 1):
                    if arr[j] < arr[j + 1]:
                        swapped = True
                        arr[j], arr[j + 1] = arr[j + 1], arr[j]

                if not swapped:
                    return index

  

调用:

    # 排序
    arrs= [64, 34, 25, 2, 22, 11, 90,88,34]
    fin=sortingalgorithms.duplicateChecking.DuplicateChecking()
    qc=34
    inde=fin.findindex(arrs,qc)
    print(f"顺序查询数组中{qc}查到下标是:",inde)
    for ud in inde:
        print(ud+1)

    sort=sortingalgorithms.TenSortAlgotrthms.TenSortAlgotrthms()
    sort.bubbleSort(arrs,sortingalgorithms.CheckSort.CheckSort.Desc)
    print("冒泡排序:")
    for i in range(len(arrs)):
        print("% d" % arrs[i], end=" ")
    q=5
    duindex=sort.bubbleSortIndex(arrs, sortingalgorithms.CheckSort.CheckSort.Desc,q)
    print(f"\n{q}下标是:",duindex)