快速排序(拔萝卜法,实质就是一直否定寻找数当前的位置,一个萝卜,一个坑)...

本文介绍了一个使用C#实现的快速排序算法。通过递归方式,选取基准元素并对数组进行分区,确保基准元素左边的元素都小于它,右边的元素都大于它。此过程重复应用于左右两个子数组,直至整个数组有序。

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

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

namespace QuickSort
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>() { 1, 1, 28, 14, 10, 67, 22, 90, 55, 100, 120, 140, 1, 12 };
            int low = 0;
            int high = list.Count - 1;
            int mid = (low + high) / 2;
            QuickSortArr(list, low, high);
            list.ForEach(a => { Console.WriteLine(a.ToString()); });
            Console.ReadKey();
        }
        public static void QuickSortArr(List<int> list, int low, int high)
        {
            //用来结束递归调用
            if (low >= high)
            {
                return;

            }
            int start = low;
            int end = high;

            int mid = (low + high) / 2;

            int findNum = list[mid];
            //tag=0是初始化 tag用来标示要定位的数字的可能的位置
            int tag = 0;
            tag = mid;
            while (low<high)
            {
               
                while (findNum <= list[high] && tag < high)
                {
                    high--;

 

                }

                //寻找的数字的可能的位置每次都在变化,不管是后面找小的还是前面找大的tag=high(定位可能的位置,tag=high是在更新可能的位置)
                if (findNum > list[high])
                {
                    list[tag] = list[high];
                    tag = high;
                }
                while (findNum >= list[low] && tag > low)
                {
                   
                    low++;


                }
                //寻找的数字的可能的位置每次都在变化,不管是后面找小的还是前面找大的tag=low(定位可能的位置,tag=low是在更新可能的位置)
                if (findNum < list[low])
                {
                    list[tag] = list[low];
                    tag = low;
                }
        
            }
            //low==high的时候说明前面都是比被寻找数小的,后面都是比被寻找数大的,说以low==high的下标就是最终数的下标
           
         
            list[low] = findNum;

            QuickSortArr(list, start, low - 1);
            QuickSortArr(list, low + 1, end);


        }
    }
}

转载于:https://www.cnblogs.com/kexb/p/5042975.html

这是一段C++代码: ``` #include <iostream> #include <vector> class Carrot { public: Carrot(int num) : number(num) {} int getNumber() const { return number; } private: int number; }; class Rabbit { public: Rabbit(int num) : number(num) {} int getNumber() const { return number; } void eatCarrot(std::vector<Carrot> &carrots) { int count = 0; for (int i = 0; i < carrots.size(); i++) { if (count >= number) { break; } carrots[i].getNumber(); count++; } std::cout << "Rabbit ate " << count << " carrots." << std::endl; } void pullCarrot(std::vector<Carrot> &carrots) { int count = 0; for (int i = 0; i < carrots.size(); i++) { if (count >= number) { break; } carrots.erase(carrots.begin() + i); count++; i--; } std::cout << "Rabbit pulled " << count << " carrots." << std::endl; } private: int number; }; int main() { std::vector<Carrot> carrots; for (int i = 0; i < 10; i++) { carrots.push_back(Carrot(i)); } Rabbit rabbit(5); rabbit.eatCarrot(carrots); rabbit.pullCarrot(carrots); return 0; } ``` 这段代码定义了两个类:`Carrot`和`Rabbit`。`Carrot`类表示萝卜,有一个量属性`number`。`Rabbit`类表示兔子,也有一个量属性`number`。 我们使用了`vector`容器存储萝卜量,在程序的主函中,我们创建了10个萝卜并存储在`vector`容器中。然后,我们创建了一个兔子对象,并调用了两个函:`eatCarrot`和`pullCarrot`,分别表示兔子吃萝卜拔萝卜。 在这两个函中,我们通过循环语句
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值