#Insertion sort in reverse not working (not sure why)

6 messages · Page 1 of 1 (latest)

keen garnet
#

Hi there, could I get some help with what's wrong with my insertion sort reversed?

dreamy baneBOT
#

This post has been reserved for your question.

Hey @keen garnet! Please use /close or the Close Post button 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.

keen garnet
#
   
 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

flint linden
#

why is j initialized to i+1? If you start at length-1 aren’t you supposed to insert the current - 1?