Preliminary discussion on Java data structure and algorithm

Source: Internet
Author: User

Data Structure and algorithm

I. Data structure and algorithms

In any language is universal, is an independent discipline, in addition to the data structure is the algorithm

Common Data Structures:

Algorithms such as arrays (Aray), (Stack), queues (queue), binary tree, linked list (Linked list), hash table, hash table (hash), etc.

Bubble sort (bubble sort), select sort (Selection sort), binary search, etc.

data structures and algorithms are used well, program execution efficiency is high

Second, the Java stack

The stack stores the data in the last-in, first-out principle, the first data is pushed into the bottom of the stack, the final data is at the top of the buyers, and the data is ejected from the top when it needs to be read (the last data is read first).

is a special Sento table that allows insertions and deletions on the same side. The end of the allowed insert and delete operations is called the top of the stack (top), the other end is the bottom of the coin (botton), the bottom is fixed, and the number of elements in the stack top float is called null.

Inserting a stock called a stack (PUSH), deleting is called a fallback (POP). Stacks are also known as Advanced post-out.

Stacks can be used to store breakpoints when a function is called, and to use the stack when recursion is done!

Third, the use of graphics, draw a program execution process

How the method is executed:

The method allocates space in memory only when the method is called, and does not allocate space if the method only declares that it is not called

The method call allocates space in the "stack" (a chunk of memory in the JVM memory is the stack memory)

Method call is actually "stack", the end of the method is actually "stack"

Only the end symbol can "bounce"

 Public classAlgorithm { Public Static voidMain (string[] args) {inti = 100;    M1 (i); }     Public Static voidM1 (inti)    {m2 (i); }     Public Static voidM2 (inti)    {m3 (i); }     Public Static voidM3 (inti) {System.out.println ("I =" in the method in M3 +i); }    //Layer -by-layer delivery     Public Static voidM4 (inti) {System.out.println ("I =" in the method in M3 +i); }}

Iv. recursion


About recursive invocation of methods

1. Recursive invocation of a method is the method itself calling itself (self-tuning)

2. under the procedure because recursion does not end the condition, so has been in the stack, no stack, resulting in stack memory overflow!

3. So recursion must have an end condition


 public  class   recursion { public  static  void   main (string[] args) {    Array ( 1 public  static  void  Array (int   i) {      System.out.println ( "Array was bounced"  /*   Exception in thread "main" java.lang.St Ackoverflowerror at Recursion.array ( at Recursion.array (  */ }  

Case one, calculate the sum of 1-n "do not use recursion, how to do"

 Public classRecursion2 { Public Static voidMain (string[] agrs) {intValue = SUM (6); System.out.println ("VALUE =" +value); }    //This method needs to complete the summation of the 1-n//1+2+3+...+n     Public Static intSumintN) {        intsum = 0;  for(inti = 0; I <= N; i + +){            //sum = sum + i;Sum + =i; }        returnsum; }    //value =//Recursive Operation     Public Static intSUM1 (intN) {         if(n = = 1){             return1; }Else  {             returnn + sum1 (n-1); }    }    //value =}

Case two, calculating the factorial of n

 Public classRecursion3 { Public Static voidMain (string[] args) {intn = 5; System.out.println ("Factorial of n =" + sum2 (n));//factorial of n =    }    //1. Do not use recursion     Public Static intSumintN) {        intresult = 1;  for(inti = 1; I <= N; i++) {result= result *i; }        returnresult; }    //2. Using recursion     Public Static intSUM2 (intN) {        if(n = = 1){            return1; }Else  {            returnn * SUM2 (n-1); }    }}

Preliminary discussion on Java data structure and algorithm

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: 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.