首页 > 其他分享 >BubbleSort

BubbleSort

时间:2024-05-01 22:12:10浏览次数:10  
标签:int BubbleSort ++ bufsize buf 163

BubbleSort

/************************************************************
*    file name:    BubbleSort
*    author   :    [email protected]
*    date     :    2024/05/01
*    function :    BubbleSort
*    note     :    
*
*    CopyRight (c)  2023-2024  [email protected]  All Right Reseverd
*
* ***********************************************************/

#include <stdio.h>

/***********************************************************
*
*    函数名称:       BubbleSort
*    函数功能:       BubbleSort
*    函数参数:
*                @a :          
*                @b :
*    返回结果:
*    注意事项:       
*    函数作者:      [email protected] 
*    创建日期:      2024/05/01
*    修改历史:
*    函数版本:      V1.0
* ***********************************************************/
//冒泡排序,指的是元素两两之间进行比较交换,需要比较n轮,每轮需要比较m次,从左向
void BubbleSort(int buf[],int bufsize)
{
	int temp = 0;//为了临时存储交换值
	
	//1.循环比较元素,需要比较n轮
	for (int n = 1; n < bufsize; ++n)
	{
		//2.每轮需要比较m次
		for (int m = 0; m < bufsize-n; ++m)
		{
            //3.数组元素两两之间进行比较交换buf[0]buf[1] buf[1]buf[2]
			if (buf[m] > buf[m+1])
			{
				temp = buf[m];//备份前一个
				buf[m] = buf[m+1]; //把后面交换到前面
				buf[m+1] = temp;//把前面交换到后面
			}
		}
	}
	
   
	//遍历
	for (int n = 1; n < 10;++n)
	{
	printf("%d\n",buf[n]);
	}

}

标签:int,BubbleSort,++,bufsize,buf,163
From: https://www.cnblogs.com/xiaoyaoj/p/18169714

相关文章

  • c:BubbleSort
     #include<stdlib.h>#include<stdio.h>#include<string.h>typedefintElementType;int*BubbleSort(int*data,intlensize);intmain(){////SetConsoleOutputCP(65001);printf("helloword\n");printf("你......