Bubble SortBubble sort (Bubble sort) is a simpler sort algorithm in the field of computer science. It repeatedly visited the sequence to sort, comparing two elements at a time, and swapping them out if they were wrong in the order. The work of the sequence of visits is repeated until no more need to be exchanged, that is, the sequence is sorted. The name of the algorithm is because the larger the element will slowly "float" to the top of the sequence, hence the name. The algorithm principle of the bubble sorting algorithm operates as follows: (from back to forward)
- Compares the adjacent elements. If the first one is bigger than the second one, swap them both.
- Do the same for each pair of adjacent elements, starting with the last pair from the first pair to the end. At this point, the last element should be the maximum number.
- Repeat the above steps for all elements, except for the last one.
- Repeat the above steps each time for less and fewer elements until there are no pairs of numbers to compare
- Time complexity if the initial state of the file is a positive sequence, a scan can be done in order. The required number of keyword comparisons and record moves has reached the minimum value:. So, the best time to bubble sort is complexity.
If the initial file is reversed, a sequencing is required. The Sub-keyword comparison (1≤i≤n-1) is performed for each order, and each comparison must move the record three times to reach the Exchange record location. In this case, the comparison and the number of moves reach the maximum: The worst time complexity for the bubbling sort is. Overall, the average time complexity for the bubble sort is. The algorithm's stability bubble sort is to move the small element forward or the large element to be adjusted backwards. The comparison is an adjacent two element comparison, and the interchange also occurs between these two elements. So, if the two elements are equal, I think you will not be bored to exchange them again, if the two equal elements are not adjacent, then even through the preceding 22 exchange two adjacent together, this time will not be exchanged, so the same elements of the order has not changed, so bubble sort is a stable sorting algorithm.
Java implementations:
1 PackageCom.led.sort;2 3 Importjava.util.Arrays;4 5 /**6 * Bubble sort:digital from small to large7 * 8 * @authorLin_hero9 * Ten */ One Public classBubble001 { A - Public Static int[] Bubblesort (int[] a) { - the inttemp; - for(inti = 0; i < a.length; i++) { - for(intj = i + 1; J < A.length; J + +) { - if(A[i] >A[j]) { +temp =A[i]; -A[i] =A[j]; +A[J] =temp; A } at } - } - returnA; - } - - Public Static voidMain (string[] args) { in - int[] Array = {1,-6,-99, 67, 45, 8, 0,-90 }; toSystem.out.println ("Before bubble sort:"); + System.out.println (arrays.tostring (array)); - theBubble001.bubblesort (array);//invocation of a static method: Class name. Method Name *System.out.println ("After bubble sort:"); $ System.out.println (arrays.tostring (array));Panax Notoginseng - } the +}
Console output:
before bubble sort:[1,-6, -99,,, 8, 0, -90]after bubble sort:[-99,-90,-6, 0, 1, 8, 45, 67]
Sort the bubble sort of series