Answer the question
In order to leave comments, you need to log in
Why do we need a variable j?
A sorting algorithm similar to sorting by inserts, but unlike the latter, a series of exchanges takes place before inserting to the right place. The algorithm finds the first place where two neighboring elements are in the wrong order
and swaps them. check position before rearranged elements.
public static void GnomeSort(int[] array)
{
int i = 1;
int j = 2;
while (i < array.Length)
{
if (array[i - 1] <= array[i])
{
i = j;
j++;
}
else
{
int temp = array[i - 1];
array[i - 1] = array[i];
array[i] = temp;
i -= 1;
if (i == 0)
{
i = 1;
j = 2;
}
}
}
}
Answer the question
In order to leave comments, you need to log in
Delete her mentions and see what changes, as I see she is not needed here at all
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question