gzc / CLRS

:notebook:Solutions to Introduction to Algorithms
MIT License
9.43k stars 2.75k forks source link

CLRS/C02-Getting-Started/exercise_code/merge-sort.py #211

Open amymayadi opened 6 years ago

amymayadi commented 6 years ago
L = items[p:q+1]         <----why use q+1
R = items[q+1:r+1]      <----why use r+1

the problem asks not to use sentinels. Why use q+1 and r+1? Will there be more items than initial times? q+1 -p +1, r+1 -(q+1) +1 are more than q-p+1, r-(q+1)+1.

ghostbody commented 6 years ago

Because they are python array slices. For instance:


arr = [1, 2, 3, 4, 5, 6]

arr[0:3] # [1, 2, 3]
arr[3:6] # [4, 5, 6]