Simple bubble sort of resolve

What is a bubble sort

A bubble sort is simple, the random array of elements within an ordered arrangement by pairwise comparison element.

1. Principle: To compare two adjacent elements, will exchange a large element values ​​to the right

2. The idea: turn compare two adjacent numbers, the relatively small number on the front, a relatively large number on the back.
(1) first comparison: firstly comparing the first and second number, the numbers are placed in front of the large numbers on the back. (2) comparing the number 2 and 3, the numbers are placed ahead of large numbers on the back.
  ...

(3) This continues to the last known comparison two numbers, the numbers are placed ahead of large numbers on the back, repeating steps until all of the sort is complete
(4) after completion of a trip comparator in the above, a certain number of the last it is the largest of a number of the array, so the comparison of the second trip, when the last number is not participating in the comparison.
(5) after comparing the second pass is completed, the number must be the inverse of the second array penultimate large number, comparing the third times, the last two numbers are not involved in the comparison.
(6) and so on, in order to reduce the number of comparisons per trip

Man of few words said, look at the code

 public class PaixuTest {

	public static void main(String[] args) {
		// 创建一个无序的数组
		int[] a = {10,1,35,61,89,36,55};
		sort(a);
		System.out.println(Arrays.toString(a));
	}

	// 创建排序的方法
	public static void sort(int[] a) {
		int temp;
		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < a.length - 1 - i; j++) {
				if (a[j] > a[j + 1]) {
					temp = a[j];
					a[j] = a[j + 1];
					a[j + 1] = temp;
				}
			}
		}
	}
}

We can see the array to be sorted is
(1) int [] = {A} 10,1,35,61,89,36,55;
(2) first pass sort:
first sort: 10 and Comparison 1 10 is greater than 1, the exchange position [1,10,35,61,89,36,55]
second Sort: 10 and comparator 35, 10 is less than 35, without exchanging the position [1,10,35,61,89, 36, 55]
third Sort: 35 and comparator 61, 35 is less than 61, without exchanging the position [1,10,35,61,89,36,55]
fourth Sort: 61 and comparator 89, 61 is less than 89, without exchanging the position [1,10,35,61,89,36,55] fifth Sort: 89 and comparator 36, 89 is greater than 36, the exchange position [1,10,35,61,36,89,55]
of six sort: 89 and comparator 55, 89 is greater than 55, the exchange position [1,10,35,61,36,55,89]
first pass total of six comparison, sorting result: [1,10,35, 61,36,55,89]
(3) times a second sorting
 first sort: 1 and 10 compared to less than 1 10, without exchanging the position [1,10,35,61,36,55,89]
 second Sort: 10 and comparator 35, 10 is less than 35, without exchanging the position [1,10,35,61,36,55,89]
 third Sort: 35 and comparator 61, 35 is less than 61, without exchanging the position [1,10 , 35,61,36,55,89]
 fourth Sort: 61 and comparator 36, 61 is greater than 36, the switching position of [1 , 10,35,36,61,55,89]
 Fifth Sort: 61 and comparator 55, 61 is greater than 55, the exchange position [1,10,35,36,55,61,89]
The second trip total of five times compared sorting result: [1,10,35,36,55,61,89]
(4) Third Sort times:
first sort: 1 and 10 compared to less than 1 10, without exchanging the position [1,10,35,36,55,61,89]
 second Sort: 10 and comparator 35, 10 is less than 35, without exchanging the position [1,10,35,36,55,61,89]
 third Sort: 35 and comparator 36, 35 is less than 36, without exchanging the position [1,10,35,36,55,61,89]
 fourth Sort: 36 and comparator 61, 36 is less than 61, without exchanging the position of [ 1,10,35,36,55,61,89]
the third trip was a total of four times compared Sort results: [1,10,35,36,55,61,89]
 until now already ordered the situation.

Figure understand

Here Insert Picture Description

4. Analysis of Algorithms:

(1) shows that: N numbers to sort completed, a total of N-1 times sort, every sort of pass i is the number (Ni) times, it is possible to use a double loop, how many times the outer control loop, the inner controlling the number of cycles each pass

Bubble sort of advantage

Each Perform a sort, once the comparison will be less, because every sort will be a trip to find a great value. Example above: after the first pass compared to the last row of a certain number is a maximum number, the second pass when sorting, among others need only compare the number of a number other than the last, you can also find a maximum the number of rows in the back several times to participate in the second comparison, when comparing the third trip, need only compare the number in addition to other than the last two numbers, and so on ...... in other words, did not carry out a trip to compare every a trip to a relatively small, to some extent, reduce the amount of the algorithm.
Released two original articles · won praise 11 · views 324

Guess you like

Origin blog.csdn.net/qq_43402310/article/details/103665085