常用排序算法脚本实现

冒泡排序

 

Public Sub BubbleSort(ByRef foldArray() As String)
    Dim iOuter As Long
    Dim iInner As Long
    Dim iLBound As Long
    Dim iUBound As Long
    Dim iTemp As Long

    iLBound = LBound(lngArray)
    iUBound = UBound(lngArray)

    For iOuter = iLBound To iUBound - 1
        For iInner = iLBound To iUBound - iOuter - 1
            If lngArray(iInner) < foldArray(iInner + 1) Then
                iTemp = lngArray(iInner)
                foldArray(iInner) = foldArray(iInner + 1)
                foldArray(iInner + 1) = iTemp
            End If
        Next iInner
    Next iOuter
End Sub

 

def bubble_sort(sort_list):
    iter_len = len(sort_list)
    if iter_len < 2:
        return sort_list
    for i in range(iter_len-1):
        for j in range(iter_len-i-1):
            if sort_list[j] > sort_list[j+1]:
                sort_list[j], sort_list[j+1] = sort_list[j+1], sort_list[j]
    return sort_list
 

选择排序

 

Public Sub SelectionSort(ByRef lngArray() As Long)
    Dim iOuter As Long
    Dim iInner As Long
    Dim iLBound As Long
    Dim iUBound As Long
    Dim iTemp As Long
    Dim iLocation As Long

    iLBound = LBound(lngArray)
    iUBound = UBound(lngArray)

    For iOuter = iLBound To iUBound - 1
        iLocation = iOuter
        For iInner = iOuter To iUBound
            If lngArray(iInner) > lngArray(iLocation) Then iLocation = iInner
        Next iInner
        
        If iLocation <> iOuter Then
            iTemp = lngArray(iLocation)
            lngArray(iLocation) = lngArray(iOuter)
            lngArray(iOuter) = iTemp
        End If
    Next iOuter
End Sub

 

def selection_sort(sort_list):
    iter_len = len(sort_list)
    if iter_len < 2:
        return sort_list
    for i in range(iter_len-1):
        smallest = sort_list[i]
        location = i
        for j in range(i, iter_len):
            if sort_list[j] < smallest:
                smallest = sort_list[j]
                location = j
        if i != location:
            sort_list[i], sort_list[location] = sort_list[location], sort_list[i]
    return sort_list
 

插入排序

 

Public Sub InsertSort(ByRef lngArray() As Long)
    Dim iOuter As Long
    Dim iInner As Long
    Dim iLBound As Long
    Dim iUBound As Long
    Dim iTemp As Long
    
    iLBound = LBound(lngArray)
    iUBound = UBound(lngArray)
    
    For iOuter = iLBound + 1 To iUBound
        iTemp = lngArray(iOuter)
        
        For iInner = iOuter - 1 To iLBound Step -1
            If lngArray(iInner) >= iTemp Then Exit For
            lngArray(iInner + 1) = lngArray(iInner)
        Next iInner
        
        lngArray(iInner + 1) = iTemp
    Next iOuter
End Sub

 

def insertion_sort(sort_list):
    iter_len = len(sort_list)
    if iter_len < 2:
        return sort_list
    for i in range(1, iter_len):
        key = sort_list[i]
        j = i - 1
        while j>=0 and sort_list[j]>key:
            sort_list[j+1] = sort_list[j]
            j -= 1
        sort_list[j+1] = key
    return sort_list
 

 

 

 

 

 

 

 

 

你可能感兴趣的:(排序算法)