create account

Programming Tip -- Why it matters which algorithms you use when you program (including an example) -- Big Oh by charlie.wilson

View this thread on: hive.blogpeakd.comecency.com
· @charlie.wilson · (edited)
$3.47
Programming Tip -- Why it matters which algorithms you use when you program (including an example) -- Big Oh
http://1.bp.blogspot.com/-OjIL5P9VQpM/Uc639K6vX4I/AAAAAAAAFaU/kvEVOrFx490/s1600/Computer+art+7.jpg
[Image source](http://quantumartandpoetry.blogspot.fr/2013/06/quantum-computers-classical-or.html)

So why does it matter which algorithm your program uses? As long as it gets the job done, isn't it fine? This is actually not the case at all. Some algorithms are a lot slower than others. This makes a huge difference when dealing with very large amounts of data. So how can we measure this?

# Big Oh
Big Oh is a way of measuring the speed of a program on a large scale. Take a look at this C++ program segment:
```
int a = 4;
int b = 5;
cout << a + b;
```

This section will always run the same no matter what. It has 3 lines. We could say the complexity of this program is 3. In terms of Big Oh, this would be an O(1) complexity. The reason for this is because Big Oh doesn't care about the literal complexity of a program. When we have a program with the complexity of 3xO(n^2) and one with 300xO(n^2), the 3 and 300 become very insignificant when n is  billion. These both just break down to simply O(n^2). This makes analyzing the speed of a program much easier. Also, when the complexity of a program is O(n) + O(n^2), we just call it O(n^2). For the sake of simplicity, we only care about the largest Big Oh.

Take a look at this program segment:
```
for(int x = 0; x < n; x++)
    cout << x + y;
```

This loop executes n times. This is categorized under O(n).

now take a look at this program segment:
```
for(int x = 0; x < n; x++)
    for(int y = 0; y < n; y++)
        cout << x + y;
```

These are two nested loops. The second loop executes n times multiplied by the number of times that the first loop executes, which is also n. This means that the Big Oh comes out to O(n^2). Do you see the pattern? For 3 nested loops, the Big Oh would be O(n^3), and so on. For the powers beyond O(n^3), the Big Oh is simply just O(2^n). This is for simplicity. Anything such as O(n^4), O(n^5), O(n^6)... are so terrible that they are just lumped in with O(2^n) category.

Here is the list of Big Oh complexity. The list goes from faster on top to slower on bottom.
```
O(1)
O(logn)
O(n)
O(nlogn)
O(n^2)
O(n^3)
O(2^n)
O(n!)
O(n^n)
``` 

So when programming, it is best to shoot for the lowest possible Big Oh for your program. 

Click [here](http://bigocheatsheet.com) for a very useful website on Big Oh.

# A Real Life Example
A while ago, I wrote a program that could solve the traveling salesman problem. Here is the problem:
> A salesman needs to visit n cities, once each. Given a list of the prices of flights from each city to another, calculate the cheapest route.

The only way to find the cheapest route every single time is to test every possible path and compare them all. This comes out to a Big Oh of O(n!). It is absolutely terrible! Here is some sample output of my program. I used the time command on a Linux system to calculate how long the program was running. The number after "input" indicates how many cities each input file contains.
```
[cmw4026@omega TSP]$ gcc tsp.c
[cmw4026@omega TSP]$ time ./a.out < input5
The tour  1 - 2 - 3 - 4 - 5 - 1 costs: 10500

real    0m0.001s
user    0m0.000s
sys     0m0.001s
[cmw4026@omega TSP]$ time ./a.out < input10
The tour  1 - 8 - 4 - 2 - 3 - 7 - 6 - 5 - 10 - 9 - 1 costs: 2000

real    0m0.202s
user    0m0.200s
sys     0m0.001s
[cmw4026@omega TSP]$ time ./a.out < input11
The tour  1 - 8 - 7 - 5 - 2 - 3 - 4 - 6 - 10 - 9 - 11 - 1 costs: 1972

real    0m3.365s
user    0m2.257s
sys     0m0.001s
[cmw4026@omega TSP]$ time ./a.out < input12
The tour  1 - 8 - 6 - 7 - 5 - 4 - 2 - 10 - 3 - 9 - 11 - 12 - 1 costs: 1644

real    0m55.419s
user    0m27.915s
sys     0m0.012s
[cmw4026@omega TSP]$
```
The time it took to run this program jumped from 3 to 55 seconds when I added only one more element! As you can see, O(n!) is an absolutely horrible Big Oh for a program to have!

Want to see the time for 13 cities (elements)?
```
The tour  1 - 11 - 4 - 8 - 2 - 10 - 7 - 6 - 3 - 9 - 13 - 5 - 12 - 1 costs: 1811

real    15m53.206s
user    6m53.345s
sys     0m0.423s
[cmw4026@omega TSP]$
```
15 minutes!!! I'm going to have to stop here!

I hope this helped! Leave any suggestions in the comments!

---

Be sure to check out my programming tutorial series of you liked this post. It goes over mostly C++, but also C and some basics that can be applied to most languages. Here is the series up to date:

[**Part 1: Hello World!**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming)

[**Part 2: Variables**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-part-2-for-dummies)

[**Part 3: Functions**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-part-3-for-dummies)

[**Part 4: if(), else, else if()**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-not-language-specific-very-helpful-for-beginners)

[**Part 5: Loops**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-not-language-specific-beginner-friendly)

[**Part 6: Arrays**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-beginner-friendly)

[**Part 7: Basic Input/Output**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-p7-very-beginner-friendly)

[**Part 9: Sorting Arrays**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-sorting)

[**Part 10: Random Numbers**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-generating-random-numbers)

[**Part 11: Colored Text in your Terminal**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-colored-text-on-your-terminal)

[**Part 12: Recursion**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-recursion-made-simple)

[**Part 13: Binary Search**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-binary-search)

[**Part 14: 2D Arrays**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-very-easy)

[**Part 15: String Processing**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-c-vs-c-programming-strings)

[**Part 16: Binary, Bitwise Operators, and 2^n**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-binary-bitwise-operators-and-2-n)

[**Part 17: Pointers**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-pointers)

[**Part 18: Pointer Arithmetic**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-pointer-arithmetic)

[**Part 19: Object Oriented Programming 1 -- Data Structures**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-object-oriented-programming-1-data-structures)

[**Part 20: Object Oriented Programming 2 -- Classes**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-object-oriented-programming-2-classes)

[**Part 21: Object Oriented Programming 3 -- Polymorphism**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-object-oriented-programming-3-polymorphism)

[**Part 22: Command Line Arguments**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-command-line-arguments)

[**Part 23: Header Files**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-header-files)

[**Part 24: Programming in separate files**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-programming-in-separate-files)

[**Part 25: File I/O**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-100-followers-edition-file-i-o)

[**Part 26: Threading and concurrency**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-concurrency-and-pthreads)

[**Part 27: Converting Decimal to Binary and Hexidecimal**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-converting-decimal-to-binary-and-hexidecimal)

---

<center>https://i.imgsafe.org/5b80091fff.png</center>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 125 others
properties (23)
authorcharlie.wilson
permlinkprogramming-tip-why-it-matters-which-algorithms-you-use-when-you-program-including-an-example-big-oh
categoryprogramming
json_metadata{"tags":["programming","tutorial","howto","bigoh","algorithms"],"image":["http://1.bp.blogspot.com/-OjIL5P9VQpM/Uc639K6vX4I/AAAAAAAAFaU/kvEVOrFx490/s1600/Computer+art+7.jpg","https://i.imgsafe.org/5b80091fff.png"],"links":["http://quantumartandpoetry.blogspot.fr/2013/06/quantum-computers-classical-or.html","http://bigocheatsheet.com","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-part-2-for-dummies","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-part-3-for-dummies","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-not-language-specific-very-helpful-for-beginners","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-not-language-specific-beginner-friendly","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-beginner-friendly","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-p7-very-beginner-friendly","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-sorting","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-generating-random-numbers","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-colored-text-on-your-terminal","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-recursion-made-simple","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-binary-search","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-very-easy","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-c-vs-c-programming-strings","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-binary-bitwise-operators-and-2-n","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-pointers","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-pointer-arithmetic","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-object-oriented-programming-1-data-structures","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-object-oriented-programming-2-classes","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-object-oriented-programming-3-polymorphism","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-command-line-arguments","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-header-files","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-programming-in-separate-files","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-100-followers-edition-file-i-o","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-concurrency-and-pthreads","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-converting-decimal-to-binary-and-hexidecimal"]}
created2016-11-03 21:28:09
last_update2016-11-03 21:30:54
depth0
children4
last_payout2016-12-05 03:36:18
cashout_time1969-12-31 23:59:59
total_payout_value2.664 HBD
curator_payout_value0.804 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length8,593
author_reputation18,779,183,062,076
root_title"Programming Tip -- Why it matters which algorithms you use when you program (including an example) -- Big Oh"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id1,681,524
net_rshares20,903,257,980,423
author_curate_reward""
vote details (189)
@linkback-bot-v0 ·
This post has been linked to from another place on Steem.


  - [Advanced Steem Metrics Report for 3rd November 2016](https://steemit.com/steemit/@ontofractal/advanced-steem-metrics-report-for-3rd-november-2016) by @ontofractal




Learn more about and upvote to support [**linkback bot v0.5**](https://steemit.com/steemit/@ontofractal/steem-linkback-bot-v0-5-the-reddit-awareness-release). Flag this comment if you don't want the bot to continue posting linkbacks for your posts.

Built by @ontofractal
👍  
properties (23)
authorlinkback-bot-v0
permlinkre-charlie-wilson-programming-tip-why-it-matters-which-algorithms-you-use-when-you-program-including-an-example-big-oh-linkbacks
categoryprogramming
json_metadata{}
created2016-11-04 18:53:27
last_update2016-11-04 18:53:27
depth1
children0
last_payout2016-12-05 03:36:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length504
author_reputation1,915,954,976,722
root_title"Programming Tip -- Why it matters which algorithms you use when you program (including an example) -- Big Oh"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id1,687,091
net_rshares13,939,211,173
author_curate_reward""
vote details (1)
@taino33 ·
Algorithm Is the word of the year
👍  
properties (23)
authortaino33
permlinkre-charliewilson-programming-tip-why-it-matters-which-algorithms-you-use-when-you-program-including-an-example-big-oh-20161103t220250796z
categoryprogramming
json_metadata{"tags":["programming"]}
created2016-11-03 22:02:54
last_update2016-11-03 22:02:54
depth1
children2
last_payout2016-12-05 03:36:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length33
author_reputation305,944,964
root_title"Programming Tip -- Why it matters which algorithms you use when you program (including an example) -- Big Oh"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id1,681,715
net_rshares12,232,840,006
author_curate_reward""
vote details (1)
@charlie.wilson ·
I didn't know this. Who decides word of the year? Haha!
properties (22)
authorcharlie.wilson
permlinkre-taino33-re-charliewilson-programming-tip-why-it-matters-which-algorithms-you-use-when-you-program-including-an-example-big-oh-20161103t221149017z
categoryprogramming
json_metadata{"tags":["programming"]}
created2016-11-03 22:11:51
last_update2016-11-03 22:11:51
depth2
children1
last_payout2016-12-05 03:36:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length55
author_reputation18,779,183,062,076
root_title"Programming Tip -- Why it matters which algorithms you use when you program (including an example) -- Big Oh"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id1,681,740
net_rshares0
@raymonjohnstone ·
We did. :P
👍  
properties (23)
authorraymonjohnstone
permlinkre-charliewilson-re-taino33-re-charliewilson-programming-tip-why-it-matters-which-algorithms-you-use-when-you-program-including-an-example-big-oh-20161103t224448736z
categoryprogramming
json_metadata{"tags":["programming"]}
created2016-11-03 22:44:48
last_update2016-11-03 22:44:48
depth3
children0
last_payout2016-12-05 03:36:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length10
author_reputation14,615,508,998,439
root_title"Programming Tip -- Why it matters which algorithms you use when you program (including an example) -- Big Oh"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id1,681,899
net_rshares12,232,840,006
author_curate_reward""
vote details (1)