The basic algorithms: Bubble Sort

The basic algorithms: Bubble Sort

Introduction to Algorithms Bubble

Bubble algorithm (Bubble Sort), is a relatively simple sorting algorithm. Sorting logic which is: the column element to be sorted repeatedly accessed sequentially comparing two neighboring elements, if the order (e.g., the descending first letter from A to Z) is incorrect, it will exchange the position of the element, the repeated process until no adjacent elements need to be exchanged.
Name of the algorithm because the algorithm after sorting, the smaller elements will pass through the exchange will be like as bubbles float slowly to the top of the columns, so named bubble algorithm.

Detailed sorting algorithm

Suppose there are an array of [1,6,3,5,2], the way we use from small to large will be carried out bubble sort.

The first trip Sort:

  1. 1 as compared with 6, 6 is less than 1, the same position. [1,6,3,5,2]
  2. 6 compared with 3, 6 is greater than 3, the exchange position. [1,3,6,5,2]
  3. 6 compared with 5, 6 is greater than 5, the exchange position. [1,3,5,6,2]
  4. 6 compared with 2, 6 is greater than 2, the exchange position. [1,3,5,2,6]

The second trip Sort:

  1. 1 as compared with 3, is less than 3, the same position. [1,3,5,2,6]
  2. 3 compared with 5, 3 is less than 5, the same position. [1,3,5,2,6]
  3. 5 compared with 2, 5 is greater than 2, the exchange position. [1,3,2,5,6]

The third trip Sort:

  1. 1 as compared with 3, is less than 3, the same position. [1,3,2,5,6]
  2. 3 compared with 2, 3 is greater than 2, the exchange position. [1,2,3,5,6]

analysis

  • Each trip sort bubble sort, a comparison will be less data. Each sort because the greater will be the index after the shift, the first trip row sorting, find maximum, second pass sorting The second largest value. Compare to last, we need to compare the data less and less. N data length to be subjected to the bubble sort, N-1 times to be sorted, the number of the i-th negative times is Ni-1 times (since each trip a data comparison will be less).

Complexity Analysis

Let's analyze the next bubble sort time complexity and space complexity.

time complexity:

  • Best-case scenario: the data itself is already sorted, then no element exchange. Only n-1 times to complete sort, the time complexity is O (n).
  • Worst case: data are in reverse order, so we have to re-order all at once. For an n-bit column number is the number of relatively (n-1) + (n-2) + ... + 1 = n * (n - 1) / 2 = O (n ^ 2).
  • Average complexity: O (n ^ 2)

Space complexity: the storage space consumed by the algorithm.

  • The best: The data itself is orderly and does not use temporary variables, the complexity is 0.
  • Worst case: data sorted in reverse order, each will use the temporary variable. Complexity is O (n).
  • Average complexity: O (1).

Java code implementation

package top.enjoyitlife.bubble;

import java.util.Arrays;

/***
* @ClassName: BubbleAlgorithm 
* @Description: 冒泡排序算法
* @author MegaSlark 
 */
public class BubbleAlgorithm {
    public static void main(String[] args) {
        int[] nums=new int[] {1,2,3,6,7};
        BubbleAlgorithm ba = new BubbleAlgorithm();
        ba.bubbleSort(nums);
        System.out.println(Arrays.toString(nums));
    }

    /****
     * 冒泡排序 正序 有小到大
     * @param nums
     */
    public void bubbleSort(int[] nums) {
        if(null==nums||nums.length<1) {
            return;
        }
        int length=nums.length;
        //数组下标从0开始,比较趟数为数组长度减1
        for(int i=0;i<length-1;i++) {
            //是否需要全部循环标识
            boolean flag=true;
            //length-1的原因同上,多减i是因为不需要比较的数据为i个。每次冒泡都会找出一个不许要再次比较的数。
            for( int j=0;j<length-1-i;j++) {
                // 控制冒泡方向 倒序还是正序
                int temp=0;
                if(nums[j]  >  nums[j+1]) {
                    temp=nums[j];
                    nums[j]=nums[j+1];
                    nums[j+1]=temp;
                    flag=false;
                }
            }
            if(flag) {
                break;
            }
            
        }
    }
}

These are accepted and code examples bubble sort, I hope for your help.

Guess you like

Origin www.cnblogs.com/enjoyitlife/p/12002496.html