Recursive and non-recursive bubble sort (c language)

Sorting algorithm - bubble sort (recursive and non-recursive)


learning record

Bubble Sort is a relatively simple sorting algorithm in the field of computer science.
It repeatedly visits the column of elements to be sorted, compares two adjacent elements in turn, and exchanges them if the order (such as from large to small, initial letter from Z to A) is wrong. The work of visiting elements is repeated until no adjacent elements need to be exchanged, that is to say, the element column has been sorted.
The name of this algorithm comes from the fact that the smaller elements will slowly "float" to the top of the sequence (in ascending or descending order) through exchange, just like the bubbles of carbon dioxide in carbonated drinks will eventually float to the top, hence the name "bubbling". Sort".
(That is, the two smaller numbers are placed in front (in ascending order), and the larger ones are placed behind)
The principle of the bubble sort algorithm is as follows :
compare adjacent elements. If the first is bigger than the second, swap them both.
Do the same for each pair of adjacent elements, from the first pair at the beginning to the last pair at the end. At this point, the last element should be the largest number.
Repeat the above steps for all elements except the last one.
Continue repeating the above steps for fewer and fewer elements each time until there are no pairs of numbers to compare.

Non-recursive (double loop):

void bubblesort1(int a[],int n)//非递归双循环
{
    
    
	int i,j,ifswap=0;//定义是否交换的一个变量,如果提前排好序就结束排序。
	for(i=0;i<n-1;i++)
	{
    
    
		ifswap=0;
		for(j=0;j<n-1-i;j++)
		{
    
    
			if(a[j]>a[j+1])
			{
    
    swap(&a[j],&a[j+1]);ifswap=1;}
		}
		if(ifswap==0)break;//如果没有交换就表示已经排好序,直接结束排序
	}

recursion :

void bubblesort2(int a[],int n)//递归
{
    
    
	int i,ifswap=0;
	for(i=0;i<n-1;i++)
	{
    
    
		if(a[i]>a[i+1]){
    
    swap(&a[i],&a[i+1]);ifswap=1;}
	}
	if(ifswap==0)return;//如果没有交换,那就表明已排好序。
	bubblesort2(a,n-1);//每次排序其实会把最大的数放到最后,下一次排序只需要排前面的几项就可以了
}

Full code:

#include<stdio.h>

void swap(int *a,int *b)
{
    
    
	int temp;
	temp = *a;
	*a=*b;
	*b=temp;
}
void bubblesort1(int a[],int n)//非递归双循环
{
    
    
	int i,j,ifswap=0;//定义是否交换的一个变量,如果提前排好序就结束排序。
	for(i=0;i<n-1;i++)
	{
    
    
		ifswap=0;
		for(j=0;j<n-1-i;j++)
		{
    
    
			if(a[j]>a[j+1])
			{
    
    swap(&a[j],&a[j+1]);ifswap=1;}
		}
		if(ifswap==0)break;//如果没有交换就表示已经排好序,
		//直接结束排序
	}

}
void bubblesort2(int a[],int n)//递归
{
    
    
	int i,ifswap=0;
	for(i=0;i<n-1;i++)
	{
    
    
		if(a[i]>a[i+1]){
    
    swap(&a[i],&a[i+1]);ifswap=1;}
	}
	if(ifswap==0)return;//如果没有交换,那就表明已排好序。
	bubblesort2(a,n-1);//每次排序其实会把最大的数放到最后,下一次排序只需要排前面的几项就可以了
}

int main()
{
    
    
	int a[]={
    
    3,5,1,-7,4,9,-6,8,10,4};
	int n=(int)sizeof(a)/sizeof(int);
	int i;
	bubblesort1(a,n);//非递归双循环
	//bubblesort2(a,n);//递归
	for(i=0;i<n;i++)printf("%3d",a[i]);
	printf("\n");
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_44627822/article/details/121453742