zoukankan      html  css  js  c++  java
  • 数据结构day1:排序

    1,冒泡排序算法的python实现

    def bubble_sort(alist):
        pass
        count = len(alist)-1
        for index in range(count,0,-1):
            for new_index in range(index):
                if alist[new_index] > alist[new_index + 1]:
                    alist[new_index],alist[new_index+1] = alist[new_index+1],alist[new_index]
    
        return alist
    
    li = [9,3,5,7,11,34,88,45,32]
    print(bubble_sort(li))

    2,快速排序算法的python实现

    def quick_sort(alist,left,right):
        if left >= right:
            return alist
        low = left
        high = right
        key = alist[low]
    
        while low < high:
            # 先从右向左
            if low < high and alist[high] >= key:
                high -= 1
            alist[low] = alist[high]
    
            # 再从左到右
            if low < high and alist[low] <= key:
                low += 1
            alist[high] = alist[low]
    
        alist[high] = key
    
        quick_sort(alist,left,low-1)
        quick_sort(alist,low+1,right)
        return alist  # 不要少了这一句
    
    li = [9,3,5,7,11,34,88,45,32]
    print(quick_sort(li,0,8))
  • 相关阅读:
    jQuery(2)
    jQuery(1)
    underscore.js
    面向对象复习
    1.14函数复习
    面向对象(3)继承
    10.18
    1017
    js笔记二
    js笔记一
  • 原文地址:https://www.cnblogs.com/lisa-blog/p/10246476.html
Copyright © 2011-2022 走看看