Derived from quick sorting-how to learn Algorithms

Source: Internet
Author: User

Most people know that we generally do not need to learn anything.Algorithm. Unless it is, it is either a student (who is determined to participate in ACM), or a professional who is engaged in purely Algorithmic Research, or preparing for an interview for some big companies, or purely interested. There are only a few people who need many algorithms to participate in their work. Of course, it may be about image processing, data processing, data mining, or searching engines.ArticleLook at the application fields of their respective algorithms: the top ten most classic algorithms in the world today-when voting, you can also get a glimpse of them ). I even think that most people must have mastered some basic algorithms related to data structures. So, all in all, I always believe that a person, especially a student, there is really no need to spend too much effort on algorithm-related.

However, I had two friends who had an understanding of quick sorting, which gave me a little doubt about my previous point of view, that I didn't need to learn more algorithms.

    • When I was looking for a job in early July, a friend (previously an interviewer) talked to me about the fact that nine out of the top ten students could not write a quick sorting list. I was shocked. Because, I don't believe it. I cannot understand how many people cannot write such basic and important simple and fast sorting. More than 20 lines in totalCodeA. So I want to try my own on-site capabilities. In this case, the quick sorting is written (however, there are still some detailed errors, and then modified again ). This is one of them.
    • I met two friends in Guangdong last night. They will be working in this company the next day. One of his friends talked about his interview at the company. When the interviewer asked him about the time complexity and space complexity of fast sorting, he said that he was completely confused, and he had no idea at all before, I don't know. I immediately told him that the average time complexity of fast sorting is O (n * logn), and the worst is O (n ^ 2). Because the space complexity is stored by arrays, of course it is O (n ). I told him that you took a piece of paper and a pen, and within five minutes, I could write out a fast and accurate sorting on the spot. A friend said it was amazing. I understood two points at the time: 1. Some people did not understand fast sorting, but did not even know the basics. 2. Many people call it amazing, it turns out that I only know some basic algorithms that they don't know but are also simple enough.

Since I started to write the classical algorithm research series, many friends have asked me to write an article on how to learn algorithms, or talk about my experiences and experiences in algorithm learning. This was because the algorithm was not used much in actual work (just like the fast sorting algorithm mentioned above, a sort in the standard library was done directly, you don't need to write more than a dozen lines of code at all. In this case, you need to repeat the wheel ). Second, I personally think that it is right for you to learn such things. If you are interested, you can solve any problems. There should be no longer a long story like "everyone" or "experts. But now, the situation is different (I hate people who install it in front of me. You can say that your algorithms are awesome, you can be self-righteous, or you can say that a person engaged in algorithm research can edit books ).

For more information about how to learn algorithms (Learning Algorithms = interest + attitude + coding + focus on practical applications):

    1. Interest. To learn anything well, you must first be interested. The algorithm is no exception. If you are not interested in algorithms, you don't have to worry about them. After all, each has its own love.
    2. Attitude, Retry degree. If you think that algorithms are not used much in actual work, or the standard library encapsulates everything. Or you don't need to know about the most basic quick sorting, so this article can be swept away.
    3. Practice. Reading books, such as data structures,Make sure that all kinds of basic data structures, such as arrays, strings, stacks, heaps, queues, trees, and graphs, are completely transparent.(In fact, it is also very helpful to read the 100 questions I have prepared for Microsoft interviews, because most of the interview questions are related to data structures and algorithms, I did that too), followed by the basic iterative method, the exhaustive search method, the progressive method, the recursive method, the greedy method, the divide and conquer method, and the dynamic planning method, as my friend Tianshi said, the backtracking method must be fully understood. Then you can read the programming Pearl,Introduction to Algorithms(Many people say that the introduction to algorithms is incomprehensible. In fact, it is a lot of mathematical evidence above, and I do not understand it very well. The specifics can be painted on paper, such as the operations and Code related to the red and black trees), coupled with scrutiny-repeated thinking, repeated research, and final coding-focus on and combined with practical applications, coding implementation (writing code to implement an algorithm is more useful than developing an algorithm ). Only the above, no it also.

Many of my friends also asked me how I learned algorithms, or how the learning process was like this: since last December, I started to get started with algorithms (write the first algorithm article, A * search algorithm) first, because I want to write articles about algorithms, I often need to refer to documents, including books and online articles. I pay special attention to the fact that an algorithm is clearly explained, to be clear, I have to understand the algorithm first, that is, I can understand it only when I understand it. Then I have been doing the interview questions about Microsoft and other companies (most of them involve data structures and algorithms), and then I have to talk with others. Finally, after thinking about an algorithm, I began to write code to implement it.This is the process of learning algorithms..

OK. Let's take a look.Implementation of quick sorting algorithms. As shown in, this is the quick sorting of exercises in the company two days ago. For details, refer:

 

I personally think that it is not difficult to write this quick sorting completely, but pay attention to many details, such:

    1. We know that there are two methods to divide and conquer partitions in quick sorting, one is the method of gradually scanning backward after the two pointer indexes mentioned above (this method is used in the introduction to algorithms ), another method is to scan two pointers from the first to the middle (most people and general textbooks use this second method ).
    2. During the partition process, pay attention to some boundary value issues. For example, I, j index initialization, for loop, J from the first position of the array L to the last second position of the H-1, and when data [J] <= privot (is less than or equal to, not less than), and then finds it, I ++, and then exchanges. There was another exchange of data [I + 1] AND DATA [H. Finally, I + 1 is returned.
    3. The third thing to note is recursion. If (L

The main issue to be noted is the above three aspects. As long as you have a good grasp of the details, you can quickly sort the algorithm in over 20 rows. Is a fast Sorting AlgorithmSecond Implementation(That is, the scanning method of the beginning and end directions mentioned above ):

Here, we also need to pay attention to a small problem, that is, in the above partition process, the order of the processes A and B in the WHILE LOOP cannot be wrong. If the process A and B are switched over, it is equivalent to assigning data [l] To privot first, so data [I], that is, privot overwrites the value of data [J] at a height. Why can I use columns in the original normal order? Because data [l] has been saved in privot, we are not afraid that privot will overwrite the low value.

While (data [J]> = privot & I <j)
J --;
Data [I] = data [J]; // process A of the above three actions
While (data [I] <= privot & I <j)
I ++;
Data [J] = data [I]; // process B of the above three actions

If you do not understand it, you can see the following sample process. First, we sort the data in the normal process first:

: 3 8 7 1 2 5 6 4 // The first element is the primary element
2 8 7 1 5 6 4
B: 2 7 1 8 5 6 4
C: 2 1 7 8 5 6 4
D: 2 1 7 8 5 6 4
E: 2 1 3 7 8 5 6 6 4 // final complement, keyword 3

If the sequence of the above Code A and B is changed, that is, the column is wrong? As follows:

While (data [I] <= privot & I <j)
I ++;
Data [J] = data [I];// Process B of the above three actions

While (data [J]> = privot & I <j)
J --;
Data [I] = data [J];// Process A of the above three actions

The sorting process will be described as follows. Element 8 at the lower part will overwrite element 4 at the height. element 4 is not saved in privot as element 3 above, so element 4 is lost:

 A:38 7 1 2 5 6 4// The primary element is the first element.
3 7 1 2 5 6 8
B :.....

OK. For more versions of the Quick Sort Algorithm Implementation, refer to this article: 12. C/C ++ implementation of all versions of the Quick Sort Algorithm.

Conclusion (The image quotations shown in the following 2nd points come from an anonymous netizen. However, he expressed what I want to say.):

    1. Although 80% of the projects do not require self-constructing algorithms, it is necessary to understand enough basic algorithms. Fast sorting (only for students) does not matter, but at the same time, it shows a question of attitude that does not focus on basic algorithms.

Today, I understand that finding a job must be related to your own interests. Otherwise, nothing can be said. (My phone, 18970930595, mailbox, zhoulei0907@yahoo.cn ). All the above comments represent my point of view.

Thank you for browsing. I hope it will be helpful in addition to chatting. .

The author owns the copyright and copyright of the original article. Reprinted. Please indicate the source and author. Otherwise, you will be held legally liable.

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.