当前位置: 移动技术网 > IT编程>开发语言>c# > c# 冒泡排序算法(Bubble Sort) 附实例代码

c# 冒泡排序算法(Bubble Sort) 附实例代码

2019年07月18日  | 移动技术网IT编程  | 我要评论
冒泡排序(bubble sort) 冒泡排序算法的运作如下: 1.比较相邻的元素。如果第一个比第二个大,就交换他们两个。2.对每一对相邻元素作同样的工作,从开始第一对到

冒泡排序(bubble sort)

冒泡排序算法的运作如下:

1.比较相邻的元素。如果第一个比第二个大,就交换他们两个。
2.对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。在这一点,最后的元素应该会是最大的数。
3.针对所有的元素重复以上的步骤,除了最后一个。
4.持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。

平均时间复杂度 o(n^2)

冒泡排序

复制代码 代码如下:

/// <summary>
/// 冒泡排序
/// </summary>
/// <param name="arr"></param>
/// <param name="count"></param>
public static void bubblesort(int[] arr, int count)
{
    int i = count, j;
    int temp;
    while (i > 0)
    {
        for (j = 0; j < i - 1; j++)
        {
            if (arr[j] > arr[j + 1])
            {
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
        i--;
    }
}

 

 
   //使用例子
   int[] y = new int[] { 1, 32, 7, 2, 4, 6, 10, 8, 11, 12, 3, 9, 13, 5 };
    bubblesort(y,  y.length );
    foreach (var item in y)
    {
        console.write(item+" ");  
    }
    //1 2 3 4 5 6 7 8 9 10 11 12 13 32

简单且实用的冒泡排序算法的控制台应用程序。运行界面如下:


复制代码 代码如下:

using system;
using system.collections.generic;
using system.linq;
using system.text;

namespace 冒泡排序
{
    class program
    {
        /// <summary>
        /// 交换两个整型变量的值
        /// </summary>
        /// <param name="a">要交换的第一个整形变量</param>
        /// <param name="b">要交换的第一个整形变量</param>
        private static void reverse(ref int a, ref int b)
        {
            int temp = a;
            a = b;
            b = temp;
        }

        static void main(string[] args)
        {
            while (true)
            {
                string[] strinput;//用来接收用户输入的字符串
                int[] intinput;
                string[] separator = { ",", " " };//设置分隔符
                console.writeline("请输入数据,以\",\"或空格分隔,或按\"q\"退出。");
                string str = console.readline();//接收键盘输入
                if (str == "q")
                {
                    return;
                }
                strinput = str.split(separator, stringsplitoptions.removeemptyentries);//将用户输入的字符串分割为字符串数组
                intinput = new int32[strinput.length];

                //将字符串数组的每一个元素转换为整型变量
                //转换时如果出现格式错误或溢出错误则提示
                try
                {
                    for (int i = 0; i < strinput.length; i++)
                    {
                        intinput[i] = convert.toint32(strinput[i]);
                    }
                }
                catch (formatexception err)
                {
                    console.writeline(err.message);
                }
                catch(overflowexception err)
                {
                    console.writeline(err.message);
                }

                //排序算法主体
                for (int i = 0; i < intinput.length - 1; i++)//这里的length要减1否则会超界
                {
                    for (int j = 0; j < intinput.length - i - 1; j++)//这里的length要减i以减少重复运算
                    {
                        //如果元素j比它之后的一个元素大,则交换他们的位置
                        //如此循环直到遍历完整个数组
                        if (intinput[j] > intinput[j + 1])
                        {
                            reverse(ref intinput[j], ref intinput[j + 1]);
                        }
                    }
                }

                string stroutput = "";//用于输出的字符串
                foreach (int temp in intinput)
                {
                    stroutput += convert.tostring(temp) + ",";
                }
                console.writeline("排序后的数据为:\r\n{0}\r\n", stroutput);
            }
        }
    }
}

 

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网