Sum of the most common algorithmic arithmetic series

Share this video with your friends

Social Share Links

Send Tweet
Published 7 years ago
Updated 3 years ago

There is a particular arithmetic series that shows up time and time again in analysis of naïve implementations of many common programming challenges.

In this lesson we show why the time complexity is Quadratic, so you don't have to keep analyzing it over and over.

Quite commonly in algorithms, you're given items one by one and the task at hand can easily be solved by looking at all the items you've been given thus far. A classic example of such a task is sorting. Let's say we have some numbers 2, 4, 6, 7, 8, 9. We're not told that they are already sorted and our task with sorting them.

Of course, we have discussed many excellent algorithms for this, but let's come up with another nice solution. We look at these input numbers one by one, copying them over to their rightful sorted place in an output array.

In iteration one, we get the number two and just pop this number into the start. We need it to look at zero existing numbers. In iteration two, you pick the next number four, compare it to two, since it's larger, we pop it at the end. In total, we need it look at one existing numbers.

In iteration three, we get the number the six, compare it to two, since it's larger, we next compare it to four, since it's larger we now get to pop it at the end. We need it to look at two existing numbers. Similarly, in iteration four, for the worst case, you might to look at three existing items. And so on till iteration end, you might have to look at n-1 existing items.

Now, the total work across n iterations for this wasteful sorting algorithm is simply the sum of the lookups in each iteration.

Let's call the sum A. Another way of writing the same sum is as a descending series where you can continually look at lesser, and lesser remaining items till you arrive at zero remaining items. Now, if you just sum these two ways of writing the same equation, we get a new equation where we have n values all of which are n-1.

Two A equals n(n-1). Thus, the total work A equals n(n-1)/2. You'll see this equation quite commonly in text books, but as algorithm designers we are generally interested in the bigger complexity which results to the highest power of n, which is n squared.

Remember, whenever you're looping n times and looking at all existing items, or looping n times and only decreasing the amount of work in each iteration by one, the final algorithm will be at least quadratic in nature.

egghead
egghead
~ just now

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today