Skip to content
ExamplesEspañolWorkspace →

Linear Search

Finds a name in a list, ignoring upper and lower case.

Level
Intermediate
Topic:
Arrays
Concepts:
Input & output
Arrays
Mientras
Text
Open in Codoritmo

Run, change and debug this example in your browser.

What you'll learn

A Mientras loop walks the array and stops as soon as it finds the name. Mayusculas converts both texts so they compare equal however they were typed.

Code

linear-search.psc · 27 lines
Algoritmo BusquedaLineal    Definir nombres, buscado Como Caracter    Definir i, posicion Como Entero    Dimension nombres[6]    nombres[1] <- "Ana"    nombres[2] <- "Bruno"    nombres[3] <- "Carla"    nombres[4] <- "Diego"    nombres[5] <- "Luis"    nombres[6] <- "Sofía"    Escribir "¿A quién buscas?"    Leer buscado    posicion <- 0    i <- 1    // Se detiene en cuanto lo encuentra    Mientras i <= 6 Y posicion = 0 Hacer        Si Mayusculas(nombres[i]) = Mayusculas(buscado) Entonces            posicion <- i        FinSi        i <- i + 1    FinMientras    Si posicion > 0 Entonces        Escribir nombres[posicion], " está en la posición ", posicion, "."    SiNo        Escribir buscado, " no está en la lista."    FinSiFinAlgoritmo

Flowchart

Each block in the flowchart matches one instruction in the code.

Algoritmo BusquedaLinealSíNoSíNoSíNoAlgoritmo BusquedaLinealAlgoritmo BusquedaLinealDefinir nombres, buscado Como CaracterDefinir nombres, buscado ComoCaracterDefinir i, posicion Como EnteroDefinir i, posicion ComoEnteroDimension nombres[6]Dimension nombres[6]nombres[1] <- "Ana"nombres[1] <- "Ana"nombres[2] <- "Bruno"nombres[2] <- "Bruno"nombres[3] <- "Carla"nombres[3] <- "Carla"nombres[4] <- "Diego"nombres[4] <- "Diego"nombres[5] <- "Luis"nombres[5] <- "Luis"nombres[6] <- "Sofía"nombres[6] <- "Sofía"Escribir "¿A quién buscas?"Escribir "¿A quién buscas?"Leer buscadoLeer buscadoposicion <- 0posicion <- 0i <- 1i <- 1Mientras i <= 6 Y posicion = 0 Haceri <= 6 Y posicion =0Si Mayusculas(nombres[i]) = Mayusculas(buscado) EntoncesMayusculas(nombres[i]) =Mayusculas(buscado)posicion <- iposicion <- ii <- i + 1i <- i + 1Si posicion > 0 Entoncesposicion > 0Escribir nombres[posicion], " está en la posición ", posicion, "."Escribir nombres[posicion], "está en la posición ",posicion, "."Escribir buscado, " no está en la lista."Escribir buscado, " no está enla lista."FinAlgoritmoFinAlgoritmo

Sample run

This is what the program shows when it runs. The answers typed by the user are highlighted.

¿A quién buscas?User answer: luisLuis está en la posición 5.

Complexity

How much work and memory it needs as the amount of data (n) grows.

Complexity
CaseCost
BestO(1)
AverageO(n)
WorstO(n)
MemoryO(1)

Challenge

If the name isn't there, offer to add it to the end of the list.

Show hint

You'll need a bigger array from Dimension and a variable counting how many names are stored.

Open in Codoritmo
  • Roman Numerals

    Converts a number from 1 to 3999 into Roman numerals.

    Advanced
  • Hangman

    Guess a secret word letter by letter, with six mistakes allowed.

    Advanced
  • Shopping List

    Adds items to a list that grows until you type FIN.

    Advanced