Topic 4 Computational Thinking
Describe the characteristics of standard algorithms on linear arrays.
Sequential search | ARRAYNAME = (9,2,6,5,8,1) TARGET = 6 FOUND = False INDEX = 0 loop for i from 0 to ARRAYNAME.length-1 if ARRAYNAME[i] = TARGET then FOUND = True INDEX = i end if end loop output INDEX if FOUND = False then output “Target not found” end if |
Binary search | ARRAYNAME = (9,2,6,5,8,1) TARGET = 6 LOW = 0 HIGH = ARRAYNAME.length - 1 FOUND = FALSE loop while LOW <= HIGH MID = (LOW + HIGH) div 2
if ARRAYNAME[MID] = TARGET then output MID FOUND = True else if TARGET > ARRAYNAME[MID] then LOW = MID + 1 else HIGH = MID - 1 end if end loop if FOUND = False then output "Target not found" end if |
Bubble sort | ARRAYNAME = (3,4,2,1,9,8) for i from 1 to ARRAYNAME.length for j from 0 to ARRAYNAME.length-1 if ARRAYNAME[j]>a[j+1] swap( a[j], a[j+1] ) end if end for loop end for loop output ARRAYNAME |
Selection sort | ARRAYNAME = (9,2,6,5,8,1) loop for i from 0 to ARRAYNAME.length-2
MIN = i
loop for j from i+1 to ARRAYNAME.length-1
if ARRAYNAME[j] < ARRAYNAME[MIN] then MIN = j end loop if MIN != i then swap(a[i],a[MIN]) end if end loop |