Python Forum

Full Version: MergeSort data input
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey,
i wanntet to make an MergeSort where i get my data from externel file like an .txt, when i import my files i get some strange results [9. 9. 9. 9.] my input data is [12, 44, 11, 9] i think it is bec. of the numpy import but iam not sure.

Thanks for your help.



import numpy as np
x = np.genfromtxt("data/123.txt", delimiter=',')


def mergeSort(alist):
    print("Splitting ",alist)
    if len(alist)>1:
        mid = len(alist)//2
        lefthalf = alist[:mid]
        righthalf = alist[mid:]

        mergeSort(lefthalf)
        mergeSort(righthalf)

        i=0
        j=0
        k=0
        while i < len(lefthalf) and j < len(righthalf):
            if lefthalf[i] < righthalf[j]:
                alist[k]=lefthalf[i]
                i=i+1
            else:
                alist[k]=righthalf[j]
                j=j+1
            k=k+1

        while i < len(lefthalf):
            alist[k]=lefthalf[i]
            i=i+1
            k=k+1

        while j < len(righthalf):
            alist[k]=righthalf[j]
            j=j+1
            k=k+1
    print("Merging ",alist)

alist = x
mergeSort(alist)
print(alist)