#Insertion sort in reverse not working (not sure why)
6 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @keen garnet! Please use
/closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
public static int[] sort_reverse(int[] numbers) {
for (int i = numbers.length - 2; i >= 0; i--) {
int key = numbers[i];
int j = i + 1;
while ((j <= numbers.length - 1) && (numbers[j] > key)) {
numbers[j - 1] = numbers[j];
j = j + 1;
}
numbers[j - 1] = numbers[key];
}
return numbers;
}
@grim wave heres my code
why is j initialized to i+1? If you start at length-1 aren’t you supposed to insert the current - 1?