Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MergeSort data input
#1
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to change input data set for character recognition program? plumberpy 5 1,656 Apr-20-2022, 07:46 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020