Bubble sort · Ordenamiento burbuja
Sorting puts items in order
- Sorting rearranges a list into order — smallest to largest, say.
- Bubble sort is the standard method named in the syllabus.
- It compares neighbours and swaps any that are the wrong way round.
Ordenar coloca los elementos en orden
- Ordenar reorganiza una lista en un orden determinado — de menor a mayor, por ejemplo.
- Bubble sort (ordenamiento burbuja) es el método estándar mencionado en el programa de estudios.
- Compara elementos adyacentes e intercambia aquellos que están en el orden incorrecto.
Swapping two values
- To sort, you must swap two items.
- Python can swap in one line:
a, b = b, a.
Intercambiar dos valores
- Para ordenar, debes intercambiar dos elementos.
- Python puede realizar el intercambio en una sola línea:
a, b = b, a.
a = 5
b = 9
a, b = b, a
print(a, b) # 9 5
Bubble sort: compare neighbours
- Walk the list comparing each pair of neighbours.
- If the left one is bigger, swap them — the biggest "bubbles" to the end.
- Repeat the passes until the list is in order.
Bubble sort: comparar elementos adyacentes
- Recorre la lista comparando cada par de elementos adyacentes.
- Si el elemento de la izquierda es mayor, se intercambian — el valor más grande "sube" hacia el final como una burbuja.
- Repite los recorridos hasta que la lista esté completamente ordenada.
nums = [5, 1, 4, 2]
for i in range(len(nums) - 1):
for j in range(len(nums) - 1 - i):
if nums[j] > nums[j + 1]:
nums[j], nums[j + 1] = nums[j + 1], nums[j]
print(nums) # [1, 2, 4, 5]
In the exam: bubble sort
- The exam writes bubble sort in pseudocode with nested loops and a swap.
En el examen: bubble sort
- El examen presenta el bubble sort en pseudocódigo con bucles anidados y un intercambio.
FOR I ← 1 TO N - 1
FOR J ← 1 TO N - 1
IF List[J] > List[J + 1]
THEN
Temp ← List[J]
List[J] ← List[J + 1]
List[J + 1] ← Temp
ENDIF
NEXT J
NEXT I
Watch bubble sort work · Ver cómo funciona el ordenamiento burbuja
Compare neighbours and swap any out of order; the biggest bubbles to the end. · Comparar elementos adyacentes e intercambiarlos si están desordenados; mover las burbujas más grandes al final.
Common mistakes
- Each pass moves the largest remaining value to the end.
- If a whole pass makes no swaps, the list is already sorted.
Errores comunes
- Cada recorrido coloca el valor restante más grande al final de la lista.
- Si un recorrido completo no realiza ningún intercambio, la lista ya está ordenada.
Now you try
- Swap two values, then sort a whole list.
- Press Check answer to test your code.
Ahora tú intentas
- Intercambia dos valores y luego ordena toda una lista.
- Presiona Comprobar respuesta para verificar tu código.
Swap the values of a and · y b, so that afterwards a is 9 and · y b is 5. · Intercambia los valores de a y b, de modo que después a sea · mar 9 y b sea · mar 5.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Sort the list nums into · dentro de ascending · ascendente order (smallest first) using a bubble sort. The result should be [1, 2, 4, 5]. · Ordena la lista nums en orden ascendente (de menor a mayor) usando un ordenamiento burbuja. El resultado debe ser [1, 2, 4, 5].
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Write is_sorted(nums) that returns True if the list is already in ascending order, otherwise False. · Escribe is_sorted(nums) que devuelva True si la lista ya está en orden ascendente, de lo contrario False.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.