hi, i need help with bubble sort. i am not sure what are variable j and variable sort are for?
public static void bubble_sort(int unsorted[])
{
int i = 0;
int j = 0;
int sort = 0;
while(sort == 0)
{
sort = 1;
j++;
for(i = 0; i < unsorted.length - j; i++)
{
if(unsorted[i] > unsorted[i+1])
{
swap();
sort = 0;
}
}
}
1st question:
is variable sort mean if the list is sorted? 0 for false and 1 for true?
2nd question: variable j
i dont understant why we need '-j' in forloop and j++? why we do "-1" and get rid of j?
if we have a array of
[2 4 6 1 10 2 0]
than statement "unsorted.length - j" means 6 - 1. which means it will loop thorough
[2 4 6 1 10 2] that i understant but than "j++" will be 2 and
now "unsorted.length - j" means 6-2. which is 4. so it only going to loop through 1st four numbers.
[2 4 6 1 10]
thanks.