Data structure Note 01: The 10 major algorithms (Java) that are common in the programming interview process

Source: Internet
Author: User
Tags array sort array to string bitwise operators

Here are the concepts related to the top 10 algorithms in the programming interview, and I'll use some simple examples to illustrate these concepts. Since the full mastery of these concepts requires more effort, this list is just as an introduction. This article looks at the problem from a Java perspective and includes the following concepts:

1. String

If the IDE does not have the code auto-completion feature, you should keep these methods in mind.

ToCharArray ()//Get the char array corresponding to the string

Arrays.sort ()//array sort

Arrays.tostring (char[] a)//array to string

charAt (int x)//Get the character at an index

Length ()//string lengths

Length//Array size

2. Linked list

In Java, the implementation of a linked list is very simple, with each node having a value Val and a link to the next node next.

Class Node {

int Val;

Node Next;

Node (int x) {

val = x;

next = null;

}

}

List of two notable applications are stack stacks and queue queues.

Stack:

Class stack{

Node top;

Public Node Peek () {

if (top! = null) {

return top;

}

return null;

}

Public Node pop () {

if (top = = null) {

return null;

}else{

Node temp = new node (top.val);

top = Top.next;

return temp;

}

}

public void push (Node N) {

if (n! = null) {

N.next = top;

top = n;

}

}

}

Queue:

Class queue{

Node first, last;

public void Enqueue (Node N) {

if (first = = null) {

First = n;

last = first;

}else{

Last.next = n;

last = n;

}

}

Public Node dequeue () {

if (first = = null) {

return null;

}else{

Node temp = new node (first.val);

first = First.next;

return temp;

}

}

}

3. Tree

The tree here usually refers to a two-fork tree, each of which contains a left child node and a right child node, as follows:

Class treenode{

int value;

TreeNode left;

TreeNode right;

}

Here are some of the concepts related to trees:

Balance vs. unbalanced: In a balanced binary tree, the depth difference between the left and right subtrees of each node is at most 1 (1 or 0).

Full binary tree with two forks: each node except the leaf node has two children.

Perfect binary tree (Perfect binary trees): is a full two-tree with the following properties: All leaf nodes have the same depth or level, and each parent node must have two children.

Complete binary tree: Two in a tree, each layer is fully filled, except for the last one, and all nodes must be left-leaning as far as they can.

Translator Note: The perfect binary tree is also vaguely called a complete binary tree. An example of a perfect binary tree is an ancestor diagram of a person at a given depth, because everyone must have two birth parents. A complete binary tree can be seen as a perfect binary tree that can have several additional left-leaning leaf nodes. Question: What is the difference between a perfect binary tree and a full two-fork tree? (Reference: http://xlinux.nist.gov/dads/HTML/perfectBinaryTree.html)

4. Figure

Graph-related issues are mainly focused on depth-first search and breadth-depth (breath first search).

The following is an implementation of a simple graph breadth-first search.

1) Define Graphnode

Class graphnode{

int Val;

Graphnode Next;

Graphnode[] neighbors;

Boolean visited;

Graphnode (int x) {

val = x;

}

Graphnode (int x, graphnode[] n) {

val = x;

Neighbors = n;

}

Public String toString () {

Return "value:" + this.val;

}

}

2) define a queue

Class queue{

Graphnode first, last;

public void Enqueue (Graphnode N) {

if (first = = null) {

First = n;

last = first;

}else{

Last.next = n;

last = n;

}

}

Public Graphnode dequeue () {

if (first = = null) {

return null;

}else{

Graphnode temp = new Graphnode (First.val, first.neighbors);

first = First.next;

return temp;

}

}

}

3) to achieve breadth-first search by queuing queue

public class Graphtest {

public static void Main (string[] args) {

Graphnode n1 = new Graphnode (1);

Graphnode n2 = new Graphnode (2);

Graphnode n3 = new Graphnode (3);

Graphnode N4 = new Graphnode (4);

Graphnode N5 = new Graphnode (5);

n1.neighbors = new Graphnode[]{n2,n3,n5};

n2.neighbors = new Graphnode[]{n1,n4};

n3.neighbors = new Graphnode[]{n1,n4,n5};

n4.neighbors = new Graphnode[]{n2,n3,n5};

n5.neighbors = new Graphnode[]{n1,n3,n4};

Breathfirstsearch (N1, 5);

}

public static void Breathfirstsearch (Graphnode root, int x) {

if (Root.val = = x)

System.out.println ("Find in Root");

Queue queue = new Queue ();

Root.visited = true;

Queue.enqueue (root);

while (Queue.first! = null) {

Graphnode C = (Graphnode) queue.dequeue ();

for (Graphnode n:c.neighbors) {

if (!n.visited) {

System.out.print (n + "");

N.visited = true;

if (N.val = = x)

System.out.println ("Find" +n);

Queue.enqueue (n);

}

}

}

}

}

Output:

1

2

Value:2 value:3 value:5 Find value:5

Value:4

5. Sorting

Here is the time complexity of the different sorting algorithms, you can go to the wiki to see the basic idea of these algorithms.

Algorithm Average time worst time Space

Bubble sort n^2 n^2 1

Select Sort n^2 n^2 1

Counting Sort n+k n+k n+k

Insertion sort n^2 n^2

Quick sort N log (n) n^2

Merge sort N log (n) n log (n) depends

In addition, here are some implementations/demos:: Counting Sort, mergesort, Quicksort, Insertionsort.

6. Recursion vs. iteration

For programmers, recursion should be an innate thought (a built-in thought), which can be explained by a simple example.

Problem: There are n steps, only 1 or 2 steps at a time, the total number of ways to go.

Step 1: Find the relationship between the top n steps and the front n-1 step steps.

In order to finish the n steps, there are only two methods: Climb 1 steps from the n-1 step steps or climb 2 steps from the n-2 steps. If f (n) is the number of methods that crawl to the nth step, then f (n) = f (n-1) + f (n-2).

Step 2: Make sure that the starting condition is correct.

F (0) = 0;

F (1) = 1;

public static int F (int n) {

if (n <= 2) return n;

int x = f (n-1) + f (n-2);

return x;

}

The time complexity of the recursive method is the number of N, because there are many redundant computations, as follows:

F (5)

F (4) + F (3)

F (3) + F (2) + F (2) + F (1)

F (2) + f (1) + f (1) + f (0) + F (1) + f (0) + F (1)

F (1) + f (0) + F (1) + f (1) + f (0) + F (1) + f (0) + F (1)

The direct idea is to translate recursion into iterations:

public static int F (int n) {

if (n <= 2) {

return n;

}

int first = 1, second = 2;

int third = 0;

for (int i = 3; I <= n; i++) {

Third = first + second;

first = second;

second = third;

}

return third;

}

For this example, iterations take less time, and you might want to see recursion vs iteration.

7. Dynamic Planning

Dynamic planning is a technique for solving the following nature-class problems:

A problem can be solved by the solution of the problem (translator Note: The optimal solution of the problem contains the optimal solution of its sub-problem, that is, the optimal sub-structure properties).

The solution of some sub-problems may need to be calculated several times (translator note: That is, sub-problem overlapping properties).

The solution to the sub-problem is stored in a table so that each sub-problem is calculated only once.

Additional space is required to save time.

The stair climbing problem is completely in line with the above four properties, so it can be solved by dynamic programming method.

public static int[] A = new int[100];

public static int F3 (int n) {

if (n <= 2)

a[n]= N;

if (A[n] > 0)

return a[n];

Else

A[n] = F3 (n-1) + F3 (n-2);//store results so only calculate once!

return a[n];

}

8. Bit operation

Bitwise operators:

OR (|) and (&) XOR (^) left shift (<<) Right shift (>>) not (~)

1|0=1 1&0=0 1^0=1 0010<<2=1000 1100>>2=0011 ~1=0

Gets the first bit of the given number n: (I count from 0 and start from the right)

public static Boolean getbit (int num, int i) {

int result = num & (1<<i);

if (result = = 0) {

return false;

}else{

return true;

}

For example, get the 2nd digit of the Number 10:

I=1, n=10

1<<1= 10

1010&10=10

0, so return true;

9. Probability problems

Solving probability-related problems usually requires a good plan to understand the problem (formatting the problem), where there is just one simple example of this type of problem:

There are 50 people in a room, so what is the probability that at least two people have the same birthday? (ignoring the fact that leap years, that is, 365 days a year)

The probability of calculating certain things can often be converted to calculate their relative faces first. In this example, we can calculate the probability that everyone's birthdays are different, that is: 365/365 * 364/365 * 363/365 * ... * (365-49)/365, so that at least two people birthday the same probability is the value of this.

public static double caculateprobability (int n) {

Double x = 1;

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

x *= (365.0-i)/365.0;

}

Double Pro = Math.Round ((1-x) * 100);

return pro/100;

Calculateprobability (50) = 0.97

10. Permutations and combinations

The difference between composition and arrangement is whether the order is critical.

Data structure Note 01: The 10 major algorithms (Java) that are common in the programming interview process

Related Article

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.