Home  >  Article  >  Java  >  Is java an algorithm?

Is java an algorithm?

青灯夜游
青灯夜游Original
2019-11-19 17:13:242858browse

Is java an algorithm?

java is not an algorithm.

Java is an object-oriented programming language and a widely used computer programming language. It has the characteristics of cross-platform, object-oriented, and generic programming and is widely used in Enterprise-level web application development and mobile application development.

Algorithm refers to an accurate and complete description of a problem-solving solution. It is a series of clear instructions for solving problems. Algorithm represents a systematic method to describe the strategic mechanism for solving problems. In other words, it is possible to obtain the required output within a limited time for certain standardized inputs. If an algorithm is flawed or inappropriate for a problem, executing the algorithm will not solve the problem. Different algorithms may use different time, space, or efficiency to complete the same task. The quality of an algorithm can be measured by its space complexity and time complexity.

We can use java language to implement different algorithms.

Several common sorting algorithms in Java

1. Bubble sort

Is java an algorithm?

a. Bubble sort , obtain the maximum/minimum value through each traversal

b, put the maximum/minimum value at the tail/head

c, and then remove the maximum/minimum value, and the rest The data is traversing to obtain the maximum/minimum value

d, code implementation

public static void main(String[] args) {

        int arr[] = {8, 5, 3, 2, 4};

        //冒泡
        for (int i = 0; i < arr.length; i++) {
            //外层循环,遍历次数
            for (int j = 0; j < arr.length - i - 1; j++) {
                //内层循环,升序(如果前一个值比后一个值大,则交换)
                //内层循环一次,获取一个最大值
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j + 1];
                    arr[j + 1] = arr[j];
                    arr[j] = temp;
                }
            }
        }
    }

e, sorting process (red: moving data)

5,3, 2,4,

8

3,2,4,

5,8

2,3,

4,5,8

2,

3,4,5,8

2,3,4,5,8

2. Select sorting

Is java an algorithm?

a. Treat the first value as the minimum value

b, and then compare it with subsequent comparisons to find the minimum Value and subscript

c. Exchange the starting value and minimum value of this traversal

d. Description: During each traversal, treat the minimum value found previously as a The ordered list is treated as an unordered list, and then the unordered list is traversed each time to find the minimum value.

e, code implementation

public static void main(String[] args) {

        int arr[] = {6, 5, 3, 2, 4};

        //选择
        for (int i = 0; i < arr.length; i++) {
            //默认第一个是最小的。
            int min = arr[i];
            //记录最小的下标
            int index = i;
            //通过与后面的数据进行比较得出,最小值和下标
            for (int j = i + 1; j < arr.length; j++) {
                if (min > arr[j]) {
                    min = arr[j];
                    index = j;
                }
            }
            //然后将最小值与本次循环的,开始值交换
            int temp = arr[i];
            arr[i] = min;
            arr[index] = temp;
            //说明:将i前面的数据看成一个排好的队列,i后面的看成一个无序队列。每次只需要找无需的最小值,做替换
        }
    }

f, sorting process (red: moving data)

2,5,3,6 ,4

2,3,5,6,4

2,3,4,6,5

2,3,4,5,6

2,3,4,5,6

3. Insertion sort

Is java an algorithm?

#a. By default, comparison starts from the second data.

b. If the second data is smaller than the first, swap. Then compare it with the third data, and if it is smaller than the previous one, insert it (cunning). Otherwise, exit the loop

c. Note: By default, the first data is regarded as an ordered list, and the following unordered lists loop through each data. If it is smaller than the previous data, it is inserted (exchanged). Otherwise exit.

d, code implementation

public static void main(String[] args) {

        int arr[] = {7, 5, 3, 2, 4};

        //插入排序
        for (int i = 1; i < arr.length; i++) {
            //外层循环,从第二个开始比较
            for (int j = i; j > 0; j--) {
                //内存循环,与前面排好序的数据比较,如果后面的数据小于前面的则交换
                if (arr[j] < arr[j - 1]) {
                    int temp = arr[j - 1];
                    arr[j - 1] = arr[j];
                    arr[j] = temp;
                } else {
                    //如果不小于,说明插入完毕,退出内层循环
                    break;
                }
            }
        }
    }

e, sorting process (red: ordered, black: unordered)

5,7 ,3,2,4

3,5,7,2,4

2,3,5,7,4

2,3,4,5,7

4. Hill sorting (insertion sort variant)

Is java an algorithm?

a. It is basically the same as insertion sort.

b. The difference is that the step size of each cycle is halved.

c. Note: The basic principle is similar to insertion sort, but the difference is. Insertion sorting by spacing multiple data.

d, code implementation

public static void main(String[] args) {

        int arr[] = {7, 5, 3, 2, 4};

        //希尔排序(插入排序变种版)
        for (int i = arr.length / 2; i > 0; i /= 2) {
            //i层循环控制步长
            for (int j = i; j < arr.length; j++) {
                //j控制无序端的起始位置
                for (int k = j; k > 0  && k - i >= 0; k -= i) {
                    if (arr[k] < arr[k - i]) {
                        int temp = arr[k - i];
                        arr[k - i] = arr[k];
                        arr[k] = temp;
                    } else {
                        break;
                    }
                }
            }
            //j,k为插入排序,不过步长为i
        }
    }

e, sorting process (step size 4/2/1)

4,1,3,2,6,5 ,8,9,7

3,1,4,2,6,5,7,9,8

1,2,3,4,5,6,7,8 ,9

5. Quick sort

Is java an algorithm?

a. Confirm that the first data in the list is the middle value, and the first value is regarded as blank ( Low pointer is vacant).

b. Then in the remaining queue, it is regarded as having two left and right pointers (high and low).

c. Start moving the high pointer to the left. If data smaller than the middle value is encountered, the data is assigned to the low pointer vacancy, and the high pointer data is regarded as a vacancy value (high pointer vacancy). Then move the low pointer to the right first, and switch the low pointer movement.

d、当低指针移动到大于中间值的时候,赋值到高指针空缺的地方。然后先高指针向左移动,并且切换高指针移动。重复c、d操作。

e、直到高指针和低指针相等时退出,并且将中间值赋值给对应指针位置。

f、然后将中间值的左右两边看成行的列表,进行快速排序操作。

g、代码实现

public static void main(String[] args) {

        int arr[] = {7, 5, 3, 2, 4, 1, 8, 9, 6};

        //快速排序
        int low = 0;
        int high = arr.length - 1;
        quickSort(arr, low, high);  
    }

    public static void quickSort(int[] arr, int low, int high) {
        //如果指针在同一位置(只有一个数据时),退出
        if (high - low < 1) {
            return;
        }
        //标记,从高指针开始,还是低指针(默认高指针)
        boolean flag = true;
        //记录指针的其实位置
        int start = low;
        int end = high;
        //默认中间值为低指针的第一个值
        int midValue = arr[low];
        while (true) {
            //高指针移动
            if (flag) {
                //如果列表右方的数据大于中间值,则向左移动
                if (arr[high] > midValue) {
                    high--;
                } else if (arr[high] < midValue) {
                    //如果小于,则覆盖最开始的低指针值,并且移动低指针,标志位改成从低指针开始移动
                    arr[low] = arr[high];
                    low++;
                    flag = false;
                }
            } else {
                //如果低指针数据小于中间值,则低指针向右移动
                if (arr[low] < midValue) {
                    low++;
                } else if (arr[low] > midValue) {
                    //如果低指针的值大于中间值,则覆盖高指针停留时的数据,并向左移动高指针。切换为高指针移动
                    arr[high] = arr[low];
                    high--;
                    flag = true;
                }
            }
            //当两个指针的位置相同时,则找到了中间值的位置,并退出循环
            if (low == high) {
                arr[low] = midValue;
                break;
            }
        }
        //然后出现有,中间值左边的小于中间值。右边的大于中间值。
        //然后在对左右两边的列表在进行快速排序
        quickSort(arr, start, low -1);
        quickSort(arr, low + 1, end);
    }

h、排序过程

6,5,3,2,4,1,7,9,8

1,5,3,2,4,6,7,9,8

1,5,3,2,4,6,7,9,8

1,4,3,2,5,6,7,9,8

1,2,3,4,5,6,7,9,8

1,2,3,4,5,6,7,9,8

1,2,3,4,5,6,7,8,9

6、归并排序

Is java an algorithm?

a、将列表按照对等的方式进行拆分

b、拆分小最小快的时候,在将最小块按照原来的拆分,进行合并

c、合并的时候,通过左右两块的左边开始比较大小。小的数据放入新的块中

d、说明:简单一点就是先对半拆成最小单位,然后将两半数据合并成一个有序的列表。

e、代码实现

public static void main(String[] args) {

        int arr[] = {7, 5, 3, 2, 4, 1,6};

        //归并排序
        int start = 0;
        int end = arr.length - 1;
        mergeSort(arr, start, end);
    }

    public static void mergeSort(int[] arr, int start, int end) {
        //判断拆分的不为最小单位
        if (end - start > 0) {
            //再一次拆分,知道拆成一个一个的数据
            mergeSort(arr, start, (start + end) / 2);
            mergeSort(arr, (start + end) / 2 + 1, end);
            //记录开始/结束位置
            int left = start;
            int right = (start + end) / 2 + 1;
            //记录每个小单位的排序结果
            int index = 0;
            int[] result = new int[end - start + 1];
            //如果查分后的两块数据,都还存在
            while (left <= (start + end) / 2 && right <= end) {
                //比较两块数据的大小,然后赋值,并且移动下标
                if (arr[left] <= arr[right]) {
                    result[index] = arr[left];
                    left++;
                } else {
                    result[index] = arr[right];
                    right++;
                }
                //移动单位记录的下标
                index++;
            }
            //当某一块数据不存在了时
            while (left <= (start + end) / 2 || right <= end) {
                //直接赋值到记录下标
                if (left <= (start + end) / 2) {
                    result[index] = arr[left];
                    left++;
                } else {
                    result[index] = arr[right];
                    right++;
                }
                index++;
            }
            //最后将新的数据赋值给原来的列表,并且是对应分块后的下标。
            for (int i = start; i <= end; i++) {
                arr[i] = result[i - start];
            }
        }
    }

f、排序过程

5,7,3,2,4,1,6

5,7,2,3,4,1,6

2,3,5,7,4,1,6

2,3,5,7,1,4,6

2,3,5,7,1,4,6

1,2,3,4,5,6,7

推荐教程:Java教程

The above is the detailed content of Is java an 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