zoukankan      html  css  js  c++  java
  • python实现排序算法

    算法设计与分析的作业,使用python实现,简单记录一下

    冒泡排序 BubbleSort

    #从小到大排序
    def BubbleSort(waitsortlist):
        for i in range(0,len(waitsortlist)-1):
            for j in range(0,len(waitsortlist)-1-i):
                if waitsortlist[j+1]<waitsortlist[j]:
                    waitsortlist[j+1],waitsortlist[j]=waitsortlist[j],waitsortlist[j+1]
    

    选择排序 SelectionSort

    def SelectionSort(waitsortlist):
        for i in range(0,len(waitsortlist)-1):
            min=i
            for j in range(i+1,len(waitsortlist)):
                if waitsortlist[j]<waitsortlist[min]:
                    min=j
            waitsortlist[i],waitsortlist[min]=waitsortlist[min],waitsortlist[i]
    

    验证上面两个算法正确性:

    def main():
        waitsortlist=[2,1,6,5,8,9,4]
        SelectionSort(waitsortlist)
        # BubbleSort(waitsortlist)
        print(waitsortlist)
    

    快速排序 QuickSort

    def QuickSort(waitsortlist,left,right):
        if left>right:
            return
        left_index,right_index=left,right
        temp=waitsortlist[left]
        while left_index!=right_index:
            while left_index<right_index and waitsortlist[right_index]>=temp:
                right_index-=1
            while left_index<right_index and waitsortlist[left_index]<=temp:
                left_index+=1
            if left_index<right_index:
                waitsortlist[left_index],waitsortlist[right_index]=waitsortlist[right_index],waitsortlist[left_index]
        waitsortlist[left],waitsortlist[left_index]=waitsortlist[left_index],waitsortlist[left]
        QuickSort(waitsortlist,left,left_index-1)
        QuickSort(waitsortlist,left_index+1,right)
    

    归并排序 MergeSort

    def merge(waitsortlist,left,might,right):
        left_index,right_index=left,might+1
        temp=[]
        while left_index<=might and right_index<=right:
            if waitsortlist[left_index]<waitsortlist[right_index]:
                temp.append(waitsortlist[left_index])
                left_index+=1
            else:
                temp.append(waitsortlist[right_index])
                right_index+=1
        while left_index<=might:
            temp.append(waitsortlist[left_index])
            left_index+=1
        while right_index<=right:
            temp.append(waitsortlist[right_index])
            right_index+=1
        t=0
        while left<=right:
            waitsortlist[left]=temp[t]
            left+=1
            t+=1
    
    
    #分 治
    def mergeSort(waitsortlist,left,right):
        if left<right:
            might=int((left+right-1)/2)
            mergeSort(waitsortlist,left,might)
            mergeSort(waitsortlist,might+1,right)
            merge(waitsortlist,left,might,right)
    

    验证上面两个算法正确性:

    def main():
        waitsortlist = [2, 1, 6, 5, 8, 9, 4]
        QuickSort(waitsortlist,0,len(waitsortlist)-1)
        print(waitsortlist)
        waitsortlist = [2, 1, 6, 5, 8, 9, 4]
        mergeSort(waitsortlist, 0, len(waitsortlist) - 1)
        print(waitsortlist)
        return
    

    Over

  • 相关阅读:
    vue开发中v-for在Eslint的规则检查下出现:Elements in iteration expect to have 'v-bind:key' directives
    一步步构造自己的vue2.0+webpack环境
    函数的扩展
    数值的扩展
    DB2数据库代码页和实例代码页的区别(解决DB2乱码问题)
    【翻译】探究Ext JS 5和Sencha Touch的布局系统
    【翻译】培训提示:解决常见编码问题的简单技巧
    【翻译】对于Ext JS 5,你准备好了吗?
    Git工程迁移方法总结(命令行)
    Ext JS 5初探(三)
  • 原文地址:https://www.cnblogs.com/Cl0ud/p/14030606.html
Copyright © 2011-2022 走看看