Linear search and bubble sort · Búsqueda lineal y ordenamiento burbuja
Find one mark, then order the list
- Syllabus topic 10.2 asks for two algorithms on an array: a linear search and a bubble sort.
- A linear search looks at every cell. Remember the index when the value matches. If it never matches, the index stays 0.
- Searching does not move the values. Sorting does.
Encuentra un valor, luego ordena la lista
- El tema 10.2 del programa de estudios solicita dos algoritmos sobre una matriz: una búsqueda lineal y una ordenación burbuja.
- Una búsqueda lineal revisa cada celda. Recuerda el índice cuando el valor coincide. Si nunca coincide, el índice permanece en 0.
- La búsqueda no mueve los valores. La ordenación sí lo hace.
DECLARE A : ARRAY[1:4] OF INTEGER
DECLARE I, Pos : INTEGER
A[1] ← 4
A[2] ← 9
A[3] ← 1
A[4] ← 7
Pos ← 0
FOR I ← 1 TO 4
IF A[I] = 9 THEN
Pos ← I
ENDIF
NEXT I
OUTPUT Pos
Bubble the largest to the end
- Compare each pair of neighbours. When the left one is larger, swap them through a temporary variable.
- One comparison is not the whole sort. Repeat the pass until every pair has been handled.
- For four values, an outer loop of three passes and an inner loop across the pairs is enough.
Sube el mayor al final
- Compara cada par de vecinos. Cuando el de la izquierda es mayor, intercámbialos mediante una variable temporal.
- Una comparación no constituye una ordenación completa. Repite el recorrido hasta que se hayan manejado todos los pares.
- Para cuatro valores, un bucle externo de tres recorridos y un bucle interno a través de los pares son suficientes.
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]
Common mistakes
- A swap needs three assignments. Copying the right cell over the left one loses the left value.
THENstays on theIFline in this guide. The innerNEXTnamesJ, the outer namesI.
Errores comunes
- Un intercambio requiere tres asignaciones. Copiar la celda derecha sobre la izquierda hace perder el valor de la izquierda.
THENpermanece en la línea delIFen esta guía. ElNEXTinterno nombraJ, el externo nombraI.
Now you try
- First report where
9sits. Then sort four values into ascending order, one per line.
Ahora practica
- Primero indica dónde se encuentra el
9. Luego ordena cuatro valores en orden ascendente, uno por línea.
The array A holds · contiene 4, 9, 1, 7. Set Pos to the index of 9 (it is 2) and output Pos. If you never find it, Pos stays 0. · El array A contiene 4, 9, 1, 7. Asigne Pos al índice de 9 (es 2) y muestre Pos. Si nunca lo encuentra, Pos permanece en 0.
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. · El array Score contiene 4, 1, 3, 2. Ordénelo mediante ordenamiento burbuja en orden ascendente y muestre los cuatro valores, uno por línea.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.