Purchase Solution

Explanation of how to make an algorithm more efficient

Not what you're looking for?

Ask Custom Question

I have an algorithm called MinDistance that determines the distance between the two closest elements in an array. I need to make it more efficient.

Here is the algorithm, written in pseudocode:

MinDistance(A[0...n-1]
dmin <-- infinity
for i <--- 0 to n - 1 do
for j <--- 0 to n - 1 do
if i does not equal j and |A|i| - A|j|| < dmin
dmin <--- |A|i| - A|j||
return dmin

To me, this feels like it's in its most efficient form, but I'm wrong. Can you help?

Thank you very much.

Purchase this Solution

Solution Summary

This solution takes an n-squared algorithm for finding the two closest elements in an array and shows how that algorithm can be converted to a much faster algorithm.

Solution Preview

There are a couple things that can make this algorithm more efficient. First, calculating the distance between the two elements can be done in one place and stored in a variable. Then that variable can be used for ...

Purchase this Solution


Free BrainMass Quizzes
Basic Computer Terms

We use many basic terms like bit, pixel in our usual conversations about computers. Are we aware of what these mean? This little quiz is an attempt towards discovering that.

Java loops

This quiz checks your knowledge of for and while loops in Java. For and while loops are essential building blocks for all Java programs. Having a solid understanding of these constructs is critical for success in programming Java.

Basic UNIX commands

Use this quiz to check your knowledge of a few common UNIX commands. The quiz covers some of the most essential UNIX commands and their basic usage. If you can pass this quiz then you are clearly on your way to becoming an effective UNIX command line user.

Word 2010: Tables

Have you never worked with Tables in Word 2010? Maybe it has been a while since you have used a Table in Word and you need to brush up on your skills. Several keywords and popular options are discussed as you go through this quiz.

Excel Introductory Quiz

This quiz tests your knowledge of basics of MS-Excel.