冒泡、选择、插入、快速排序和二分查找python实现

文章目录

      • 1 排序
        • 1.1 冒泡排序
        • 1.2 选择排序
        • 1.3 插入排序
        • 1.4 快速排序
      • 2 二分查找

1 排序

1.1 冒泡排序

def MaoPaoSort1(alist):
   
    alist = list(alist)
    length = len(alist)
    sorted_list = []   #开辟了新的内存空间
    
    if length == 1:
        sorted_list.append(alist[0])
    else:
        while length > 1: 
            for i in range(1,length):
                if alist[i] < alist[i - 1]:
                    alist[i],alist[i-1] = alist[i - 1],alist[i]
                    
            sorted_list.append(alist[-1])
            alist = alist[0:length - 1]
            length = len(alist)
            
    return sorted_list


def MaoPaoSort2(alist):
    alist = list(alist)
    
    for j in range(len(alist) - 1):
        for i in range(len(alist) - 1 - j):
            if alist[i] > alist[i + 1]:
                alist[i], alist[i + 1] = alist[i + 1], alist[i]
        
    return alist  
            
a = MaoPaoSort1([1,2,3,4,6,1,9,20,33,67,54])
a = MaoPaoSort2([1,2,3,4,6,1,9,20,33,67,54])

'''
output:
[1, 1, 2, 3, 4, 6, 9, 20, 33, 54, 67]
'''

1.2 选择排序

#选择排序:将乱序中的最大值找出,跟最后一个元素交换位置
def SelectedSort(alist):
    alist = list(alist)
    
    for j in range(len(alist) - 1):
    
        max_index = 0
        for i in range(1, len(alist) - j):
            if alist[i] > alist[max_index]:
                max_index = i
        
        alist[max_index],alist[len(alist) - 1 - j] = alist[len(alist) - 1 - j], alist[max_index]
        
    return alist  
    
a = SelectedSort([1,2,3,4,6,1,9,20,33,67,54])    


'''
output:
[1, 1, 2, 3, 4, 6, 9, 20, 33, 54, 67]
'''

1.3 插入排序

def InsertionSort(alist):
    
    for i in range(1, len(alist)):
        while i > 1:
            if alist[i-1] > alist[i]:
                alist[i - 1] ,alist[i] = alist[i], alist[i-1]
                i = i - 1
            else:
                break
    return alist

a = InsertionSort([1,2,3,4,6,1,9,20,33,67,54]) 

'''
output:
[1, 1, 2, 3, 4, 6, 9, 20, 33, 54, 67]
'''

1.4 快速排序

#给定一个基准值,<= 基准值的在左边,大于基准值的在右边
def QuickSort(alist):
    
    alist = list(alist)
    
    if len(alist) <= 1:
        return alist
    
    else:    
        middle = len(alist)//2    
        middle_value = alist[middle]
        
        alist_rest = alist[0:middle] + alist[middle + 1 :]
        
        left = [value for value in alist_rest if value <= middle_value]
        right = [value for value in alist_rest if value > middle_value]
    
        return QuickSort(left) + [middle_value] +  QuickSort(right) 

alist = [1,8,4,4,6,1,9,20,33,67,54,100,70,60,40,58]
a = QuickSort(alist)    
'''
output:
[1, 1, 2, 3, 4, 6, 9, 20, 33, 54, 67]
'''

2 二分查找

#二分查找,查找指定某一个数值在某个顺序数列中的位置
def HalfFind(alist,item):
    alist = list(alist)
    middle = len(alist)//2
    left = 0
    right = len(alist) - 1 
    
    while True:
        if item < alist[0] or item > alist[-1]:
            print(str(item),' no in the alist' )
            break
        if item == alist[middle]:
            return middle
            break
        elif item < alist[middle]:
            right = middle - 1
            middle = (left + right)//2
        else:
            left = middle + 1
            middle = (left + right)//2 
            
alist = [0,1,2,3,4,5,6,7,8,9,10]
print(HalfFind(alist,0))

'''
0
'''

二分查找适用于已经排好序的数组。

参考连接:

十大经典排序算法(动画显示)

你可能感兴趣的:(python,算法)