Pseudocode searches (1 Viewer)

SadCeliac

done hsc yay
Joined
Sep 23, 2021
Messages
3,116
Location
Sydney <3
Gender
Male
HSC
2023
Do we need to define the swap(a, b) function if we call it within a Sort???

Code:
// suppose this function is called by swap(arr(i), arr(i-1))
// would this be enough, or do I need to do something else??

BEGIN swap(a, b):
    INITIALISE c as integer
    LET c = a
    LET a = b
    LET b = c
END swap
 

dav53521

Well-Known Member
Joined
Mar 23, 2022
Messages
345
Gender
Male
HSC
2022
Do we need to define the swap(a, b) function if we call it within a Sort???

Code:
// suppose this function is called by swap(arr(i), arr(i-1))
// would this be enough, or do I need to do something else??

BEGIN swap(a, b):
    INITIALISE c as integer
    LET c = a
    LET a = b
    LET b = c
END swap
You probably should unless it's already defined as you could loose marks over it
 

dav53521

Well-Known Member
Joined
Mar 23, 2022
Messages
345
Gender
Male
HSC
2022
so, would what I put be sufficient? It just doesn't seem very... good
Yes it is because it does swap the values as C temporary holds the value in A which will have its value set to the in B and then B will be set to the the value in A as C is temporarily holding it.

There's no point in making an overly complex algorithm if you can achieve the desired results using a simple one.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top