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.

btw i now how bubble sort works for ex:
* 2 4 6 1 10 2 0
* 2 4 1 6 2 0 10
* 2 1 4 2 0 6 10
* 1 2 2 0 4 6 10
* 1 2 0 2 4 6 10
* 1 0 2 2 4 6 10
* 0 1 2 2 4 6 10

i just dont know how is the code is working

ah nvm i see it now

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.