我被要求用python编写一个程序来检查排序算法是否稳定。需要帮忙吗

2024-09-28 05:28:44 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我决定通过创建另一个与要排序的输入数组(arraynumber)长度相同的数组(比如arraycount)来解决这个问题。为了让程序真正检查排序函数的稳定性,我决定使用重复出现的整数数组的测试用例,例如arraynumber = {10,8,7,8,2,2,2,1,6}。然后我使用一个函数来计算数组中出现的数字,并将它们存储在arraycount[]中,结果将是arraycount={1,1,1,2,1,2,3,1,1}。 自然地,在按递增顺序排序之后,它们变成arraynumber={1,2,2,2,6,7,8,8,10},数组计数的结果决定了它是否稳定。因此,如果它稳定arraycount={1,1,2,3,1,1,1,2,1},否则就不一样了。 这是我一直在写的代码。感谢您的帮助和反馈。主要功能和输入还没有采取。我将采取另一个数组副本的原始arraycount这将是比较后,检查是否保留原来的位置。如果我犯了什么小错误,我很抱歉。非常感谢。你知道吗

def merge(arr1, l, m, r,arr2): 
    n1 = m - l + 1
    n2 = r- m 

    # create temp arrays 
    L = [0] * (n1) 
    R = [0] * (n2) 

    # Copy data to temp arrays L[] and R[] 
    for i in range(0 , n1): 
        L[i] = arr1[l + i] 

    for j in range(0 , n2): 
        R[j] = arr1[m + 1 + j] 

    # Merge the temp arrays back into arr[l..r] 
    i = 0     # Initial index of first subarray 
    j = 0     # Initial index of second subarray 
    k = l     # Initial index of merged subarray 

    while i < n1 and j < n2 : 
        if L[i] <= R[j]: 
            arr1[k] = L[i]
            arr2[k]=L[i]
            i += 1
        else: 
            arr1[k] = R[j]
            arr2[k]=R[j]
            j += 1
        k += 1

    # Copy the remaining elements of L[], if there 
    # are any 
    while i < n1: 
        arr1[k] = L[i]
        arr2[k]=L[i]
        i += 1
        k += 1

    # Copy the remaining elements of R[], if there 
    # are any 
    while j < n2: 
        arr1[k] = R[j]
        arr2[k]=R[j]
        j += 1
        k += 1

# l is for left index and r is right index of the 
# sub-array of arr to be sorted 
def mergeSort(arr1,l,r,arr2): 
    if l < r: 

        # Same as (l+r)/2, but avoids overflow for 
        # large l and h 
        m = (l+(r-1))/2

        # Sort first and second halves 
        mergeSort(arr1, l, m,arr2) 
        mergeSort(arr1, m+1, r,arr2) 
        merge(arr1, l, m, r,arr2)


# This function takes last element as pivot, places 
# the pivot element at its correct position in sorted 
# array, and places all smaller (smaller than pivot) 
# to left of pivot and all greater elements to right 
# of pivot 
def partition(arr1,low,high,arr2): 
    i = ( low-1 )         # index of smaller element 
    pivot = arr1[high]     # pivot 

    for j in range(low , high): 

        # If current element is smaller than or 
        # equal to pivot 
        if   arr1[j] <= pivot: 

            # increment index of smaller element 
            i = i+1 
            arr1[i],arr1[j] = arr1[j],arr1[i]
            arr2[i],arr2[j]=arr2[j],arr2[i]

    arr1[i+1],arr1[high] = arr1[high],arr1[i+1]
    arr2[i+1],arr2[high]=arr2[high],arr2[i+1]
    return ( i+1 ) 

# The main function that implements QuickSort 
# arr1[] --> Array to be sorted,
# arr2[] --> Second Array of the count to be sorted along with first array
# low  --> Starting index, 
# high  --> Ending index 

# Function to do Quick sort 
def quickSort(arr1,low,high,arr2): 
    if low < high: 

        # pi is partitioning index, arr[p] is now 
        # at right place 
        pi = partition(arr1,low,high,arr2) 

        # Separately sort elements before 
        # partition and after partition 
        quickSort(arr1, low, pi-1,arr2) 
        quickSort(arr1, pi+1, high,arr2)


def countArray(arr1,arr2):
i=0,j=1,k=0
    while i<range(len(arr1[])):

        while j<range(len(arr1[])):
            if arr1[i]==arr1[j]:
                arr2[i]=k+1
            j=j+1
            k=0
        i=i+1

arraylist=[]        
arraycount=[]
def isStable(arr1,arr2):
    k=0
    while i < range(len(arr1[])):
        if arr[i]==arr2[i]:
           k=k+1
        i=i+1

    if k==len(arr2):
        print("Stable sort")
    else:
        print("Unstable sort")


Tags: andofthetoindexifdefrange

热门问题