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.
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 arrayIn 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.
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 SumHere 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
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 FOR i = 1 to array length - 1 IF array[i] > Max THEN SET Max to array[i] ENDIF ENDFOR PRINT Max
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 -1Another 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
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 -1Binary 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.
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 ENDFORSelection 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.
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