简体   繁体   中英

java bubble sort issue

I am learning Java from the past one month. I am having difficulties to understand the line

 for(int i = 0;i<list.length-1;i++){

Can anybody explain me in layman lang. I am a slow learner. I understand for loop but this thing i am not able to understand

Int[]list = {5͵7͵54͵34͵87͵44};

boolean swap = true;
int temp;
while(swap){
   swap = false;
   for(int i = 0;i<list.length-1;i++){
        if(list[i] > list[i+1]){
            temp = list[i];
            list[i] = list[i+1];
            list[i+1] = temp;                   
            swap = true;
        }
    }
}

Let's consider an array

 0 1 2 3 4 5 6 7 8 9   <-- array indexes
 _ _ _ _ _ _ _ _ _ _
| | | | | | | | | | |

The line for(int i = 0;i<list.length-1;i++){ tells you you are going to iterate from 0 to lenght - 2 . In my example the length is 10, so you're going to iterate from 0 to 8 .

The reason for it is to avoid OutOfBoundsException in this line list[i] > list[i+1] where you compare i -th index with i+1 -th index. In the last iteration it's going to be i = 8 and i + 1 = 9 (which is the last index of my array).

your code is going through all list.entries in this for loop.

if your list.entry at position i is bigger than list.entry at position i+1 you save your list.entry at position i in temp then you are saving the bigger list.entry at position i+1 in your actual list at position i. your list[i+1] is getting the smaller value of temp (which you saved in the beginning) after that you set swap = true... probably in order to know that you swapped i and i + 1

if swap is not true your while loop is done, because your list is sorted

the loop at line 5 is for comparing all possible pairs of numbers. The while loop is for selecting first member and the for loop is for selecting second member.

You have an array of size 6 ie from index 0-5. Your for loop starts with the index 0 and checks the element at index 0 with index 1. If index 0 is greater than index 1, swap the two values. Similarly, it checks till index 4(i=4) and index 5(i+1) is reached, because this will be the end of the array index ie you will check for the last two elements in the array(since the array index starts from 0). If you go any beyond i=4, you will check for 5 and 6(which does not exist in your array and will throw an OutOfBoundsException).

for(int i = 0;i<list.length-1;i++){

goes through items in list . You are accessing each item with list[i]. (the first item has index 0 (list[0]). i is going to be zero (0) on the beginning of your loop and then it is going to increase by one (i++) each iteration; It will increase until the middle expression

i<list.length-1`) 

fitting i .

So in your case it will go through the items (you have 6 in your list as you can see). The last item has index 5 list[5]. That is why you have -1 in list.length-1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM