C language selection bubble sorting explanation (with code)

C language selection bubble sorting explanation (with code)

An example of the principle of bubble sorting :
Given a set of numbers 15 20 1 16 to bubble sort from large to small. The first comparison of the first blistering: compare 15 and 20. If 15 is smaller than 20, switch positions. At this time, the number position is 20 15 1 16. The second comparison of the first blistering: use 15 and 1 comparison, if 15 is less than 1, then swap positions, 15 is not less than 1, then the position remains unchanged, at this time the number position is 20 15 1 16, the third comparison of the first bubbling: compare 1 and 16, if If 1 is smaller than 16, the positions are exchanged. At this time, the number position is 20 15 16 1. The four numbers need to be compared, and a total of 3 times are compared. The first comparison of the second bubbling: compare 20 and 15, if 20 is not less than 15, the position remains the same. At this time, the number position is 20 15 16 1. The second comparison of the second bubbling: use Compare 15 and 16, if 15 is smaller than 16, then swap positions, 15 is smaller than 16, then swap positions. At this time, the number position is 20 16 15 4. The three numbers need to be compared. A total of 2 comparisons are made. And so on.
An example of the principle of selection sorting :
Given a set of numbers 15 20 1 16 to sort from small to large. The first selection and sorting: swap the smallest of the four numbers with the first number, and the position of the number is 1 20 15 16 at this time, the second selection and sorting: the smallest of the three numbers and the second number Exchange, the position of the number is 1 15 16 20 at this time, and the third selection sort: exchange the smallest number of the two numbers with the third number, and the position of the number is 1 15 16 20 at this time. The four numbers were selected and sorted three times.
The specific code is as follows :

#include <stdio.h>
#define LEN 10//宏定义LEN为10
void sort(int array[], int n);//函数声明
void buble(int array[], int n);

void main()
{
    
      
	int a[LEN], i;
	printf("\n请输入%d个无序整数:\n",LEN);
	for(i=0;i<LEN;i++)  scanf("%d",&a[i]);
	printf("\n开始选择排序\n");
	sort(a,LEN);
	printf("从小到大选择排序结果:\n");
	for(i=0;i<LEN;i++)  printf("%d ",a[i]);
	printf("\n");
	printf("\n开始冒泡排序\n");
	buble(a,LEN);
	printf("从大到小冒泡排序结果:\n");
	for(i=0;i<LEN;i++)  printf("%d ",a[i]);
	printf("\n");
}
//选择排序
void sort(int array[], int n)
{
    
      
	int i,j,k,t;
    for(i=0;i<n-1;i++)
    {
    
      
		k=i;
        for(j=i+1;j<n;j++)
		{
    
    
			if(array[j]<array[k])
			{
    
    
				k=j;
			}
		}
		t=array[k]; 
		array[k]=array[i]; 
		array[i]=t;
   }
}
//冒泡排序
void buble(int array[], int n){
    
    
	int i, j, t;
	for (i=0; i<n-1; i++)
	{
    
    
		for (j=0; j<n-1-i; j++)
		{
    
    
             if (array[j]<array[j+1])
			 {
    
    
				 t=array[j];
				 array[j]=array[j+1];
				 array[j+1]=t;
			 }
		} 

	}
}
/*测试用例:
3 5 8 11 15 20 1 4 77 109

*/


Execution result :
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_46020391/article/details/109401029