Search routines (paper 2) Flashcards

1
Q

what happens in a linear search

A

each item to be searched from will be checked one by one in the list until the item is found or an error is outputted

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what are the two types of search

A
  • linear
  • bubble sort
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the code for a linear search algorithm

A

// perform a linear search of an array
DECLARE AList : ARRAY[1:10] OF INTEGER
AList <- [14, 2, 3 ,11, 1, 9, 5, 8, 10, 6]
OUTPUT “List to be searched:”, AList
Found <- FALSE
Index <- 1
INPUT SearchItem
WHILE Found = FALSE AND Index <= LENGTH(AList) DO
IF AList[Index] = SearchItem
THEN
Found <- TRUE
ELSE
Index <- Index + 1
ENDIF
ENDWHILE
IF Found = TRUE
THEN
OUTPUT SearchItem, “in position”, Index, “of the list”
ELSE
OUTPUT “Item not found”
ENDIF

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what happens in a bubble sort

A

it repeatedly goes through the list to be sortedd, swapping adjacent elements if they are in the wrong order

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what is the pseudocode for a bubble sort

A

//bubble sort with flag set when no swaps are made
AList <-[17, 3, 7, 15, 12, 23, 20]
// get number of items in the array
NumItems <- LENGTH(AList)
Comparisions <- NumItems - 1
SwapMade <- TRUE
WHILE Comparisons > 0 AND SwapMade = TRUE
SwapMade <- FALSE
FOR i <- 1 TO Comparisons
IF AList[i] > AList[i + 1]
THEN
Temp <- AList[i]
AList[i] <- AList[i + 1]
AList[i + 1] <- Temp
SwapMade <- TRUE
ENDIF
NEXT i
Comparisons <- Comparisons - 1
ENDWHILE
OUTPUT “Sorted list: “, AList

How well did you know this?
1
Not at all
2
3
4
5
Perfectly