Learntofish's Blog

A blog about math, physics and computer science

Basic algorithm 1: Finding the minimum

Posted by Ed on November 8, 2011

I will write about a couple of basic algorithms that are handy for programming puzzles. Today we start with the following problem:

Given an array with integers, how do you determine the smallest number in the array?

Here is my solution in C++:

#include <iostream>
using namespace std;

// function prototype
int findMin(int*, int);


// main program
int main(){
    int myArray[]={33,-14,90,-27,53,15};
    cout << "Smallest number: " << findMin(myArray,6) << endl;

    return 0;
}


// pass the array and its length to the function
int findMin(int* array, int arrayLength){
    int currentMin=array[0];

    for(int i=1; i<arrayLength; i++){
        if(array[i]<currentMin){
            currentMin = array[i];
        }
    }
    return currentMin;
}

We go through the array with a for loop (line 19-23). Each time we examine an array-element we compare it to the current minimum. If the array-element is smaller than the current minimum, the array-element becomes the new current minimum (line 20-22).
As you may have noticed I’ve written a function findMin() instead of writing everything into the main() function. And I have also used a function prototype. A function prototype is useful if many people are working on a project. Suppose Alice and Bob are developers. Alice needs the findMin() function but leaves the implementation to Bob. So, Alice writes the function prototype into the source code

int findMin(int*, int);

By doing so Bob knows exactly what kind of parameters Alice needs: Alice wants a function that returns an int and takes two parameters, an int* pointer and an int.

Below is a Java version of the algorithm:

public class BasicAlgorithm {
	
	// determine smallest number
	int findMin(int array[]){
		int currentMin=array[0];
		for(int i=1; i<array.length; i++){
			if(array[i]<currentMin){
				currentMin = array[i];
			}
		}
		return currentMin;
	}
	
	// main program
	public static void main(String[] args){
		int myArray[]={33,-14,90,-27,53,15};
			
		// Create new object
		BasicAlgorithm myObj = new BasicAlgorithm();
		// Call findMin() method
		System.out.println("Smallest number: " + myObj.findMin(myArray));		
	}
}

Note that in the Java we can determine the length of a string by using: myString.length
In C++ this is not possible. In C++ we just pass a pointer to our function.

Below is a Python version of the algorithm:

def findMin(array):
    currentMin=array[0]
    for i in range(1,len(array)):
        if(array[i]<currentMin):
            currentMin=array[i]
    return currentMin
    
def main():
    myArray=[33,-14,90,-27,53,15]
    print("Smallest number: ", findMin(myArray))

The Python version looks similar to the Java one and also looks very “clean”.

Advertisements

One Response to “Basic algorithm 1: Finding the minimum”

  1. […] Basic algorithm 1: Finding the minimum […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: