Spatial complexity of algorithms

Source: Internet
Author: User

' Algorithmic space complexity ', don't think how big this thing is, I promise you will understand it after reading this article.

Recently in the gnawing algorithm, found very interesting. In the course of my study, I found a problem, that is, the problem of space complexity, it is definitely the killer of efficiency.

Introduction to space complexity (from Baidu)

Spatial complexity (space complexity) is a measure of how much storage space is temporarily occupied by an algorithm while it is running, and is recorded as S (n) =o (f (n)). For example, the time complexity of direct insertion sequencing is O (n^2), and the spatial complexity is O (1). The general recursive algorithm will have O (n) space complexity, because each recursive to store the return information. The advantages and disadvantages of an algorithm are mainly measured from the execution time of the algorithm and the storage space required to occupy two.

Take the insertion sort to say. Insert sort and the step we made when we played poker in real life was like that. In the case of landlords, we often take the shun out of the way, and think about how we got it? For example we have so 5 cards 9, 8, 10, 7, 6. The process should look like this:

9, 8, 10, 7, 6

Starting from 8, 8 found 9 larger than it, and then 8 inserted into the front of 9.

8, 9, 10, 7, 6

Then to 10,10 found that it is larger than the previous 2, so do not move.

8, 9, 10, 7, 6

Then to 7, 7 found 10 larger than it, and then changed position with 10.

8, 9, 7, 10, 6

And then 7 found that 9 was bigger than it, so he changed positions with 9.

8, 7, 9, 10, 6

And then 7 found that 8 was bigger than it, so he changed positions with 8.

7, 8, 9, 10, 6

Wait, it seems a little wrong. To the card ' 7 ' that step seems too troublesome, we usually pick up 7, directly into the 8 before the finished. Simple and quick, definitely faster than plugging in. That's right! This is the problem of spatial complexity. The following directly on the 2 sets of code to check.

   public static void Insertsort (list<int> list)        {for            (int i = 0; i < list. Count; i++)            {for                (int j = i; j-1 >= 0; j--)                {                    if (List[j-1] > List[j])                    {                        int temp = list[j-1 ];                        LIST[J-1] = list[j];                        LIST[J] = temp;                        Console.WriteLine (String. Join (",", list));                    else break        ;        }}} static void Main (string[] args)        {            list<int> List = new List<int> ()            {                9,8,10,7,6            };            Insertsort (list);            Console.readkey ();        }

We can see that this method is really stupid. is a one-way forward plug. This is certainly not what we want. We'll improve.

  public static void InsertSort2 (list<int> list)        {for            (int i = 0; i < list. Count; i++)            {                int j = i;                int basenumber = list[j];//First pulls the card out for                (; j-1 >= 0; j--)                {                    if (List[j-1] > Basenumber)                    {                        L IST[J] = forward push behind list[j-1];//                    }                    else break                        ;                }                LIST[J] = basenumber;//After the end of the card inserted into the empty space            }        }        static void Main (string[] args)        {            list<int> List = new List<int> ()            {                9,8,10,7,6            };            InsertSort2 (list);        }

In fact, the idea is to take out 1 cards (for example, the position of the card drawn is 3, note: Now 3 is empty), if the previous card (position 2) than it, the 2 moved to the top. 2 is empty.

And then the front one (position 1) if it's bigger than the drawn card, keep moving forward. Because 2 is empty, 1 moves to 2. Now the 1 is empty.

Then put the drawn cards on the 1 and finish.

The process is as follows

8, 9, 10, 7, 6

7

8, 9, 10, 6

8, 9,, 10, 6

8,, 9, 10, 6

, 8, 9, 10, 6

7, 8, 9, 10, 6

Let's see how far away the efficiency of execution is.

 public static void Insertsort (List<int> list) {for (int i = 0; i < list. Count; i++) {for (int j = i; j-1 >= 0; j--) {if (List[j-1] & Gt                        LIST[J]) {int temp = list[j-1];                        LIST[J-1] = List[j];                    LIST[J] = temp;                } else break; }}} public static void InsertSort2 (List<int> List) {for (int i = 0; i < list. Count;                i++) {int j = i;  int basenumber = list[j];//First pulls the card out for (; j-1 >= 0; j--) {if (list[j                    -1] > Basenumber) {list[j] = forward push after list[j-1];//}                else break;     }           LIST[J] = basenumber;//After the end of the card inserted into the empty space}} static void Main (string[] args) {            list<int> list = new list<int> ();            Random random = new random (); for (int i = 0; i < 50000; i++) {list. ADD (random.            Next ());            } Stopwatch Watch = new Stopwatch (); Watch.            Start ();            Insertsort (list); Watch.            Stop (); Console.WriteLine (watch.            Elapsedmilliseconds); Watch.            Reset (); Watch.            Start ();            InsertSort2 (list); Watch.            Stop (); Console.WriteLine (watch.            Elapsedmilliseconds);        Console.readkey (); }

Run results

Was it a big surprise? 1234X times worse. Forget it, I'm not good at maths.

The idea is often used in a lot of times when the platoon is in a hurry. I wonder if you have got some help? Do you also have to notice when programming?

A companion tour, a free dating site: www.jieberu.com

Push family, free tickets, scenic spots: www.tuituizu.com

Spatial complexity of algorithms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.