An easy option would be to iterate through the array in reverse
for (int k = numItems; k > i; k--){ items[k]=items[k-1]; }
Option 2:
If you want to keep your method intact, you can also use a temporary variable in different ways
before your for loop initializes temp so that
double temp = items[i];
and then in a loop, you can use temp to store the value [k + 1] in temp instead of saving the value [k].
items [k+1] = temp; temp = items [k+1]; items[k+1] = items[k];
you should also keep track of your borders so that k + 1 does not pass by the last element of the array. You can use something like numItems-1 with validation before to make sure the array is not empty.
Jasmeet oberai
source share