递归算法和快速排序

本文深入探讨了递归算法的应用实例,通过计算正方形边长的递归过程展示其工作原理。同时,介绍了选择排序的实现方式,并通过具体代码演示了如何进行列表排序。此外,还讲解了快速排序算法,提供了详细的代码实现与步骤说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

递归算法和选择排序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] mmm = new int[] { 1, 23, 4, 5, 6, 677, 65, 67 };
            List<int> mmn =   mmm.ToList();
            SortGet(mmn);
            sunList(mmn);
            getrec(1680, 640);
            Console.WriteLine(result);
        }
       static double  result = 0;
        static int SumResult = 0;
        static void   getrec(double length , double width)
        {
            Console.WriteLine("递归计算正方形一次");
            if (length > width)
            {
                Console.WriteLine("得到一个正方形边长为:"+ width);
                getrec(width, length - width);
                                     
            }
            else if (length < width)
            {
                Console.WriteLine("得到一个正方形边长为:" + length);
                getrec(length, width - length );
             
            }
            else
            result = length;


        }
        static void  sunList ( List<int >  mm)
        {
            Console.WriteLine("递归计算和一次");
            if (mm.Count>0)
            {
                
                SumResult = SumResult + mm[0];
                mm.Remove(mm[0]);
                sunList(mm);
            }
            else
            Console.WriteLine("得到结果:"+SumResult);
            ;
        }
        //快速排序
        static void SortGet(List<int> mm)
        {
            Console.WriteLine("快速排序一次");
            if (mm.Count < 1)
                return;
            else
            {
                int tt = mm[0];
                List<int> maxList = new List<int>();
                List<int> minList = new List<int>();

                foreach (var i in mm)
                {
                    if (i > tt)
                        maxList.Add(i);
                    else if (i < tt)
                        minList.Add(i);

                }
                SortGet(minList);
                Console.WriteLine(tt);
                SortGet(maxList);
              
            }
        }
    }
}

结果在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值