python 实现八大排序

### 归并排序
def merge(left, right):
    i = j = 0
    arr = []

    while i < len(left) and j < len(right):
        if left[i] < right[j]:
            arr.append(left[i])
            i += 1
        else:
            arr.append(right[j])
            j += 1

    if i == len(left):
        for h in right[j:]:
            arr.append(h)
    else:
        for k in left[i:]:
            arr.append(k)

    return arr

def mergesort(lists):
    len1 = int(len(lists))
    if len1 == 1:
        return lists
    mid = len1 // 2
    left = mergesort(lists[:mid])
    right = mergesort(lists[mid:])
    return merge(left, right)


# 插入排序
def insertsort(lists):
    len1 = len(lists)
    for i in range(1, len1):
        for j in range(i):
            if lists[i-j] < lists[i-j-1]:
                lists[i-j], lists[i-j-1] = lists[i-j-1], lists[i-j]
            else:
                break
    return lists

# 选择排序
def selectionsort(lists):
    len1 = len(lists)
    for i in range(len1):
        index = i
        for j in range(i+1, len1):
            if lists[index] > lists[j]:
                index = j
        lists[i], lists[index] = lists[index], lists[i]
    return lists

# 快速排序
def quick_sort(alist, start, end):
    if start >= end:
        return 
    low = start
    high = end
    mid = alist[low]
    
    while low < high:
        while low < high and mid < alist[high]:
            # 从右边开始找,如果元素小于基准,则把这个元素放到左边
            high -= 1
        alist[low] = alist[high]
        
        while low < high and mid > alist[low]:
            # 从左边开始找,如果元素大于基准,则把元素放到右边
            low += 1
        alist[high] = alist[low]
    
    # 循环退出,low==high,把基准元素放到这个位置
    alist[low] = mid
    
    # 递归调用,重新排列左边的和右边的序列
    quick_sort(alist, start, low-1)
    quick_sort(alist, low+1, end)

# 希尔排序
def shellsort(lists):
    len1 = len(lists)
    gap = len1 // 2
    while gap > 0:
        for i in range(gap, len1):
            j = i
            while j >= gap and lists[j-gap] > lists[j]:
                lists[j-gap], lists[j] = lists[j], lists[j-gap]
                j -= gap
        gap = gap // 2
    return lists

def countSort(lists, max): 
    # 得有范围,适合量大 值小的排序
    output = [0 for i in range(max)] 
  
    count = [0 for i in range(max)] 
  
    ans = ["" for _ in lists] 
  
    for i in lists: 
        count[i] += 1    
       
  
    for i in range(max): 
        count[i] += count[i-1] 
  
    for i in range(len(lists)): 
        output[count[ord(lists[i])]-1] = lists[i] 
        count[lists[i]] -= 1
  
    for i in range(len(lists)): 
        ans[i] = output[i] 
    return ans  

# ord
#  功能描述:以一个字符(长度为1的字符串)作为参数,返回对应的ASCll数值,或者Unicode值,
#  如果所给的Unicode字符超出了你的Python定义范围,则会引发一个TypeError的异常。

#
def countsort(lists):
    n = max(lists) + 1

    count = [0] * n
    output = []

    for i in lists:
        count[i] += 1
    print(count)
    
    for i in range(n):
        while count[i]:
            output.append(i)
            count[i] -= 1
    return output

# 先按身高排序,在按姓名排序
# def Ncountsort(lists1, lists2):
#     n = max(lists1) + 1
#     count = [['0'] for _ in range(n)]
#     output = []

#     for i in range(len(lists1)):
#         count[lists1[i]].append(lists2[i])
#     print(count)

#     for i in range(n):
#         len1 = 1
#         while len1 < len(count[i]):
#             count[i].sort()
#             output.append(count[i][len1])
#             len1 += 1
#     return output


if __name__ == "__main__":
    lists = [2,11,10,6,12,3,4,8,9,5,1,15,13,7,14,123,145,111,100,124,126]
    # liststr = list(map(str, lists)) # 字符排序类型
    liststrs = ['abd', 'abb', 'aa', 'aaa', 'aab', 'bac', 'bacb', 'aba', 'abb']

    lists1 = [167, 177, 180, 167, 167, 177]
    lists2 = ['abb', 'bbb', 'abc', 'aba', 'abc', 'bab']

    #print(mergesort(lists))
    
    # print(insertsort(liststrs))
    
    # print(selectionsort(liststrs))
    
    quick_sort(lists,0,14)
    print(lists)
    
    # quicksort(lists)
    # print(lists)

    # print(shellsort(liststr))

    # print(shellsort(liststrs))

    #print(countsort(lists))

    #print(Ncountsort(lists1, lists2))

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值