To write this algorithm you firstly have to determine whether his score is high enough to be placed on the HighScore table.

For this to be the case his score must be higher than the lowest score.

Remember to always consider the variables before anything else.

Let Sam's score be called NewScore, and let Valid be True if its high enough and False if it isn't

To determine whether his score is large enough for the leaderboard we would need something like:

Valid = False

IF NewScore > HighScore(5)

....Valid = True

....HighScore(5) = NewScore

ENDIF

Now our problem is that Sam's score is on the last position on the leaderboard but what if its higher than the scores above it?

This part is really simple because we can already assume the rest of the scores are in order, so all we have to do is move Sam's score up an index if his score is greater than the index above.

Now we would need a variable called Position that is true if Sam's score is in the correct position and false if it isn't, and a variable Index that tells us the Index of Sam's Score.

Hence the full algorithm:

BEGIN ALGORITHM

....Index = 5//As Sam's score or any NewScore will always begin at the bottom

....Position = False

....Valid = False

....IF NewScore > Highscore(5)

........Valid = True

........Highscore(5) = NewScore

....ENDIF

....IF Valid = True

........WHILE Position = False AND Index <> 1//If Sam's score is first place, it wont undergo the Sort............IF NewScore > Highscore(Index - 1)

//Swaps Sam's score with the one above if his is greater

................Swap(Highscore(Index),HighScore(Index - 1))]//Standard Swap subroutine

................Index = Index - 1//Index of Sam's score will reduce by 1 every time the Swap occures

............ELSE

................Position = True

............ENDIF

........ENDWHILE

....ENDIF

END ALGORITHM

//Note this is a Standard Swap algorithm that you should know off by heart!

BEGINSwap(HighScore(Index),HighScore(Index - 1))

....Temp = HighScore(Index)

....Highscore(Index) = Highscore(Index-1)

....Highscore(Index - 1) = Temp

ENDSwap(HighScore(Index),HighScore(Index - 1))

## Bookmarks