The example of this article tells the bubbling sort concluded by the Java sorting algorithm. Share it for everyone for your reference. The specific analysis is as follows:
Foreword: Bubblesort is two adjacent numbers in turn, put the decimal in front, and the large number is behind.
Let's take a look at the algorithm implementation of bubbling sorting in Java.
Bubble sorting is a method of sorting the computer. Its time complexity is O (N^2). Although it is not as good as the sorted and fast sorting O (NLOGN, the bottom number is 2), there are two advantages:
1. "Programming complexity" is very low, and it is easy to write code;
2. It has stability. The stability here refers to the relative order of the same elements in the original sequence that remains to the sequence that is sorted, while the stack sorting and fast sorting are not stable.
However, the sorting and unbalanced binary tree sorting all the way and second roads are faster than bubbling sorting, and they are stable, but the speed is not as good as stacking sorting.
Quickly sort. Bubble sorting is completed by n-1 trips. The sort of the first trip from the first number from the first to the number Ni is large. Two numbers.
Bubble sorting algorithms are stable, and the extra space of O (1), comparison and exchange time complexity are O (n^2), adaptive, for the basic sorting algorithm, time complexity is O (n). The nature of the bubble algorithm is similar to that of the insertion algorithm, but a little higher for the system overhead.
Sorting process
Imagine the sorted array R [1..N] vertically erected, and each data element is regarded as a heavy bubble. According to the principle of light bubbles that cannot be under heavy air bubbles, scan the array R from the bottom to the top. The light bubble that violates this principle will "float" upward, so repeatedly, until the last two bubbles are light ones, and those who are heavy until the bottom.
Code implementation:
// Bubble class bubblesort {public static void sort (comparable [] data) {// array length int lan = data.length; for (int i = 0; i <len- 1; i ++) {// temporary Variable comparable test = null; // exchange logo, false indicates that the BOOLEAN ISEXCHANGED = false; for (int j = Len -1; j> J-) {// If data [j] smaller than data [j - 1], exchange if (data [j] .compareto (data [j -1]) <0) {test = data [j]; data [j] = data [j - 1]; data [j - 1] = Temp; // The exchange occurs, so the exchange logo is set to real isexchanged = true;} // end if} // End for // The sorting of this trip has not been exchanged. Early termination algorithm to improve efficiency if (! Isexchanged) { Return;} // End IF} // End for} // End Sort Public Static Void Main (String [] ARGS) {// above JDK1.5, the basic data type can be automatically packed // int, double, etc. The basic type of packaging class has implemented the comparable interface comparable [] c = {4, 9, 23, 23, 45, 27, 5, 2}; sort (c); for (comparable data: c) {system.out. Println (data);}}}The use of bubbling sorting method to sort n data, and a total of N-1 comparison is required. If it is originally a order of data, N-1 comparison is needed. The algorithm of the bubbling sorting method is very simple and the efficiency is poor.
It is hoped that this article is helpful to everyone's Java program design.