Bubble 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. Algorithm principle Analysis:
- 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 fewer elements, until there are no pairs of numbers to compare.
In general it is: starting from the first element of the array arr[0], 22 comparison (arr[n],arr[n+1]), if the preceding number is greater than the number of subsequent (Arr[n] > arr[n+1]), then the position of two elements is exchanged ,move the large number backwards. The largest number will be swapped to the last position (Arr[n-1]) after a round of comparisons in turn. Algorithm Demo: not sorted before: 5,9,8,23,2 first trip Sort: 5,8,9,2,23 The second trip sort: 5,8,2,9,23 the Third Order: 5,2,8,9,23 fourth order (after sorting): 2,5,8,9,23 Time complexity Analysis: The time complexity of the bubble sort is O (n^2), which is personally understood because there are two for loops that cause algorithm algorithm stability analysis: Bubble sort is to move the small element forward or the large element back. 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. Code implementation:
/** * @author Schrödinger's cat * java-bubble sort */public class Main {private static int[] arr = {5,9,8,23,2};p ublic static void Main (Strin G[] args) {bubblesort (arr);//Call Sort method GetResult ();//Call sort result}//core bubble sort method public static void Bubblesort (int[] arr) {/* Set flag, If the interchange does not occur when the first loop is compared, the array is * ascending, without sorting, directly ending the loop */boolean flag = false;for (int i = 0;i<arr.length-1;i++) {//Outer loop control loop count for ( Int J = 0;j<arr.length-1-i;j++) {//Inner loop controls the number of times each loop is compared if (arr[j]>arr[j+1]) {//swap array elements Note If descending is converted to < int temp = ARR[J];ARR[J] = arr[j+1];arr[j+1] = Temp;flag = true;}} if (flag==false) {System.out.println ("No Sort"); Package result private static void GetResult () {System.out.println ("After bubbling Sort:"); for (int i = 0;i<arr.length;i++) { System.out.print (arr[i]+ ",");}}}
java-Bubble Sorting algorithm