Bubble sort _java of Java sort algorithm summary

Source: Internet
Author: User
Tags array length comparable

This article illustrates the bubbling sort of Java sort algorithm summary. Share to everyone for your reference. The specific analysis is as follows:

Bubble Sort (bubblesort) is to compare the adjacent two numbers sequentially, place the decimal number in the front, and the large numbers in the back.

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

Bubble sort is a sort of computer that has a time complexity of O (n^2), although it is less than a heap-sorted, fast-sorted O (Nlogn, base 2), but has two advantages:

1. "Complexity of programming" is very low, it is easy to write code;
2. Stability, where the stability of the original sequence of the same elements is still maintained to the order of the sequence, and the heap sorting, fast sorting are not stable.

However, all the way, two merge sort, unbalanced binary tree sorting speed is faster than bubble sort, and has stability, but the speed is less than the heap sorting,
Quick Sort. The bubble sort is done by n-1, and the number of the I-bus is sorted from 1th to N-i, if the number of I is greater than the latter
(then ascending, small, descending) swaps the two number.

Bubble sort algorithm stability, O (1) of additional space, comparison and exchange of time complexity are all O (n^2), adaptive, for the algorithm has been basically sorted, time complexity of O (n). Many of the properties of the bubbling algorithm are similar to the insert algorithm, but a little bit higher for the system overhead.

Sorting process

Imagine the array r[1 being sorted. N] vertically erect, each data element as a bubble of weight, according to the principle that the light bubbles cannot be under the heavy bubbles, scan the array r from the bottom up and scan the light bubbles that violate this principle so that they "float" upwards, so that the last two bubbles are light, and the heavy is the next.

Code implementation:

Bubble sort public class bubblesort{public static void sort (comparable[] data) {//array length int len = data.length; 
      for (int i = 0; i < len-1 i++) {//temporary variable comparable temp = NULL; 
      Swap flag, False indicates not exchanged 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; 
        An exchange is made, so the Exchange flag is set to true isexchanged = true; 
      }//End If}//ends for//This trip sort does not occur swapping, advance termination algorithm, increase efficiency if (!isexchanged) {return;
    }//End If}//end to}//end sort public static void main (string[] args) {//In JDK1.5 version, basic data types can be boxed automatically
    Int,double and other basic types of wrapper classes 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 the bubble sort method to sort n data, a total of n-1 comparisons are required. If there is a sequential data, it is also necessary to perform a n-1 comparison. The bubble sort algorithm is simple and inefficient.

I hope this article will help you with your Java programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.