Algorithms : Standard Algorithms
Standard Algorithms
Don't reinvent the wheel!
Introduction
In this section of the Algorithms Tutorial we will investigate a series of standard algorithms. Standard ways to do useful processing that happens regularly. Rather than reinvent the wheel, use and learn from these algorithms and save your self a lot of time and effort.
These algorithms are time tested and used by many people so you can use them with confidence knowing they have been rigourously tested and critiqued.
You don't have to use the algorithms as is. A lot of the time you will be able to as they represent often used, standard processing requirements, however if your needs vary there is nothing stopping you from useing them as a base and modifying to suit.
- Searching algorithms
- Sorting algorithms (coming soon)
- Advanced data structure algorithms (coming soon)
Basic Algorithms
These are simple and regularly used elements of processing.
Switch Two Items
Let's say you have two values and want to switch the variables that hold them :
Switch variables
- BEGIN
- Get var1
- Get var2
- temp = var1
- var1 = var2
- var2 = temp
- END
This standard algorithm can easily be expanded to switch up any number of variables.
Array based algorithms
For the following algorithms we will assume an array called elements. We will also use zero indexing.
Load an array of values
A Priming read is when we ask for a value or read a line from a file that is important in the condition of the loop prior to entering the loop.
Load array values
- BEGIN
- Set i to 0"
- Get dataValue
- WHILE dataValue != "ZZZ"
- Set elements[i] to dataValue
- Increment i by 1
- Get dataValue
- ENDWHILE
- Return elements
- END
Print an array of values
Print array values
- BEGIN
- Set i to 0"
- WHILE i < length of elements
- Display elements[i]
- Increment i by 1
- ENDWHILE
- END
Print array values
- BEGIN
- Set i to 0"
- WHILE i < length of elements
- Display elements[i]
- Increment i by 1
- ENDWHILE
- END
Sum an array of values
Sum array values
- BEGIN
- Set i to 0"
- Set total to 0"
- WHILE i < length of elements
- total = total + elements[i]
- Increment i by 1
- ENDWHILE
-
- Return total
- END
Sum array values
- BEGIN
- Set i to 0"
- Set total to 0"
- WHILE i < length of elements
- total = total + elements[i]
- Increment i by 1
- ENDWHILE
- Return total
- END
String based alorithms
We will look at three basic algorithms for manipulating strings. Think of these as the building blocks from which most other string manipulation functions can be created. These are :
- Substring
- Deleting
- Inserting
When working with manipulating strings we can effectively treat them like arrays of characters. Because of this, these standard algorithms can also be easily adapted to perform various array manipulation actions as well.
We will assume zero indexing for the following algorithms.
Substring
The original string remains untouched and we copy out or extract a portion of the string. There are two methods we can use in terms of specifying what to extract :
- Specify the start position in the string and how many charactesr to extract.
- Specify the start position and the end position to extract from.
Substing length
- BEGIN SubString(startCharacter, amount, baseString)
- Set extracted to ""
- Get pointer to startCharacter
- WHILE pointer < startCharacter + amount
- extracted = extracted + baseString[pointer]
- Increment pointer by 1
- ENDWHILE
- Return extracted
- END

Substring by position
- BEGIN SubString(startCharacter, endCharacter, baseString)
- Set extracted to ""
- Get pointer to startCharacter
- WHILE pointer < endCharacter
- extracted = extracted + baseString[pointer]
- Increment pointer by 1
- ENDWHILE
- Return extracted
- END SubString
These algorithms (and the ones below) assume that the input parameters are valid (eg. the endCharacter is not a value beyond the length of the baseString). If you require validation you will need to add that in yourself.
The next two standard algorithms will make use of the algorithms we just defined.
Deleting Strings
Removing a part of the string can be achieved by thinking of this process as cutting the original string into three sections :
- The part of the string before the deleted section.
- The part of the string to be deleted.
- The part of the string after the deleted section.
In this regard. if we get the substring before and the substring after and join them together then we have effectively deleted the required section.
Delete Substring
- BEGIN DeleteSubString(startCharacter, endCharacter, baseString)
- before = SubString(0, startCharacter, baseString)
- after = SubString(endCharacter + 1, length of baseString, baseString)
- finalString = before + after
- Return finalString
- END DeleteSubString

Again, this algorithm can be written with startCharacter and endCharacter as well.
Inserting Strings
Inserting a string into another is similar to the delete algorithm in that we split the original string into parts and then rejoin in the right way.
Insert String
- BEGIN InsertString(startCharacter, insertString, baseString)
- before = SubString(0, startCharacter, baseString)
- after = SubString(startCharacter + 1, length of baseString, baseString)
- finalString = before + insertString + after
- Return finalString
- END InsertString
Random number based algorithms
We will look at two things here :
- Generating a single random number.
- Generating a set of random numbers with no repeated numbers.
A single random number
Most modern programming languages have built in functions that can generate random numbers. They are generally very well written and although they are pseudo-random numbers, for all intents and purposes they are fairly random. A lot of languages have a built in function that will return a random value between 0 and 1.
0 ≤ x < 1
Note that the value could be equal to 0 but will be less than 1.
With this function we can easily transform the value into random integers in any range we want. Let's say this base function is called RND
Random number
- BEGIN randomInteger(min, max)
- randomBase = RND()
- range = max - min
- randomRange = randomBase * range
- randominteger = integer value of randomRange + min
- Return randomInteger
- END
- Line 2 - obtain a random number as a fraction between 0 and 1
- Line 3 - determine the range of numbers we need to expand it into
- Line 4 - by multiplying the number by the range we expand it into numbers between zero and range
- Line 5 - by adding min we shift the number to be between min and max
A set of unique random numbers
We often need a set of unique random numbers within a given range. That is there are no repeated numbers within the set. We will look at two algorithms to achieve this. A simple yet inefficient means and a more complex but rather effiecient means.
First off, the simple yet inefficient means :
Unique Random Numbers - Simple
- BEGIN UniqueRandom(min, max, amount)
- Set numbers to blank array
- Set i to 0
- WHILE length of numbers < amount
- number = randomInteger(min, max)
- IF number is not in numbers THEN
- numbers[i] = number
- Increment i by 1
- ENDIF
- ENDWHILE
- Return numbers
- END UniqueRandom
For the part is not in numbers (line 7) you would probably use a linear search.
This algorithm is quite simple however you can end up wasting a lot of time if you keep generating random numbers that have already been generated previously.
Improving from this there are a few options we could take. We could for instance :
- Create an array with all the numbers between max and min then shuffle that array (like you would a deck of cards). Then take in order from the beginning of the array the amount you need. This method seems reasonable but what degree of shuffling is required as acceptable to bring in randomness. It is also a lot of work if you have a large range and only need a small amount of numbers.
- Create a shadow array with the same number of items as the numbers to choose from and set them all to False. As you use them you can set them to True. This method is nice, and simple but still has the problem of repeatedly selecting a used number (it is just a bit quicker now to determine that it has in fact already been used).
There are also more complex solutions such as the Floyd algorithm and the Knuth algorithm. These methods involve a bit of mathematics but if you are up to it they are well worth investigating.
We will consider a method that is sort of a middle ground. It is fairly efficient and not too complex. It does have the down side of requireing a shadow array of numbesr however so there is a hit to memory requirements. For smallish ranges of numbers on modern computers this won't be an issue but if your range of numbers is quite large it could become an issue.
Unique Random Numbers - More Efficient
- BEGIN UniqueRandom(min, max, amount)
- Set availableNumbers to blank array
- Set i to 0
- Set range to max - min
- WHILE i < range
- availableNumbers[i] = min + i
- Increment i by 1
- ENDWHILE
- i = 0
- Set numbers to blank array
- WHILE length of numbers < amount
- number = randomInteger(i, range)
- numbers[i] = availableNumbers[number]
- availableNumbers[number] = availableNumbers[i]
- Increment i by 1
- ENDWHILE
- Return numbers
- END UniqueRandom
This might seem a little confusing so let's break it down :
- Lines 2 - 9 : Create a base array with all the possible numbers to choose from. That is, each number between min and max.
- Line 15 : Select a random index from that base array. It's important to note that we are selecting a random index as opposed to a random number.
- Line 16 : Copy the number stored at that random index into our array of random numbers.
- Line 17 : Move the number stored at index i to that of the random index we generated. This overwrites the number so we don't have to worry about it being selected again.
- Line 18 : Increment i, which actually serves two purposes. i represents the current point in the numbers array to place the next item. It also represents the point to start selecting the next random number from.
A simple example will help to illustrate the operation of this algorithm.
Let's say we want to generate 3 random numbers between 3 and 10.
Set up the availableNumbers array :
The first random number we generate is 2. This means that the value at index 2 is copied into the numbers array :
i is currently 0 so we copy the number at index 0 over the number at index 2 :
i is now incremented to 1 meaning that we will only select numbers between index 1 and 7. We start the process again and generate a random number of 5.
Again we move the item at index i (1) to the random index we generated (5).
We increment i again to 2 and generate another random number, this time between 2 and 7, which happens to be 5 again. This is ok as the number at index 5 is no longer 8 but 4. We have now generated our 3 unique random numbers.