In vain you yourself did not write and did not understand the algorithm, but decided to take ready-made code with a lack of knowledge.
To begin with, we will take and sort out the sorting and take an array for example:
words = ["Шарик","Бобик","Барсик","Зорька","Кеша","Арчи","Мурка"]
To walk through it, you can use a for loop:
for i in 0...words.length puts words[i] end
You can compare strings using common logical operations: words[0] > words[1]
Now let's go through the cycle and try to partially sort our data with intermediate output. Since we will use the i+1 construction, in order not to go beyond the array, we change words.length to words.length - 1 .
For clarity, let's slightly change our array.
words = ["Яшка","Шарик","Бобик","Барсик","Зорька","Кеша","Арчи","Мурка"] for i in 0...words.length - 1 if words[i] > words[i+1] then temp = words[i] words[i] = words[i+1] words[i+1] = temp end puts words.join(","),"\n" end
The output will be:
"Шарик","Яшка","Бобик","Барсик","Зорька","Кеша","Арчи","Мурка" "Шарик","Бобик","Яшка","Барсик","Зорька","Кеша","Арчи","Мурка" "Шарик","Бобик","Барсик","Яшка","Зорька","Кеша","Арчи","Мурка" "Шарик","Бобик","Барсик","Зорька","Яшка","Кеша","Арчи","Мурка" "Шарик","Бобик","Барсик","Зорька","Кеша","Яшка","Арчи","Мурка" "Шарик","Бобик","Барсик","Зорька","Кеша","Арчи","Яшка","Мурка" "Шарик","Бобик","Барсик","Зорька","Кеша","Арчи","Мурка","Яшка"
As we see, the smallest element (the letter "I") traveled to the end, while others moved to the beginning. If we make several such operations over an array, then all the elements will be arranged in a sorted row.
for j in 0...words.length for i in 0...words.length - 1 if words[i] > words[i+1] then temp = words[i] words[i] = words[i+1] words[i+1] = temp end puts words.join(","),"\n" end end
In the end, we get a ready-made bubble sorting program.
words = ["Яшка","Шарик","Бобик","Барсик","Зорька","Кеша","Арчи","Мурка"] for j in 0...words.length - 1 for i in 0...words.length - 1 if words[i] > words[i+1] then temp = words[i] words[i] = words[i+1] words[i+1] = temp end end end puts words.join(","),"\n"
But in fact, this algorithm is not perfect. To change the position of elements, we use the following structure:
temp = words[i] words[i] = words[i+1] words[i+1] = temp
Ruby makes it easier:
words[i], words[i + 1] = words[i + 1], words[i]
In addition, IF has several syntax options:
if condition [then] code end
and
code if condition
In the second variant, one operator is written first and then the check, so we change our code to the same one:
for j in 0...words.length - 1 for i in 0...words.length - 1 words[i], words[i + 1] = words[i + 1], words[i] if words[i] > words[i+1] end end
This is all the same algorithm, but recorded in different ways. Now let's deal with the infinite loop that confused you.
swap = false while swap swap = true end
As you can see, from a simple example, we actually have a condition, this is the value of the logical variable swap! And now how it changes.
There are abbreviated transaction records:
a = a + 1 аналогично a += 1 a = a * 1 аналогично a *= 1 a |= true аналогично a = a | true
In this case | is a logical OR operation that returns TRUE if at least one of the operators contains the value TRUE.
This code will always be TRUE if the words are not sorted.
for i in 0...words.length - 1 swap |= words[i] > words[i + 1] end
This means that WHILE will start again if the words are out of order. But if they are in order, the cycle will be interrupted and will no longer be executed. For example, for already sorted words we will go only once, instead of words.length times, than we save ourselves time.
while swap swap = false for i in 0...words.length - 1 swap |= words[i] > words[i + 1] words[i], words[i + 1] = words[i + 1], words[i] if words[i] > words [i + 1] end end
While viewing the sort, you can see that the last word to be sorted is already in the last place, so it does not necessarily pass through the entire length of the array. From here it turns out size , which is constantly decremented by one size -= 1 . This is how the algorithm gets its final form.
words = ["Шарик","Бобик","Барсик","Зорька","Кеша","Арчи","Мурка"] swap = true size = words.length - 1 while swap swap = false for i in 0...size swap |= words[i] > words[i + 1] words[i], words[i + 1] = words[i + 1], words[i] if words[i] > words [i + 1] end size -= 1 end puts words.join(', ')
a, b = b, a- exchange of values.while swapis a condition. Whileswap == truewill be a loop. - pirj