Bubble sort · Ordenamiento burbuja
Put the smallest time first
- Three race times sit in an array:
14,9,11. The smallest should come first. - Compare each pair of neighbours. When the left one is larger, swap them.
- That single comparison does not finish the list. The next pair is compared after it.
Coloca el tiempo más pequeño primero
- Tres tiempos de carrera están en un array:
14,9,11. El menor debe ir primero. - Compara cada par de vecinos. Cuando el de la izquierda es mayor, intercámbialos.
- Esa única comparación no termina la lista. Se compara el siguiente par después.
DECLARE Score : ARRAY[1:3] OF INTEGER
DECLARE J, Temp : INTEGER
Score[1] ← 14
Score[2] ← 9
Score[3] ← 11
FOR J ← 1 TO 2
IF Score[J] > Score[J + 1]
THEN
Temp ← Score[J]
Score[J] ← Score[J + 1]
Score[J + 1] ← Temp
ENDIF
NEXT J
OUTPUT Score[1]
OUTPUT Score[2]
OUTPUT Score[3]
Repeat the pass
- One pass of the inner loop bubbles the largest value it sees toward the end.
- The outer loop repeats the pass. For four values, three passes are enough.
- This is the syllabus's bubble sort. A linear search, which you wrote earlier, only looks. A sort rearranges.
Repite el recorrido
- Un recorrido del bucle interno hace burbujear el valor más grande hacia el final.
- El bucle externo repite el recorrido. Para cuatro valores, tres recorridos son suficientes.
- Este es el bubble sort del programa de estudios. Una búsqueda lineal, que escribiste antes, solo busca. Un ordenamiento reorganiza.
DECLARE Score : ARRAY[1:4] OF INTEGER
DECLARE I, J, Temp : INTEGER
Score[1] ← 4
Score[2] ← 1
Score[3] ← 3
Score[4] ← 2
FOR I ← 1 TO 3
FOR J ← 1 TO 3
IF Score[J] > Score[J + 1]
THEN
Temp ← Score[J]
Score[J] ← Score[J + 1]
Score[J + 1] ← Temp
ENDIF
NEXT J
NEXT I
OUTPUT Score[1]
OUTPUT Score[2]
OUTPUT Score[3]
OUTPUT Score[4]
Watch out
- Swap with a
Tempvariable.Score[J] ← Score[J + 1]alone deletes the left value. - The inner
NEXTnamesJ. The outerNEXTnamesI.
Ten cuidado
- Intercambia con una variable
Temp. SoloScore[J] ← Score[J + 1]elimina el valor de la izquierda. - El
NEXTinterno se llamaJ. ElNEXTexterno se llamaI.
Common mistakes
- The test is "left is greater", so the smaller value moves left. The other way sorts into reverse order.
- Arrays in this course count from 1. The last pair is the cell just before the end, and the last cell.
Errores comunes
- La condición es "izquierda es mayor", así que el valor menor se mueve a la izquierda. Al revés, ordena en orden inverso.
- Los arrays en este curso cuentan desde 1. El último par es la celda justo antes del final y la última celda.
Now you try
- The first task is one pass. The second is the full sort. Output each value on its own line.
- Press Check answer to test it.
Ahora tú intentas
- La primera tarea es un solo recorrido. La segunda es el ordenamiento completo. Imprime cada valor en su propia línea.
- Presiona Check answer para probarlo.
The array Score holds · contiene 14, 9, 11. Make one · uno pass that swaps a neighbour when the left value is larger. Output the three values, one per line (9, then 11, then 14). · El array Score contiene 14, 9, 11. Realice una pasada que intercambie un vecino cuando el valor izquierdo sea mayor. Output los tres valores, uno por línea (9, luego 11, luego 14).
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
The array Score holds · contiene 4, 1, 3, 2. Bubble-sort it into ascending order and output the four values, one per line. · Ordene el array Score con 4, 1, 3, 2 en orden ascendente mediante ordenamiento burbuja y output los cuatro valores, uno por línea.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.