You are on page 1of 4

Basic Algorithms

In all the algorithms that we will illustrate in pseudocode we will be using an array or list of
integers. Integers are easy to work with and understand and there is no loss of generality in
our algorithms.

An array is a contiguous space in memory to store values. An array is an ordered sequence of


values. The order is indicated by the position or index. The first position or index starts at 0.
The next index 1 and so on. The last index is the size of the array - 1. Here is an example of
an array of 10 unsorted integers.

Index 0 1 2 3 4 5 6 7 8 9
Value 20 13 24 53 46 15 36 72 8 29

Here the length of the array is 10 but the last index is 9. To refer to any given element by
position we use square brackets. For example, if we called the array a, the third element in a
is a[2]. In all our examples, to follow we will be using an array of integers called a that has
been prepopulated. Assume also that we can get the size or length of the array.

Traversing an Array

Our basic operation starts with visiting each element of the array exactly once and in order.
We can write that in pseudocode in two ways:

FOR i = 0 to array length - 1

FOR each element in the array


In the first example we are going by position starting at the first position 0 and going to the
last position. In the second example we are going element by element.

Summing the Array

In any summing problem, we need a place or variable to store the sum. We have to initialize
this variable to zero to start with and then traverse the array adding each element to the sum.
Here is the pseudocode:

SET Sum to 0
FOR each element in the array
ADD element to Sum
ENDFOR
PRINT Sum
Here is another version of the same code:
INITIALIZE Sum to 0
FOR i = 0 to array length - 1
ADD array[i] to Sum
ENDFOR
WRITE Sum

Find Maximum Value in the Array


To find the maximum value, you initialize a placeholder called max with the value of the first
element in the array. Then you go through the array element by element. If any element is
greater than max you replace max with that element. Here is the pseudocode:

SET Max to array[0]


FOR i = 1 to array length - 1
IF array[i] > Max THEN
SET Max to array[i]
ENDIF
ENDFOR
PRINT Max

Sequential Search for an Element in the Array

Let the element that we are searching for be X. We need to know if that element occurs in the
array. One way is to return the position of the first occurrence of that element and if that
element does not exist return -1 which is an invalid index. This is the pseudocode:

FOR i = 0 to array length - 1


IF X = array[i] THEN
RETURN i
ENDIF
ENDFOR
RETURN -1
Another variation of this problem is to return the number of occurrences of X in the array.
Here is a modification of the above code:
SET Count to 0
FOR i = 0 to array length - 1
IF X = array[i] THEN
INCREMENT Count
ENDIF
ENDFOR
RETURN Count

Binary Search for an Element in a Sorted Array

For binary search to work, let us assume that the array is already sorted in ascending order.
We will return the position of the element X if we find it, otherwise we will return -1.

SET Lo to 0
SET Hi to array length - 1
WHILE Lo <= Hi
SET Mid to (Lo + Hi) / 2
IF X < array[Mid] THEN
SET Hi to Mid - 1
ELSE IF X > array[Mid] THEN
SET Lo to Mid + 1
ELSE
RETURN Mid
ENDIF
ENDWHILE
RETURN -1
Binary Search is one of the the most efficient algorithms that we have. It is efficient because
in each iteration we are halving the search space.
Selection Sort

This is one of the easiest sorting algorithms to understand and write but not the most efficient
one. In Selection Sort we start at the first element of the array and go through the array and
find the minimum element. We swap the minimum element with the element at the first
place. We start at the second position in the array and go through the array and find the
minimum element in the remaining portion of the array. We swap that minimum element
with the element with the element at the second position. We start at the third position and
repeat the procedure until we reach the end of the array.

FOR i = 0 to array length - 2


SET Min to array[i]
SET MinIndex to i
FOR j = i + 1 to array length - 1
IF array[j] < Min THEN
Min = array[j]
MinIndex = j
ENDIF
ENDFOR
SET array[MinIndex] to array[i]
SET array[i] to Min
ENDFOR
Selection Sort is not efficient. It does the same amount searches if the values in the array are
in random order, partially sorted or completely sorted.

Merge Two Sorted Arrays

To merge two sorted arrays a and b we first create a third array c that has the same size as
array a and b combined. We compare the first element in a with the first element in b. We
insert the smaller of the two elements into array c. We then advance by one position in the
array a or b that we took the element from. We keep going until we deplete all the elements
in either array a or b. Then we add all the elements in the remaining array to c. Here is the
pseudocode:

CREATE array c having size = array length a + aray length b


SET aIdx to 0
SET bIdx to 0
SET cIdx to 0
WHILE aIdx < length of a - 1 and bIdx < length of b - 1
IF a [aIdx] < b [bIdx] THEN
SET c[cIdx] to a[aIdx]
INCREMENT aIdx
INCREMENT cIdx
ELSE
SET c[cIdx] to b[bIdx]
INCREMENT bIdx
INCREMENT cIdx
ENDIF
ENDWHILE

WHILE aIdx < length of a - 1


SET c[cIdx] to a[aIdx]
INCREMENT aIdx
INCREMENT cIdx
ENDWHILE
WHILE bIdx < length of b - 1
SET c[cIdx] to b[bIdx]
INCREMENT bIdx
INCREMENT cIdx
ENDWHILE

RETURN c

This code is superfluous when initializing to zero, but the nested for loops are needed to
initialize to other value(s).

task implementation (code fragment)


double max = a[0];
find the maximum of the for (int i = 1; i < a.length; i++)
array values if (a[i] > max) max = a[i];
int N = a.length;
double sum = 0.0;
compute the average of for (int i = 0; i < N; i++)
the array values sum += a[i];
double average = sum / N;
int N = a.length;
double[] b = new double[N];
copy to another array for (int i = 0; i < N; i++)
b[i] = a[i];
int N = a.length;
for (int i = 0; i < N/2; i++)
{
reverse the elements double temp = a[i];
within an array a[i] = a[N-1-i];
a[N-i-1] = temp;
}
int N = a.length;
double[][] c = new double[N][N];
matrix-matrix for (int i = 0; i < N; i++)
multiplication (square for (int j = 0; j < N; j++)
matrices) { // Compute dot product of row i and
column j.
for (int k = 0; k < N; k++)
a[][]*b[][] = c[][] c[i][j] += a[i][k]*b[k][j];
}

You might also like