Linear search and bubble sort · Busca linear e ordenação bolha
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · Esta página precisa de um navegador recente (com suporte a SharedArrayBuffer). Por favor, atualize Chrome, Edge, Firefox ou Safari para a versão mais recente.
English
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.
Português
Encontre uma marca, então organize a lista
- O tópico do programa 10.2 pede dois algoritmos em uma matriz: uma busca linear e uma ordenação bolha.
- Uma busca linear olha cada célula. Lembre-se do índice quando o valor corresponder. Se nunca corresponder, o índice permanece 0.
- Buscar não move os valores. Ordenar move.
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
English
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.
Português
Ordene o maior para o final
- Compare cada par de vizinhos. Quando o da esquerda for maior, troque-os através de uma variável temporária.
- Uma comparação não é toda a ordenação. Repita a passagem até que todos os pares tenham sido tratados.
- Para quatro valores, um loop externo de três passagens e um loop interno sobre os pares é suficiente.
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]
English
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.
Português
Erros comuns
- Uma troca precisa de três atribuições. Copiar a célula da direita sobre a da esquerda perde o valor da esquerda.
THENfica na linhaIFneste guia. ONEXTinterno nomeiaJ, o externo nomeiaI.
English
Now you try
- First report where
9sits. Then sort four values into ascending order, one per line.
Português
Agora você tenta
- Primeiro relate onde
9está. Em seguida, ordene quatro valores em ordem crescente, um por linha.
The array A holds · contém 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. · O array A contém 4, 9, 1, 7. Defina Pos como o índice de 9 (é 2) e imprima Pos. Se nunca encontrá-lo, Pos permanece 0.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
The array Score holds · contém 4, 1, 3, 2. Bubble-sort it into ascending order and output the four values, one per line. · O array Score contém 4, 1, 3, 2. Ordenação bolha para ordem crescente e imprima os quatro valores, um por linha.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.