[Java] Java basic algorithm bubble sort, selection sort and binary search

table of Contents

Bubble Sort:

Selection order:

Binary search method:


Bubble Sort:

        Bubble sort is a relatively simple and common sorting algorithm in the computer field. The principle of the algorithm is: repeatedly access the element column that needs to be sorted, and compare two adjacent elements in turn. If the order is wrong from large to small or from small to large, the two data are exchanged . The origin of the name of this algorithm is because the smaller element slowly "floats" to the top of the sequence through exchange (ascending or descending). If the bubbles of carbon dioxide in carbonated beverages eventually rise to the top, the name "bubble sort" .

Sort the array as follows:

Array: 6,5,4,2,3,1 (ascending order)

The first trip (i=0): The                                                                                           second trip (i=1):

          First comparison: 5      6       4 2 3 1 First comparison: 4      5       2 3 1 6     

          Second comparison: 5 4      6       2 3 1 Second comparison: 4 2      5       3 1 6

          Third comparison: 5 4 2      6       3 1 Third comparison: 4 2 3       5       1 6

          Fourth comparison: 5 4 2 3      6       1 Fourth comparison: 4 2 3 1      5     6

          Fifth comparison: 5 4 2 3 1      6                     

The third trip (i=2): The                                                                                         fourth trip (i=3):

           First comparison: 2        4       3 1 5 6 First comparison: 2      3        1 4 5 6

           Second comparison: 2 3        4      1 5 6 Second comparison: 2 1        3        4 5 6

           The third comparison: 2 3 1      4      5 6                              (The first comparison, 2<3, so there is no need to exchange positions)

The fifth time (i=4):

          First comparison: 1      2        3 4 5 6 

       Comparison of the first pass, or more than 6 in any of a number set (array) should be large, so After completion of the first comparison, 6 jumps to the last time, while during the second pass of the comparison, since the first In the comparison of trips, the largest number (the smallest number) has been moved to the end, so in the second trip, there is no need to compare with the last number . The same is in the latter

In the third pass, there is no need to compare with the last two numbers.

In the fourth pass, there is no need to compare with the last three numbers.

In the fifth pass, there is no need to compare with the last four numbers

        In the above moving process, it can be found that the number of comparisons "i" is the length of the set (array) -1, and the number of times of each comparison (5,4,3,2,1) "j" can be used: set (Array) Length-the number of passes -1. In each comparison, start from the beginning and compare the size of the two numbers. If the current number is greater than the following number, the two numbers exchange positions.

        code show as below:

   public static int[] bubbleMethod(int[] arr){
        //执行的趟数
        for (int i = 0; i < arr.length; i++) {
            //每趟比较的次数
            for (int j = 0; j < arr.length - 1 - i; j++) {
                //判断两个数的大小
                if (arr[j]>arr[j+1]){
                    int temp=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=temp;
                }
            }
        }
        return arr;
    }

Selection order:

          I found an animated picture to reflect it:

Array: 6,5,4,2,3,1 (ascending order) [Red numbers represent: data to be compared with the next data,      blue numbers represent: data to be compared ]

The first trip (i=0): (Compare the first number with the following number) The                         second trip (i=1): (Compare the second number with the following number)

          First comparison:     6       4 2 3 1 First comparison: 1          4       5 3 6    

          Second comparison: 4       5      6 2 3 1 Second comparison: 1          4      5       3 6   

          Third comparison: 2           5 6 3 1 Third comparison: 1      2     4 5          6   

          Fourth comparison: 2        4 5      3       6 1 Fourth comparison: 1      2      4 5 3      6   

          Fifth comparison: 1             4 5 3 6                    

The third trip (i=2): () The                                                                                          fourth trip (i=3):

           First comparison: 1 2     4           3 6 First comparison: 1 2 3      4           6

           Second comparison: 1 2     3   4       5 6 Second comparison: 1 2 3      4       5      6   

           Third comparison: 1 2     3       5       4 6                              

The fifth time (i=4):

          First comparison: 1 2 3 4     5     6 

First trip: Compare the first number with all subsequent numbers . If the first number is greater than the subsequent numbers, swap positions with the subsequent numbers, and continue to compare the exchanged numbers.

Second pass: After the first cycle, the smallest number has been placed in the first position , so compare the second number with the following number. If the second number is greater than the following number, it will be compared with the following number. Exchange positions, continue to compare the exchanged ones,…………

Get the rules:

        Each time the i-th index element is compared, it is first compared with the i+1th sum element , and the value of i is not greater than the length of the data (collection)

public static int[] bubbleMethod(int[] arr){
    for (int i = 0; i < arr.length; i++) {
        for (int j = i+1; j < arr.length; j++) {
           //判断两个数的大小
           if (arr[i]<arr[j]){
               int temp=arr[i];
               arr[i]=arr[j];
               arr[j]=temp;
           }
       }
    }
}

Binary search method:

      It is also called a binary search algorithm to find the index position of an element.

Applicable premise:

  • The sequence must be sorted

  • Do not consider the repetition of elements

//定义方法完成二分查找
    public static int find(int[] arr, int a){
        //定义开始和结束
        int i = 0;
        int j = arr.length-1;
        //循环
        while(true){
            //判断
            if(i > j){
                //表示找不到
                return -1;
            }
            //中间值
            int mid = (i+j)/2;
            //比较
            if(arr[mid] == a){
                return mid;
            }else if(arr[mid] < a){
                i = mid + 1;
            }else{
                j = mid - 1;
            }
        }
    }

 

Guess you like

Origin blog.csdn.net/weixin_43267344/article/details/108018568