Shifting elements in an array In many ordered collections that are implemented based on arrays, a major issue is the cos
Posted: Sun Jul 03, 2022 11:59 am
Shifting elements in an array
In many ordered collections that are implemented based onarrays, a major issue is the cost associated with shifting elementsto the right when adding and to the left when removing.
For example, let's say that we're implementing a list that keepsits elements in natural order using an array using our familiar"left justified" strategy. If the array has capacity 10 and thelist contains the first five odd integers, then the array wouldlook like this:
[1, 3, 5, 7, 9, _, _, _, _, _]
please work to the code below:
/** * Implements shift-right behavior in an array. * */
public class ShiftRight {
/** * Shifts the elements at a[index] througha[a.length - 2] one * position to the right. */ public static void shiftRight(Object[] array, intindex) {
// FILL IN THE BODY OF THISMETHOD
}
}
Adding the value 4 to the list would require elements 5, 7, and 9 to be shifted to the right [1, 3, _1 [1, 3, 4, 5, 7, 9, I 5, 7, 9, Call: Before: to make room for the 4 to be added in natural order: After: Call: Before: After: Call: Before: After: Call: Before: After: _1 I _] You must complete the body of the shiftRight method to implement this behavior. Below are examples of various calls to shiftRight with the array parameter shown before and after the call. 1 _1 -1 _] a shiftRight (a, 2) a = [1, 3, 5, 7, 9, null, null, null, null, null] a = [1, 3, null, 7 7, 9, null, null, null, null] shiftRight (a, 0) a [1, 3, 5, 7, 9, null, null, null, null, null] a = [null, 1, 3, 5, 7, 9, null, null, null, null] shiftRight (a, 4) a = [1, 3, 5, 7, 9, null, null, null, null, null] a = [1, 3, 5, 7, null, 9, null, null, null, null] shiftRight (a, 5) [1, 3, 5, 7, 9, null, null, null, null, null] a = [1, 3, 5, 7, 9, null] null, null, null, null,
In many ordered collections that are implemented based onarrays, a major issue is the cost associated with shifting elementsto the right when adding and to the left when removing.
For example, let's say that we're implementing a list that keepsits elements in natural order using an array using our familiar"left justified" strategy. If the array has capacity 10 and thelist contains the first five odd integers, then the array wouldlook like this:
[1, 3, 5, 7, 9, _, _, _, _, _]
please work to the code below:
/** * Implements shift-right behavior in an array. * */
public class ShiftRight {
/** * Shifts the elements at a[index] througha[a.length - 2] one * position to the right. */ public static void shiftRight(Object[] array, intindex) {
// FILL IN THE BODY OF THISMETHOD
}
}
Adding the value 4 to the list would require elements 5, 7, and 9 to be shifted to the right [1, 3, _1 [1, 3, 4, 5, 7, 9, I 5, 7, 9, Call: Before: to make room for the 4 to be added in natural order: After: Call: Before: After: Call: Before: After: Call: Before: After: _1 I _] You must complete the body of the shiftRight method to implement this behavior. Below are examples of various calls to shiftRight with the array parameter shown before and after the call. 1 _1 -1 _] a shiftRight (a, 2) a = [1, 3, 5, 7, 9, null, null, null, null, null] a = [1, 3, null, 7 7, 9, null, null, null, null] shiftRight (a, 0) a [1, 3, 5, 7, 9, null, null, null, null, null] a = [null, 1, 3, 5, 7, 9, null, null, null, null] shiftRight (a, 4) a = [1, 3, 5, 7, 9, null, null, null, null, null] a = [1, 3, 5, 7, null, 9, null, null, null, null] shiftRight (a, 5) [1, 3, 5, 7, 9, null, null, null, null, null] a = [1, 3, 5, 7, 9, null] null, null, null, null,