C#for语句之冒泡法基础练习

@作者: 风不停息丶
在这里插入图片描述


练习1:for循环打印正三角图案

        #      1
        ##     2
        ###    3
        ####   4
class Program
{
    static void Main(string[] args)
    {
        for (int r = 0; r < 4; r++)    // 0  1  2   3
            {
                for (int i = 0; i <= r; i++)//0 01 012 0123
                {
                    Console.Write("#");
                }
                Console.WriteLine();
            }
    }
}

练习2:for循环打印倒三角图案

		####   4
         ###   3
          ##   2
           #   1
class Program
{
    static void Main(string[] args)
    {
		for (int i = 0; i < 4; i++)
            {
                for (int c = 0; c < i; c++)
                {
                    Console.Write(" ");
                }
                for (int a = 4; a > i; a--)
                {
                    Console.Write("#");
                }
                Console.WriteLine();
            }
    }
}

练习3:冒泡排序

输入一段数字进行排序

//冒泡排序
private static int[] OrderBy(int[] array)
        {
            //需要与后面比较的元素索引
            //只需要对比到倒数第二位,array。Length-1
            for (int currentIndex = 0; currentIndex < array.Length - 1; currentIndex++)
            {   
                for (int i = currentIndex + 1; i < array.Length; i++)//
                {
                    if (array[currentIndex] > array[i])//第二位和第一位比较,如果大于就交换
                    {
                        //交换方法
                        int temp = array[currentIndex];//定义一个变量 将array[currentIndex]里的数据赋值给变量temp
                        array[currentIndex] = array[i];//将array[currentIndex]里的数据换成array[i]的数据
                        array[i] = temp;//将变量temp的数据赋值给array[i]
                    }
                }                  
            }
            return array;
        }

练习4:选择冒泡排序


private static int[] OrderBy(int[] array)//定义数组方法int[]
        {
            //需要与后面比较的元素索引
            //比较3次 0 1 2
            
			//当前指数,第0个数拿去初始比较,所以array.length-1
            for (int currentIndex=0;currentIndex<array.Length-1;currentIndex++)
            {         
                int minIndex = currentIndex;//设置一个变量 = currentIndex
                for (int i=currentIndex+1;i<array.Length;i++)
                { 
                    if (array[currentIndex] > array[i])//比较  交换
                    {
                        //需要记录的索引
                        if (array[minIndex] > array[i])
                        {
                            minIndex = i;
                        }
                    }
                }
                if (minIndex != currentIndex)//如果上面循环结束之后没有相同,那么就执行
                {
                    int temp = array[currentIndex];
                    array[currentIndex] = array[minIndex];
                    array[minIndex] = temp;
                }
            }
            return array;
        }

练习5:冒泡查重

定义检查数组中是否存在相同元素的方法

private static bool IsRepeating1(int[] array)
        {
            //定义检查数组中是否存在相同元素的方法int[]
            //5   3   1   3   8
            for (int i=0;i<array.Length-1;i++)
            {
                for (int a=i+1;a<array.Length;a++)
                {
                    if (array[a] == array[i])
                    {
                        return true;
                    }
                }
            }
            return false;
        }
  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风不停息丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值