IOS Interview Encyclopedia of Common Algorithms _ios

Source: Internet
Author: User

This text to share the iOS interview familiar with the common algorithm, let's take a look at it.

1. Sort the following set of data in descending order (bubble sort). "24,17,85,13,9,54,76,45,5,63"

int main (int argc, char *argv[]) {

  int array[10] = {9, k, N, 5, MB;

  int num = sizeof (array)/sizeof (int);

  for (int i = 0; i < num-1. i++) {for

    (int j = 0; J < num-1-I. j) {

      if (Array[j] < array[j+1]) {

        I NT TMP = Array[j];

        ARRAY[J] = array[j+1];

        ARRAY[J+1] = tmp;

  }}} for (int i = 0; i < num i++) {

    printf ("%d", Array[i]);

    if (i = = num-1) {

      printf ("\ n");

    }

    else {

      printf ("");}}}

2. Sort the following set of data in ascending order (select sort). "86, 37, 56, 29, 92, 73, 15, 63, 30, 8"

void sort (int a[],int n)
{

  int i, J, index;

  for (i = 0; i < n-1; i++) {

    index = i;

    for (j = i + 1; j < N; j + +) {

      if (A[index] > A[j]) {

        index = j;

      }

    }

    if (index!= i) {

      int temp = a[i];

      A[i] = A[index];

      A[index] = temp;

    }

}} int main (int argc, const char * argv[]) {

  int numarr[10] = {(a), Panax Notoginseng, MB, 8};

  Sort (Numarr, ten);

  for (int i = 0; i < i++) {

    printf ("%d,", Numarr[i]);

  printf ("\ n");

  return 0;

}

3. Fast sorting algorithm

void sort (int *a, int left, int right) {

if (left >= right) {return

;

}

int i = left;

int j = right;

int key = A[left];

while (I < j) {while

(I < J && Key >= A[j]) {

j--

}

A[i] = a[j];

while (I < J && Key <= A[i]) {

  i++;

}

A[J] = A[i];

}

A[i] = key;

Sort (A, left, i-1);

Sort (A, i+1, right);

}

4. Merge sort

void merge (int sourcearr[], int temparr[], int startIndex, int midindex, int endindex) {int i = StartIndex;

  Int J = Midindex + 1;

  int k = StartIndex;  while (i!= Midindex + 1 && J!= endindex + 1) {if (Sourcearr[i] >= sourcearr[j]) {temparr[k++] =

    Sourcearr[j++];

    else {temparr[k++] = sourcearr[i++];

  } while (I!= Midindex + 1) {temparr[k++] = sourcearr[i++];

  while (J!= endindex + 1) {temparr[k++] = sourcearr[j++];

  for (i = StartIndex i <= endindex; i++) {sourcearr[i] = Temparr[i];

  } void sort (int soucearr[], int temparr[], int startIndex, int endindex) {int midindex;

    if (StartIndex < endindex) {Midindex = (StartIndex + endindex)/2;

    Sort (Soucearr, Temparr, StartIndex, Midindex);

    Sort (Soucearr, Temparr, Midindex + 1, endindex);

  Merge (Soucearr, Temparr, StartIndex, Midindex, endindex); {int main (int argc, const char * argv[]) {intNUMARR[10] = {86, 37, 56, 29, 92, 73, 15, 63, 30, 8};

  int temparr[10];

  Sort (Numarr, Temparr, 0, 9);

  for (int i = 0; i < i++) {printf ("%d,", numarr[i]);

  printf ("\ n");

return 0; }

5, the realization of the binary search algorithm (programming language is not limited)

int bsearchwithoutrecursion (int array[],int low,int high,int target) {while

[low <= high] {

int mid = (low + hi GH)/2;

if (Array[mid] > target) high

= Mid-1;

else if (Array[mid] < target) Low

= mid + 1;

else  //findthetarget return

mid;

}

The array does not contain the target

return-1;

}

----------------------------------------

recursively implement

int binary_search (const int arr[],int low,int high,int key)
{

int mid=low + (high-low)/2;

if (Low > High)

return-1;

else{

if (arr[mid] = = key) return

mid;

else if (Arr[mid] > key) return to

Binary_search (arr, Low, mid-1, key);

else return

Binary_search (arr, mid+1, High, key);

}

6, how to achieve the linked list reversal (linked list reverse order)?

Idea: Each time the second element is referred to the front.

#include <stdio.h> #include <stdlib.h> typedef struct Node {struct node *next;

int num;


}node;

  node *createlinklist (int length) {if (length <= 0) {return NULL;

  Node *head,*p,*q;

  int number = 1;

  Head = (node *) malloc (sizeof (node));

  Head->num = 1;

  Head->next = head;

  p = q = head;

    while (++number <= length) {p = (node *) malloc (sizeof (node));

    P->num = number;

    P->next = NULL;

    Q->next = p;

  Q = p;
return head;

  } void Printlinklist (node *head) {if (head = = NULL) {return;

  Node *p = head;

    while (p) {printf ("%d", p->num);

  p = P-> next;

printf ("\ n");


  Node *reversefunc1 (node *head) {if (head = = NULL) {return head;

  Node *p,*q;

  p = head;

  Q = NULL;

    while (p) {node *pnext = P-> Next;

    P-> next = q;

    Q = p;

  p = pnext;

} return q; int main (int argc, const char * argv[]) {Node *head = createlinklist (7);

    if (head) {printlinklist (head);

    Node *rehead = reverseFunc1 (head);

    Printlinklist (Rehead);

  Free (rehead);

  Free (head);

return 0; }

7, the realization of a string "How to Are You" output in reverse order (programming language is not limited). If the given string is "Hello World", the output should be "world hello".

int Spliterfunc (char *p) {

  char c[100][100];

  int i = 0;

  int j = 0;


  while (*p!= ') {

    if (*p = = ') {

      i++;

      j = 0;

    } else {

      C[i][j] = *p;

      j + +;

    }

    p++;


  }


  for (int k = i; k >= 0; k--) {

    printf ('%s ', c[k]);

    if (k > 0) {

      printf ("");

    } else {

      printf ("\ n")

    ;

  }

  return 0;


}

8. Given a string, output the position of the character that appears only once in this string and the top of the list? such as "Abaccddeeef", the character is B, the output should be 2.

Char *stroutput (char *);


int Comparedifferentchar (char, char *);


int main (int argc, const char * argv[]) {


  char *inputstr = ' Abaccddeeef ';

  Char *outputstr = stroutput (INPUTSTR);

  printf ("%c \ \", *outputstr);

  return 0;

}


Char *stroutput (char *s) {

  char str[100];

  char *p = s;

  int index = 0;

  while (*s!= ' ") {

    if (Comparedifferentchar (*s, p) = = 1) {

      Str[index] = *s;

      index++;

    }

    s++;

  }

  Return &str;
}


int Comparedifferentchar (char c, char *s) {

  int i = 0;

  while (*s!= ' && i<= 1) {

    if (*s = = c) {

      i++;

    }

    s++;
  }

  if (i = = 1) {return

    1;

  } else {return

    0

  ;

}}

9, the first sequence of binary tree traversal for Fbacdegh, the middle sequence traversal is: ABDCEFGH, please write this binary tree after the subsequent traversal results.

Adecbhgf

First-order + middle-order traversal reduction binary tree: first-order traversal is: ABDEGCFH in sequence traversal is: DBGEACHF

First, the first from order is a, the root of the binary tree, back to the middle order, it can be divided into three parts:

Zuozi sequence of DBGE, root A, and right subtree in sequence of CHF

Then the sequence of the left subtree is returned to the first order to get B as the root, so the sequence of returning to the left subtree divides the left sub-tree into three parts again:

Zuozi's left sub-tree D, Zuozi's root B, Zuozi's right subtree ge

Similarly, you can get the root of the right subtree as C

Similarly divides the right subtree into the root C, right subtree of the right subtree HF, note that its left subtree is empty

If the only one is the leaves do not have to do, just GE and HF again this operation, you can restore the two-fork tree.

10, print the prime between 2-100.

int main (int argc, const char * argv[]) {for

  (int i = 2; i < i++) {

    int r = isprime (i);

    if (r = = 1) {

      printf ("%ld", I);

    }

  }

  return 0;

}


int isprime (int n)
{

  int i, S;

  for (i = 2; I <= sqrt (n); i++)

    if (n% i = = 0) return 0;

  return 1;

}

11, to find the gcd of two integers.

int gcd (int a, int b) {

  int temp = 0;

  if (a < b) {

    temp = A;

    A = b;

    b = temp;

  }

  while (b!= 0) {

    temp = a% B;

    A = b;

    b = temp;

  }

  return A;

}

Summarize

The above is for you to tidy up in the iOS interview may encounter the common algorithm problems and answers, I hope this article for everyone's interview can have some help, if there are questions you can message exchange.

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.