Standard Methods of Solution


This is a standard algorithm which is used to find element in an unordered list. The list is search sequentially and systematically one element at a time. It can be very time consuming.

OUTPUT “Enter a value to find”
INPUT Number
Found ← FALSE
Index ←1

REPEAT
  IF Number = Mylist[Index] 
          THEN
      Found ← TRUE
  ELSE
      Counter ← Counter + 1
  ENDIF
UNTIL Found =TRUE OR Counter > LENGTH(Mylist) 

IF Found = TRUE 
    THEN
        OUTPUT Number, “ found at position “, Counter
ELSE
    OUTPUT Number, “ not found”
ENDIF

Bubble Sort

This sorts items into order from the smallest to the largest by comparing element and swapping them around. It is very time consuming and not efficient. Below is an example of it.

Mylist ← [5, 9, 4, 2, 6, 7, 1, 2, 4, 3]

FirstElement ← 1
LastElement ← LENGTH(Mylist)

REPEAT
  Swap ← FALSE
  
  For Index ← FirstElement TO LastElement - 1
    IF Mylist[Index] > Mylist[Index + 1] 
      THEN
    Temp ← Mylist[Index]
      Mylist[Index] ← Mylist[Index + 1]
      Mylist[Index + 1] ← Temp
      Swap ← TRUE
    ENDIF
  
  NEXT Index
  LastElement ← LastElement - 1
UNTIL Swap = FALSE OR LastElement = 1

OUTPUT “Your sorted list is:”, Mylist

Last updated