Bubble sorting principle and C++ source code implementation


1. Principle

Compare two adjacent numbers and swap the larger value to the right.


2. Thinking

Compare two adjacent numbers in turn, placing the smaller number on the left and the larger number on the right. Specific steps are as follows:

  1. In the first comparison, the first number is compared with the second number. The smaller number is placed on the left and the larger number is placed on the right.
  2. The second comparison is to compare the second number with the third number, with the smaller number on the left and the larger number on the right.
  3. For the third comparison, compare the third number with the fourth number. The smaller number is placed on the left and the larger number is placed on the right.
  4. The comparison is continued until the last two data comparisons are completed, and then a round of sorting is completed. The last number is the largest number in the array. When performing the next round of comparison, the last value does not need to participate in the comparison.
  5. The next round of comparisons will continue to be sorted according to the above rules. After each round of sorting is completed, the number of values ​​participating in the next round of sorting will be one less.

Example:
Bubble sort the data [1, 4, 2, 6, 7, 3, 5, 10, 9].

  1. The first round of sorting
    1) The first sorting: 1 and 4 are compared, 1 is smaller than 4, no need to exchange, get the array [1, 4, 2, 6, 7, 3, 5, 10, 9]
    2) The second sorting : Comparison of 4 and 2, 4 is greater than 2, exchange positions, get the array [1, 2, 4, 6, 7, 3, 5, 10, 9]
    3) The third sort: 4 and 6, 4 to 5 Small, no need to exchange, get the array [1, 2, 4, 6, 7, 3, 5, 10, 9]
    4) The fourth sort: 6 and 7, 6 is smaller than 7, no need to exchange, get the array [1 , 2, 4, 6, 7, 3, 5, 10, 9]
    5) The fifth order: 7 and 3 are compared, 7 is greater than 3, exchange positions, get the array [1, 2, 4, 6, 3, 7, 5, 10, 9]
    6) The sixth order: 7 and 5 are compared, 7 is greater than 5, exchange positions, get the array [1, 2, 4, 6, 3, 5, 7, 10, 9]
    7 ) Seventh sorting: 7 and 10 comparison, 7 is smaller than 10, no need to exchange, get data [1, 2, 4, 6, 3, 5, 7, 10, 9]
    8) Eighth sorting: 10 and 9 Comparison, 10 is greater than 9, exchange positions, get data [1, 2, 4, 6, 3, 5, 7, 9, 10] There
    are 8 rounds of sorting in this round, and the sorting result: [1, 2, 4, 6, 3, 5, 7, 9, 10]

  2. Second round of sorting The
    second round of sorting data is based on the sorting result of the previous round. Since the rightmost value of the previous round is the maximum value, this round of sorting does not need to be compared with the previous round of maximum value.
    1) The first sorting: 1 and 2 are compared, 1 is smaller than 2, no need to exchange, get the array [1, 2, 4, 6, 3, 5, 7, 9, 10]
    2) The second sorting: 2 and 4 comparison, 2 is smaller than 4, no need to exchange, get the array [1, 2, 4, 6, 3, 5, 7, 9, 10]
    3) The third sort: 4 and 6, 4 is smaller than 6, no need Exchange, get the array [1, 2, 4, 6, 3, 5, 7, 9, 10]
    4) Fourth sort: compare 6 and 3, 6 is greater than 3, swap positions, get the array [1, 2, 4, 3, 6, 5, 7, 9, 10]
    5) The fifth order: 6 and 5 comparison, 6 is greater than 5, exchange positions, get the array [1, 2, 4, 3, 5, 6, 7 , 9, 10]
    6) The sixth sorting: 6 and 7, 6 is smaller than 7, no need to exchange, get the array [1, 2, 4, 3, 5, 6, 7, 9, 10]
    7) seventh Sub-sorting: 7 and 9, 7 is smaller than 9, no position is needed, and the array [1, 2, 4, 3, 5, 6, 7, 9, 10] is
    sorted in this round, and the result of the sort: [ 1, 2, 4, 3, 5, 6, 7, 9, 10]

  3. Continue to the next round of sorting...


Three, bubble sorting characteristics

  • N number sorting, a total of N-1 rounds of sorting, each i-round sorting times is (Ni) times, so when the source code is implemented, you can use a double loop, the outer loop controls how many rounds of the loop, and the inner loop controls the number of each Number of comparisons.

  • Advantages: Every time a round of sorting is performed, there will be one less comparison, because no round of sorting will find a larger value. Reduce the amount of algorithms to a certain extent.

  • Time complexity
    1) If the data is in positive order, it only takes one round to complete the sorting. The number of comparisons C and the number of recording moves M both reach the minimum, that is, Cmin=n-1, Mmin=0, so the minimum time complexity is O(n).
    2) If the data is in reverse order, n-1 rounds of sorting are required. Each round of sorting requires ni comparisons, and each comparison must move the record three times to reach the exchange record position. The maximum time complexity is:

Insert picture description here
Therefore, the total average time complexity of bubble sort is: O(n2), and the time complexity has nothing to do with the data status.


Four, C++ source code implementation

Brief summary: Two loops are done.

/*
1.从当前元素起,向后依次比较每一对相邻元素,若逆序则交换
2.对所有元素均重复以上步骤,直至最后一个元素
*/
void buddleSort(int arr[], int len)
{
    
    
	int temp = 0;
 	int i, j;

	for(i = 0; i < len-1; i++) //外循环为轮数
	{
    
    
		for(j=0; j < len-1-i; j++) //内循环为每轮比较次数,第i轮比较len-i次
		{
    
    
			//相邻元素,若逆序则交换(升序为左大于右,降序反之)
			if(arr[j] > arr[j+1]) 
			{
    
    
				temp = arr[j];
				arr[j] = arr[j+1];
				arr[j+1] = temp;
			}
		}	
	}
}

Guess you like

Origin blog.csdn.net/locahuang/article/details/110166279