Search code examples
pythonrecursiondivide-and-conquer

Why is python list showing slicing abnormality?


I am trying to partition the list element recursively (like in divide and conquer) and consequently printing the sliced elements but suddenly sees unexpected abnormality(at line 6 and onwards in Output).

def Mergesort(a, l, r):
    if(l<r):
      mid = (r+l+1) // 2
      print(a)
      Mergesort(a[l : mid], l, mid-1)
      Mergesort(a[mid : r+1], mid, r)

a = [8, 3, -2, 6, 7, 4, 1, 2, -1, 0, 9, 12, 11, 5] 
Mergesort(a, 0, len(a)-1)

Output:

[8, 3, -2, 6, 7, 4, 1, 2, -1, 0, 9, 12, 11, 5]
[8, 3, -2, 6, 7, 4, 1]
[8, 3, -2]
[3, -2]
[6, 7, 4, 1]
[1]
[]
[2, -1, 0, 9, 12, 11, 5]
[]
[]
[]
[]
[]

Solution

  • I found the solution. Actually, while passing sliced list in function, index start from zero.