' 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)
Complexity of Space (space complexity) is a measure of how much storage space is temporarily occupied by an algorithm while it is running, and it 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,ten, 7, 6
Then to 7, 7 found 10 larger than it, and then changed position with 10.
8, 9,7,ten, 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 voidInsertsort (list<int>list) { for(inti =0; I < list. Count; i++) { for(intj = i; J-1>=0; j--) { if(List[j-1] >List[j]) { inttemp = list[j-1]; List[j-1] =List[j]; LIST[J]=temp; Console.WriteLine (string. Join (",", list)); } Else Break; } } } Static voidMain (string[] args) {List<int> list =Newlist<int>() { 9,8,Ten,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 voidInsertSort2 (list<int>list) { for(inti =0; I < list. Count; i++) { intj =i; intBasenumber = List[j];//Pull the cards out first. for(; J-1>=0; j--) { if(List[j-1] >Basenumber) {List[j]= List[j-1];//behind the forward push } Else Break; } List[j]= Basenumber;//Insert the card into the vacancy after the end } } Static voidMain (string[] args) {List<int> list =Newlist<int>() { 9,8,Ten,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 voidInsertsort (list<int>list) { for(inti =0; I < list. Count; i++) { for(intj = i; J-1>=0; j--) { if(List[j-1] >List[j]) { inttemp = list[j-1]; List[j-1] =List[j]; LIST[J]=temp; } Else Break; } } } Public Static voidInsertSort2 (list<int>list) { for(inti =0; I < list. Count; i++) { intj =i; intBasenumber = List[j];//Pull the cards out first. for(; J-1>=0; j--) { if(List[j-1] >Basenumber) {List[j]= List[j-1];//behind the forward push } Else Break; } List[j]= Basenumber;//Insert the card into the vacancy after the end } } Static voidMain (string[] args) {List<int> list =Newlist<int>(); Random Random=NewRandom (); for(inti =0; I <50000; i++) {list. ADD (random. Next ()); } Stopwatch Watch=NewStopwatch (); 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?
The problem of spatial complexity of algorithms