Ejemplo n.º 1
0
def check_sums(S, p, r, x):
    ans = list()
    merge_sort(S, p, r)
    for i in range(p, r+1):
        y = recursive_binary_search(S, x-S[i], i+1, r)
        if y != -1:
            ans.append([S[i], S[y]])
    return ans
def check_sums(S, p, r, x):
    ans = list()
    merge_sort(S, p, r)
    # 滑窗法 给定初始锚 和大了移动右边 和小了移动左边 因此为O(n)
    i = p
    j = r
    while i < j:
        a = S[i] + S[j]
        if a == x:
            if [S[i], S[j]] not in ans:  # 输出不同解
                ans.append([S[i], S[j]])
            i = i + 1
        elif a > x:
            j = j - 1
        else:
            i = i + 1
    return ans
Ejemplo n.º 3
0
def count_inversions(lst):
    """
    counts inversions in LST. Inversion: is when the natural relative order
    or 2 elements does not match their relative order in LST
    Also outputs new ordered list
    """
    if len(lst) < 2:
        return lst, 0
    else:
        split_index = round(len(lst)/2)
        left = lst[:split_index]
        right = lst[split_index:]
        left_inv = count_inversions(left)[1]
        right_inv = count_inversions(right)[1]
        output_lst, split_inv = merge_count(merge_sort(left), merge_sort(right))
        total = left_inv + right_inv + split_inv
    return output_lst, total
Ejemplo n.º 4
0
def StartAlgorithm():
    global data
    if not data: return

    if (algMenu.get() == 'Intserion Sort'):
        insertionSort(data, drawData, speedScale.get())

    if (algMenu.get() == 'Bubble Sort'):
        bubble_sort(data, drawData, speedScale.get())

    if (algMenu.get() == 'Selection Sort'):
        selectionSort(data, drawData, speedScale.get())

    if (algMenu.get() == 'Merge Sort'):
        merge_sort(data, drawData, speedScale.get())

    if (algMenu.get() == 'Quick Sort'):
        quick_sort(data, 0, len(data) - 1, drawData, speedScale.get())
        drawData(data, ["green" for x in range(len(data))])
Ejemplo n.º 5
0
    for Number in x:
        Number
    Sub = Number.split(" ")
    x.close()
    return Sub


WriteNumbersinFile(RequaredRanges)
#fes = HatMenFile()
#es  = HatMenFile()

MG = HatMenFile()
AK = MG
for x in range(0, 1):
    WriteResult = open("Result.txt", "a")

    WriteResult.write("QuickSort\n")
    WriteResult.write(str(RequaredRanges[x]) + '\n')
    t3 = time.time()

    QuickSort(AK)
    t4 = time.time() - t3
    WriteResult.write(str(t4) + '\n')

    WriteResult.write("MergeSort\n")
    WriteResult.write(str(RequaredRanges[x]) + '\n')
    T4 = time.time()
    merge_sort(MG)
    T7 = time.time() - T4
    WriteResult.write(str(T7) + '\n')
Ejemplo n.º 6
0
randomlist2 = randomlist.copy()
randomlist3 = randomlist.copy()
randomlist4 = randomlist.copy()
randomlist5 = randomlist.copy()
randomlist6 = randomlist.copy()
#print(randomlist)

# start_time = time.time()
# selection_sort(randomlist1)
# print("selection--- %s seconds ---" % (time.time() - start_time))
#
# start_time = time.time()
# bubble_sort(randomlist2)
# print("bubble--- %s seconds ---" % (time.time() - start_time))
#
#
# start_time = time.time()
# insertion_sort(randomlist3)
# print("insertion--- %s seconds ---" % (time.time() - start_time))

start_time = time.time()
merge_sort(randomlist4)
print("merge--- %s seconds ---" % (time.time() - start_time))

start_time = time.time()
heap_sort(randomlist5)
print("heap--- %s seconds ---" % (time.time() - start_time))

start_time = time.time()
quick_sort(randomlist6)
print("quick--- %s seconds ---" % (time.time() - start_time))
Ejemplo n.º 7
0
#Doing all from 1 To 100000
for x in range(0,6):
    Result_File = open("Result.txt","a")
    #QuickSort
    Result_File.write("QuickSort\n")
    Result_File.write(str(RequaredRanges[x])+'\n')
    TForQuick1 =time()
    QuickSort(SmallHat[x])
    TForQuick2 = time()-TForQuick1
    Result_File.write(str(TForQuick2)+"\n")

    Result_File.write("MergeSort\n")
    Result_File.write(str(RequaredRanges[x])+'\n')
    TForQuick1 =time()
    merge_sort(SmallHat[x])
    TForQuick2 = time()-TForQuick1
    Result_File.write(str(TForQuick2)+"\n")

    Result_File.write("SelectionSort\n")
    Result_File.write(str(RequaredRanges[x])+'\n')
    TForQuick1 =time()
    selection_sort(SmallHat[x])
    TForQuick2 = time()-TForQuick1
    Result_File.write(str(TForQuick2)+"\n")

    Result_File.write("InsertionSort\n")
    Result_File.write(str(RequaredRanges[x])+'\n')
    TForQuick1 =time()
    Insertion_sort(SmallHat[x])
    TForQuick2 = time()-TForQuick1
from Merge_Sort import merge_sort
from Selection_Sort import selection_sort
from Quick_Sort import quick_sort



arr = [1,2,3,4]
bar = [8, 100 ,1,-3,11,1,0]
car = [0,-3,1,-2]
foo = [123,91,-19, 1,1,2,1,-54,1909,-51293,192,3,-4]
goo = ['a','m','c','d', 'z', 'f','g', 'e']

tests = [arr, bar, car, foo, goo, foo+car+arr+bar]

for l in tests:
    bub = bubble_sort(l)
    ins = insertion_sort(l)
    mer = merge_sort(l)
    sel = selection_sort(l)
    qui = quick_sort(l)

    if bub == ins and bub == mer and bub == sel and bub == qui:
        print("\nAll same for: " + str(l) )
    else:
        print("\nError..Input: " + str(l) +
              "\nBubble sort: " + str(bub) +
              "\nInsertion sort: " + str(ins) +
              "\nMerge sort: " + str(mer) +
              "\nSelection sort: " + str(sel) +
              "\nQuick sort: " + str(qui))
    print("bub " + str(bub))