Final (I hope):
newArr = [].concat(
arr.slice(0,left),
arr.slice(left,right+1).sort(),
arr.slice(right+1,arr.length)
)
taking right
inclusively,
assuming left is not greater than right,
assuming the array is not empty,
etc.
Previous Edit:
I've read the comments.
Mostly correct.
The problem I oversaw was that the sort requested is between left and right inclusively, whereas slice
's first argument is inclusive and second argument is exclusive.
I see, now, the last part should be an negative value, the difference between left and the array's length.
But, I'm not going to try to resolve that...
My original "answer":
I suggest you split the array into 3 sub-arrays using slice
, sort the middle, and then put them back together using concat
, such as so:
newArr=[].concat(arr.slice(0,left),arr.slice(left,right+1).sort(),arr.slice(left+right-1))
May I suggest you get more familiar with slice
and concat
by searching the web?