排序算法 - 交换和比较计数器

时间:2017-03-13 16:59:40

标签: python-3.x quicksort mergesort insertion-sort

我的任务是找到算法中的比较和交换次数。我从互联网上尝试了几种方法,但我不确定它是否正确。如果有人只是改变变量的位置(或者如果一切都正确的话),那就太好了。不需要额外的解释,因为代码是自我解释的。

 def insertionSort(array):
    numberOfComp = 0
    numberOfSwap = 0
    for index in range(1,len(array)):
        curr = array[index]
        position = index
        numberOfComp += 1
        while position > 0 and array[position-1] > curr:

            array[position] = array[position-1]
            position = position - 1
            numberOfSwap += 1
            numberOfComp += 1
        array[position] = curr

    complexityOfInsertion.append(numberOfComp)
    swapsOfInsertion.append(numberOfSwap)
    return array



def quickSort(array):
    global numberOfCompQuick
    global numberOfSwapsQuick

    less = []
    equal = []
    greater = []

    if len(array) > 1:
        pivot = array[int(len(array)/2)]

        for x in array:
            if x < pivot:
                less.append(x)
            if x == pivot:
                equal.append(x)
            if x > pivot:
                greater.append(x)
            numberOfCompQuick += 1
        numberOfSwapsQuick +=1
        # Don't forget to return something!
        return quickSort(less)+equal+quickSort(greater)  #the + operator to join lists

    else:  #when only have one element in array, just return the array.
        return array



def merge(a,b):
    """ Function to merge two arrays """
    global numberOfCompMerge
    global numberOfSwapsMerge
    c = []
    while len(a) != 0 and len(b) != 0:
        numberOfCompMerge += 1
        if a[0] < b[0]:
            c.append(a[0])
            a.remove(a[0])

        else:
            c.append(b[0])
            b.remove(b[0])
            numberOfSwapsMerge += 1
    if len(a) == 0:
        c += b
        numberOfSwapsMerge += 1
    else:
        numberOfSwapsMerge += 1
        c += a
    return c

# Code for merge sort

def mergeSort(array):
    """ Function to sort an array using merge sort algorithm """
    #global numberOfCompMerge
    if len(array) == 0 or len(array) == 1:
        return array
    else:
        middle = int(len(array)/2)
        a = mergeSort(array[:middle])
        b = mergeSort(array[middle:])
        return merge(a,b)

1 个答案:

答案 0 :(得分:-1)

def insertionSort(array):
    numberOfComp = 0
    numberOfSwap = 0
    for index in range(1,len(array)):
        curr = array[index]
        position = index
        if position > 0: numberOfComp += 1
        while position > 0 and array[position-1] > curr:
            array[position] = array[position-1]
            position = position - 1
            numberOfSwap += 1
            if position > 0: numberOfComp += 1
        array[position] = curr
        numberOfSwap += 1

    complexityOfInsertion.append(numberOfComp)
    swapsOfInsertion.append(numberOfSwap)
    return array



def quickSort(array):
    global numberOfCompQuick
    global numberOfSwapsQuick

    less = []
    equal = []
    greater = []

    if len(array) > 1:
        pivot = array[int(len(array)/2)]

        for x in array:
            if x < pivot:
                less.append(x)
            if x == pivot:
                equal.append(x)
            if x > pivot:
                greater.append(x)
            numberOfCompQuick += 3 #can prior code to reduce this, but I'm only fixing the comp and swap counters
        numberOfSwapsQuick += 1
        # Don't forget to return something!
        return quickSort(less)+equal+quickSort(greater)  #the + operator to join lists

    else:  #when only have one element in array, just return the array.
        return array



def merge(a,b):
    """ Function to merge two arrays """
    global numberOfCompMerge
    global numberOfSwapsMerge
    c = []
    while len(a) != 0 and len(b) != 0:
        numberOfCompMerge += 1
        if a[0] < b[0]:
            c.append(a[0])
            a.remove(a[0])
        else:
            c.append(b[0])
            b.remove(b[0])
        numberOfSwapsMerge += 1
    if len(a) == 0:
        c += b
        numberOfSwapsMerge += len(b)
    else:
        numberOfSwapsMerge += len(a)
        c += a
    return c

# Code for merge sort

def mergeSort(array):
    """ Function to sort an array using merge sort algorithm """
    #global numberOfCompMerge
    if len(array) == 0 or len(array) == 1:
        return array
    else:
        middle = int(len(array)/2)
        a = mergeSort(array[:middle])
        b = mergeSort(array[middle:])
        return merge(a,b)

固定。魔鬼的细节。