Home  >  Article  >  Java  >  what is java algorithm

what is java algorithm

angryTom
angryTomOriginal
2020-02-03 11:45:226905browse

what is java algorithm

What is java algorithm

Algorithm refers to an accurate and complete description of the problem-solving solution, which is a series of methods to solve the problem. Clear instructions, java algorithm is to use Java language to implement clear instructions to solve a certain problem.

Characteristics of the algorithm:

Input property: There are zero or more external quantities as input to the algorithm

Output property: Algorithm Produce at least one quantity as output

Deterministic: Each instruction in the algorithm is clear and unambiguous

Finiteness: The number of executions of each instruction in the algorithm is limited, and the execution of each instruction is time Also limited

Feasibility: The algorithm can run accurately in principle, and people can complete it after doing a limited number of operations with paper and pen.

Program: The algorithm uses a specific programming language Implementation, the program does not need to meet the four criteria of the exhaustive

algorithm:

Correctness: Under reasonable data input, it can be obtained within a limited time Correct results

Readability: It should be easy for humans to understand and debug

Robustness: The ability to check for errors and handle them appropriately

Efficiency: Algorithm The amount of computer resources required for execution, including running time and storage space

The description form of the algorithm: 1. Natural language 2. Algorithm block diagram method 3. Pseudo code language 4. High-level programming language

The general process of algorithm design:

1. Understand the problem

2. Predict all possible inputs

3. Between exact solution and approximation Choosing between solutions

4. Determining the appropriate data structure

5. Algorithm design technology

6. Describing the algorithm

7. Tracking algorithm

8. Analyze the efficiency of the algorithm

9. Write code according to the algorithm

The following is an algorithm implemented in Java: Bubble sort

/**
 * 冒泡排序
 */
public class BubbleSort1 {
    public static void BubbleSort(int[] arr) {
        boolean flag = true;
        while(flag){
            int temp;//定义一个临时变量
            for(int i=0;i<arr.length-1;i++){//冒泡趟数,n-1趟
                for(int j=0;j<arr.length-i-1;j++){
                    if(arr[j+1]<arr[j]){
                        temp = arr[j];
                        arr[j] = arr[j+1];
                        arr[j+1] = temp;
                        flag = true;
                    }
                }
                if(!flag){
                    break;//若果没有发生交换,则退出循环
                }
            }
        }
    }
    public static void main(String[] args) {
        int arr[] = new int[]{1,6,2,2,5};
        BubbleSort.BubbleSort(arr);
        System.out.println(Arrays.toString(arr));
    }
}

Related article tutorial recommendations: java introductory tutorial

The above is the detailed content of what is java algorithm. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn