Bubble sort summarized by the Java sorting algorithm

  • 2020-04-01 03:50:20
  • OfStack

This article illustrates the bubble sort summarized by Java sort algorithm. Share with you for your reference. Specific analysis is as follows:

Preface: BubbleSort is the process of comparing adjacent Numbers in turn, with decimals placed first and large Numbers placed last.

Let's get together       Let's look at the implementation of bubble sort in Java.

Bubble sort is a sort method of the computer, its time complexity is O (n^2), although not as good as heap sort, quick sort O (nlogn, base is 2), but has two advantages:

1. Low "programming complexity", easy to write code;
2. It has stability. The stability here refers to that the relative order of the same elements in the original sequence still remains to the sorted sequence, while heap sort and quicksort do not have stability.

However, the speed of one way merge sort, two ways merge sort and unbalanced binary tree sort is faster and more stable than bubble sort, but not as fast as heap sort and  .
Quicksort. Bubble sort is done by n-1 subsort, the ith subsort from the first number to the n-i number, if the ith number is larger than the last number
(then ascending, then descending) exchanges two Numbers.

Bubble sort algorithm is stable, O(1) of extra space, comparison and exchange time complexity is O(n^2), adaptive, for the basic sorted algorithm, time complexity is O(n). Many of the properties of the bubble algorithm are similar to the insertion algorithm, but slightly more expensive for systems.

Sorting process

Assumed that the array sorted R [1.. N] vertical erect, will have of the weight of each data element as a bubble, according to the principle of light bubbles cannot be under heavy bubble, scanning array R from down to up, all the scanning to the violation of the principle of light bubble, makes the "floating" upwards, so repeatedly, until the last any two bubbles is light person, the person that weigh in.

Code implementation:


//Bubble sort
public class BubbleSort{
  public static void sort(Comparable[] data){
    //The length of the array
    int len = data.length; 
    for (int i = 0; i < len - 1; i++){
      //Temporary variable
      Comparable temp = null; 
      //Switched flag,false means not switched
      boolean isExchanged = false; 
      for (int j = len - 1; j > i; j--){
        //If data[j] is less than data[j-1], swap
        if (data[j].compareTo(data[j - 1]) < 0){
          temp = data[j]; 
          data[j] = data[j - 1]; 
          data[j - 1] = temp; 
          //The swap occurs, so the swap flag is set to true
          isExchanged = true; 
        }// end if 
      }// end for 
      //This sort does not exchange, terminate the algorithm in advance, improve the efficiency
      if (!isExchanged){
        return; 
      }// end if 
    }// end for 
  }// end sort 
  public static void main(String[] args){
    //Basic data types can be autoboxed above JDK1.5
    //Wrapper classes of primitive types such as int,double, and so on have implemented the Comparable interface
    Comparable[] c = { 4, 9, 23, 1, 45, 27, 5, 2 };
    sort(c);
    for (Comparable data : c){
      System.out.println(data);
    }
  }
}

Using bubble sort to sort n data, a total of n-1 comparisons are needed. If it is already sequential data, it needs to be compared n-1 times. Bubble sort algorithm is very simple and inefficient.

I hope this article has been helpful to your Java programming.


Related articles: