如果语句对于某些输入行为不当

时间:2019-07-08 05:17:18

标签: python algorithm

我正在尝试在python中实现MergeSort。但是由于某种原因,我得到了意外的行为,如果第二眼可以看一下并确定我可能做错了,我将不胜感激。代码如下:

# Implementation of Merge-Sort algorithm using Divide and Conquer paradigm

# Reading the unsorted array from a file into a list
#filename = 'test.txt'
#
#array = [line.rstrip('\n') for line in open(filename)]
#
#print(array)
array = ['1', '3', '5', '7', '4', '6', '8', '2', '19', '15', '12', '11', '16', '17', '10', '20'] 

#function for dividing and calling merge function
def Mergesort(array):
  n = len(array)
  if(n<2):
    return

  mid = n/2
  left = []
  right = []

  for i in range(mid):
    number = array[i]
    left.append(number)  

  for i in range(mid,n):
    number = array[i]
    right.append(number)

  Mergesort(left)
  Mergesort(right)

  Merge(left,right,array)

def Merge(left, right, array):
    i = 0
    j = 0
    #k = 0

    print'Merging!'
    print(array)
    print(left)
    print(right)


    for k in range(len(array)):
        if (i<len(left) and j<len(right)):
          print('value of i is ',i, 'value of j is', j)
          print('Entering first while statement')
          if(left[i] < right[j]):
            print('Left index value is ', left[i], 'Right index value is ', right[j])
            array[k] = left[i]
            print('Value is being entered at index ', k, 'and the value is', array[k])
            print('End of iteration ', i, j)
            i = i+1
          else:
            print('Entering the else of first while statement')
            print('Left index value is ', left[i], 'Right index value is ', right[j])
            array[k] = right[j]
            print('Value is being entered at index ', k, 'and the value is', array[k])
            print('End of iteration ', i, j)
            j = j+1
      elif(i<len(left)):
        print('Entering second while statement')
        print('Left index value is ', left[i])
        array[k] = left[i]
        print('Value is being entered at index ', k, 'and the value is', array[k])
        i = i+1
      else:
        print('Entering third while statement')
        print('Right index value is ', right[j])
        array[k] = right[j]
        print('Value is being entered at index ', k, 'and the value is', array[k])
        j = j+1
      print('Merging done! Sorted array is ',array)

#calling Mergesort
Mergesort(array)
for i in array:
  print i,

P.S:有很多用于调试的打印语句。我将它们留在了这里,以便可以看到我在执行流程中查找相应值的确切位置

现在,运行上述程序后,我得到了几组数组的预期行为,例如:

Merging!
['1', '3']
['1']
['3']
('value of i is ', 0, 'value of j is', 0)
Entering first while statement
('Left index value is ', '1', 'Right index value is ', '3')
('Value is being entered at index ', 0, 'and the value is', '1')
('End of iteration ', 0, 0)
Entering third while statement
('Right index value is ', '3')
('Value is being entered at index ', 1, 'and the value is', '3')  
('Merging done! Sorted array is ', ['1', '3'])
Merging!  
['5', '7']
['5']
['7']
('value of i is ', 0, 'value of j is', 0)  
Entering first while statement
('Left index value is ', '5', 'Right index value is ', '7')
('Value is being entered at index ', 0, 'and the value is', '5')
('End of iteration ', 0, 0)
Entering third while statement
('Right index value is ', '7')
('Value is being entered at index ', 1, 'and the value is', '7')
('Merging done! Sorted array is ', ['5', '7'])
Merging!
['1', '3', '5', '7']
['1', '3']
['5', '7']
('value of i is ', 0, 'value of j is', 0)
Entering first while statement
('Left index value is ', '1', 'Right index value is ', '5')
('Value is being entered at index ', 0, 'and the value is', '1')
('End of iteration ', 0, 0)
('value of i is ', 1, 'value of j is', 0)
Entering first while statement
('Left index value is ', '3', 'Right index value is ', '5')
('Value is being entered at index ', 1, 'and the value is', '3')
('End of iteration ', 1, 0)
Entering third while statement
('Right index value is ', '5')
('Value is being entered at index ', 2, 'and the value is', '5')
Entering third while statement
('Right index value is ', '7')
('Value is being entered at index ', 3, 'and the value is', '7')
('Merging done! Sorted array is ', ['1', '3', '5', '7'])

在执行的后期,if的行为不是所希望的:

Merging!
['1', '3', '5', '7', '4', '6', '8', '2', '19', '15', '12', '11', '16', '17', '10', '20']
['1', '2', '3', '4', '5', '6', '7', '8']
['10', '11', '12', '15', '16', '17', '19', '20']
('value of i is ', 0, 'value of j is', 0)
Entering first while statement
('Left index value is ', '1', 'Right index value is ', '10')
('Value is being entered at index ', 0, 'and the value is', '1')
('End of iteration ', 0, 0)
('value of i is ', 1, 'value of j is', 0)
Entering first while statement
Entering the else of first while statement
('Left index value is ', '2', 'Right index value is ', '10')
('Value is being entered at index ', 1, 'and the value is', '10')
('End of iteration ', 1, 0)
('value of i is ', 1, 'value of j is', 1)
Entering first while statement
Entering the else of first while statement
('Left index value is ', '2', 'Right index value is ', '11')
('Value is being entered at index ', 2, 'and the value is', '11')
('End of iteration ', 1, 1)
('value of i is ', 1, 'value of j is', 2)

我不明白为什么会这样。从外观上看,我似乎错过了一些显而易见的东西,但还无法弄清楚。

如果您能对此发表想法,我将不胜感激。

1 个答案:

答案 0 :(得分:2)

问题在于您不是在对数字列表进行排序,而是对字符串列表进行排序。如果您进行字符串比较,则'10'会按字母顺序排在'2'之前。

您需要做的就是使用数字定义列表。

更改

array = ['1', '3', '5', '7', '4', '6', '8', '2', '19', '15', '12', '11', '16', '17', '10', '20'] 

array = [1, 3, 5, 7, 4, 6, 8, 2, 19, 15, 12, 11, 16, 17, 10, 20]