### As a program development, do we really need an algorithm? This question has been discussed all the time, zhihu, forums, various places are intertwined with his discussion, so as an iOS development, do we really need an algorithm? Personal feeling or need. #### why do we need to understand the algorithm so why do we need to understand the algorithm, the following first put a personal feel said is not bad, see from zhihu on an answer. Portal, indeed we can make a APP and let him live in less than how much knowledge of algorithm, but in the long run, if the motivation to do a technical personnel, have good logical thinking ability, simple code code is not enough, we need to do not simple knowledge made wheels, we also need to learn more things, Sometimes the pursuit of technology is what keeps us going. #### Algorithm in our life has always been enveloped in algorithm, from the data structure of the university to the first time to write a bubble sort with OC a long time ago, and now almost the interview will ask algorithm knowledge (including our company (one _ one). He seemed to surround our programming lives. Today we’re going to start with the most common bubbling sort.

  • Bubble sort In a set of numbers to be sorted, all numbers in the range of not yet sorted, the two adjacent numbers are compared and adjusted from top to bottom, so that the larger number to sink, the smaller number up. That is, whenever two adjacent numbers are compared and find that their sort is the opposite of the sort required, they are swapped. So let’s go to the code and start with the C version
int i, j, temp; Int a [] =,3,9,8,2,4,5,7,6,0,11 {1}; int length = sizeof(a)/sizeof(a[0]);for (j = 0; j < length; j++)
        {
            for (i = 0; i < length - j - 1; i++)
            {
                if(a[i] > a[i + 1]) { temp = a[i]; a[i] = a[i + 1]; a[i + 1] = temp; }}}for (i = 0; i < length; i++)
        {
            printf("%d,", a[i]);
        } 
        printf("\n");
Copy the code

Then watch a wave of the familiar OC version

  NSMutableArray *p = [[NSMutableArray alloc] initWithObjects:@"3"The @"5"The @"4"The @"1"The @"9"The @"0",nil];

    for (int i = 0; i<p.count; i++)
    {
        for (int j=i+1; j<p.count; j++)
        {
            int a = [p[i] intValue];
            
            int b = [p[j] intValue];
           
            if (a > b)
            {
                [p replaceObjectAtIndex:i withObject:p[j]];
                
                [p replaceObjectAtIndex:j withObject:[NSString stringWithFormat:@"%d",a]];
            }
        }
    }
   
    NSLog(@"% @",p);
Copy the code

And then there’s insertion sort, selection sort, all kinds of sorting, and we’re not going to do that, but we’re going to start talking about binary trees.

#### binary tree first talk about binary tree first look at the basic introduction of it.

  • The basic introduction of binary tree

  • Classification of binary trees

  • The first sequence traversal

  • For the same binary tree, if we choose different traversal methods, the results will be different. For the same figure, we choose middle traversal. The principle is: middle traversal is to visit the left subtree first, then the root node, then the right subtree, so we get the results: H,D,I,B,J,E,K,A,F,C,G

  • Follow-up traversal The principle of follow-up traversal is to traverse the left (right) subtree, then traverse the right (left) subtree in sequence, and finally visit the root to obtain the result: H,I,D,J,K,E,B,F,G,C,A

  • Hierarchical traversal The principle of hierarchical traversal is: top to bottom and left to right results: A,B,C,D,E,F,G,H,I,J,K

### Postscript So much for today, if I have time to write another one in the future, all luck comes from hard work, there is still a lot to learn in iOS development, there is something wrong with the above mentioned, welcome to point out.