zoukankan      html  css  js  c++  java
  • 冒泡,选择,插入,快排,归并,计数

    1、数据结构和算法

      1.1、严魏敏  数据结构预算法

      1.2、大话数据结构和算法

    2、网络

    3、计算机的组成原理

    4、操作系统

    常见时间复杂度:效率

    O(1)<O(logn)<O(n)<O(nlogn)<O(n2)<O(n2logn)<O(n3)

    ###########################
    # 冒泡排序
    def bubbleSort(li):
        for i in range(len(li)-1): 
            flag = False
            for j in range(len(li)-i-1): 
                if li[j+1] < li[j]:
                    li[j+1],li[j] = li[j],li[j+1]
                    flag = True
            if not flag:
                return
    l = [2,6,4,9,1,8,7]
    bubbleSort(l)
    print(l)
    ###########################
    冒泡排序
    ###########################
    # 选择排序
    def selectSort(li):
        for i in range(len(li)-1):
            minLoc = i
            for j in range(i+1,len(li)):
                if li[j] < li[minLoc]:
                    li[minLoc],li[j] = li[j],li[minLoc]
    l = [2,6,4,9,1,8,7]
    selectSort(l)
    print(l)
    ###########################
    选择排序
    ###########################
    # 插入排序
    def insertSort(li):
        for i in range(1,len(li)):
            tmp = li[i]
            j =i - 1
            while j >= 0 and li[j] > tmp:
                li[j+1] = li[j]
                j = j - 1
            li[j+1] = tmp
    l = [2,6,4,9,1,8,7]
    insertSort(l)
    print(l)
    ###########################
    插入排序
    ###########################
    # 快速排序
    def partition(li,left,right):
        tmp = li[left]
        while left < right:
            while left < right and tmp <= li[right]:
                right = right -1
            li[left] = li[right]
    
            while left < right and tmp >= li[left]:
                left = left + 1
            li[right] = li[left]
    
        li[left] = tmp
        return left
    
    def quickSort(li,left,right):
        if left < right:
            mid = partition(li,left,right)
            quickSort(li,left,mid-1)
            quickSort(li,mid+1,right)
    li = [10,4,6,3,8,2,5,7]
    quickSort(li,0,len(li)-1)
    print(li)
    ###########################
    快速排序
    ###########################
    # 归并排序
    def merge(li,low,mid,high):
        i = low
        j = mid + 1
        ltmp = []
        while i <= mid and j <= high:
            if li[i] >= li[j]:
                ltmp.append(li[j])
                j = j + 1
            else:
                ltmp.append(li[i])
                i = i + 1
        while i <= mid:
            ltmp.append(li[i])
            i = i + 1
        while j <= high:
            ltmp.append(li[j])
            j = j + 1
        li[low:high+1] = ltmp
    
    def mergeSort(li,low,high):
        if low < high:
            mid = (low + high) // 2
            mergeSort(li,low,mid)
            mergeSort(li,mid+1,high)
            print('分解后的元素:',li[low:high+1])
            merge(li,low,mid,high)
            print('合并后的元素:',li[low:high+1])
    
    li = [10,4,6,3,8,2,5,7]
    mergeSort(li,0,len(li)-1)
    ###########################
    归并排序
    现在有一个列表,列表中的数范围都在0到100之间,列表长度大约为100万。设计算法在O(n)时间复杂度内将列表进行排序。
    ###########################
    # 计数排序
    def countSort(li):
        count = [0 for i in range(11)]
        for x in li:
            count[x] += 1
        li.clear()
    
        for index,val in enumerate(count):
            for i in range(val):
                li.append(index)
    
    li = [10,4,6,3,8,2,5,7]
    countSort(li)
    print(li)
    ###########################
    计数排序
  • 相关阅读:
    Educational Codeforces Round 64 (Rated for Div. 2)题解
    Codeforces Round #555 (Div. 3) F. Maximum Balanced Circle
    莫队算法总结
    cobalt strike使用笔记
    CMD命令行下载文件
    CTF线下赛AWD模式下的生存技巧
    python中multiprocessing模块
    密码重置
    python中的argparse模块
    python中BeautifulSoup模块
  • 原文地址:https://www.cnblogs.com/xujinjin18/p/9965663.html
Copyright © 2011-2022 走看看