C language - bubble sort (detailed analysis)

Bubble Sort

Introduction to Sorting Algorithms

Sorting is also called sorting algorithm (Sort Algorithm). Sorting is the process of arranging a set of data in a specified order.

sorted categories

(1) Internal sorting:

Refers to loading all the data that needs to be processed into **internal storage (memory)** for sorting.

(2) External sorting method:

The amount of data is too large to be fully loaded into memory, and external storage is required for sorting.

[key] bubble sort

1. Basic introduction

The basic idea of ​​bubble sorting (Bubble Sorting) is: by treating the sorting sequence from front to back (starting from the element with a smaller subscript), compare the values ​​​​of adjacent elements in turn, and exchange if the reverse order is found, so that the element with a larger value Gradually move from the front to the back, and gradually rise up like bubbles under the water.

Because during the sorting process, each element is constantly approaching its own position. If there is no exchange after a comparison, it means that the sequence is in order . Therefore, a flag should be set during the sorting process to determine whether the element has been exchanged. Thereby reducing unnecessary comparisons.

2. Application examples of bubble sorting

Arrange five unordered numbers: {3, 9, -1, 10, -2} into an ordered sequence from small to large using the bubble sort method.

3. Analyze the bubbling process + code

image-20221027211306669

Code:

#include <stdio.h>
int main(){
    
    
	
	int arr[] = {
    
    3,9,-1,10,-2};
	
	//第一轮排序
	
	int j;
	int t;//临时变量
	for(j=0;j<4;j++){
    
    
		
		//如果前面的数大于的后面的数,就交换
		if(arr[j]>arr[j+1]){
    
    
			t = arr[j];
			arr[j] = arr[j+1];
			arr[j+1] = t;
		}
	}
	//输出看看第一轮的排序后的情况
	for(j=0;j<5;j++){
    
    
		printf(" %d",arr[j]);	
	}
	printf("\n");
	//第二轮排序
	for(j=0;j<3;j++){
    
    
		
		//如果前面的数大于的后面的数,就交换
		if(arr[j]>arr[j+1]){
    
    
			t = arr[j];
			arr[j] = arr[j+1];
			arr[j+1] = t;
		}
	}
	//输出看看第二轮的排序后的情况
	for(j=0;j<5;j++){
    
    
		printf(" %d",arr[j]);	
	}
	printf("\n");
	
	//第三轮排序
	for(j=0;j<2;j++){
    
    
		
		//如果前面的数大于的后面的数,就交换
		if(arr[j]>arr[j+1]){
    
    
			t = arr[j];
			arr[j] = arr[j+1];
			arr[j+1] = t;
		}
	}
	//输出看看第三轮的排序后的情况
	for(j=0;j<5;j++){
    
    
		printf(" %d",arr[j]);	
	}
	
	printf("\n");
	//第四轮排序
	for(j=0;j<1;j++){
    
    
		
		//如果前面的数大于的后面的数,就交换
		if(arr[j]>arr[j+1]){
    
    
			t = arr[j];
			arr[j] = arr[j+1];
			arr[j+1] = t;
		}
	}
	//输出看看第四轮的排序后的情况
	for(j=0;j<5;j++){
    
    
		printf(" %d",arr[j]);	
	}
	return 0;
}

Because each round of sorting is almost the same, we can use a for loop to simplify the code, and at the same time define the variable of the array size, arrLen, to make the code more flexible

#include <stdio.h>
int main(){
    
    
	
	int arr[] = {
    
    3,9,-1,10,-2};
	
	//因为每轮排序几乎一样,因此,我们可以使用for循环处理
	
	//第一轮排序
	
	int j,i;
	int t;//临时变量
	int arrLen = sizeof(arr) / sizeof(int); // 5 数组大小
	for(i=0;i<arrLen-1;i++){
    
    
		for(j=0;j<arrLen-1-i;j++){
    
    		
		//如果前面的数大于的后面的数,就交换
		if(arr[j]>arr[j+1]){
    
    
			t = arr[j];
			arr[j] = arr[j+1];
			arr[j+1] = t;
			}
		}
		//输出看看第一轮的排序后的情况
		for(j=0;j<arrLen;j++){
    
    
			printf(" %d",arr[j]);	
		}
		printf("\n");	
	}
	return 0;
}

Because it is too troublesome to repeat the code every time, the above code can be encapsulated into a function of bubble sorting:

#include <stdio.h>
//冒泡排序的函数
void bubbleSort(int arr[],	int arrLen){
    
    
	
	int j,i;
	int t;//临时变量	
		//因为每轮排序几乎一样,因此,我们可以使用for循环处理
	for(i=0;i<arrLen-1;i++){
    
    
		for(j=0;j<arrLen-1-i;j++){
    
    		
		//如果前面的数大于的后面的数,就交换
		if(arr[j]>arr[j+1]){
    
    
			t = arr[j];
			arr[j] = arr[j+1];
			arr[j+1] = t;
			}
		}	
	}
}

int main(){
    
    
	
	int j;
	int arr[] = {
    
    3,9,-1,10,-2};
	int arrLen = sizeof(arr) / sizeof(int); // 5 数组大小
	bubbleSort(arr,arrLen);//数组默认是地址传递 (指针)
	printf("\n排序后(函数)\n");
		for(j=0;j<arrLen;j++){
    
    
			printf(" %d",arr[j]);	
		}

	return 0;
}

Guess you like

Origin blog.csdn.net/m0_53415522/article/details/127566627