对具有嵌套 For 循环的数字元组列表进行排序。.净重 4.0 C#

我有一个数字元组列表,并希望使用嵌套的For循环按Item1对其进行排序。我知道我可以只做List.Sort();但是我也想用For循环来做,这样我就可以比较性能。我感谢任何帮助。


        List<Tuple<double, int, int>> FHM = new List<Tuple<double, int, int>>(); 

        FHM.Add(Tuple.Create(2500.00, 1, 5));

        FHM.Add(Tuple.Create(2400.00, 2, 300));    

        FHM.Add(Tuple.Create(2300.00, 4, 10));

        FHM.Add(Tuple.Create(2600.00, 1, 325));

我试图实现下面的代码,对嵌套的for循环的双精度列表进行精细排序,但我无法为元组重新编码它...


List<double> FH = new List<double>() { 2510, 2509, 2501.5, 2501.75 };



        var S1 = Stopwatch.StartNew();

        double temp;

        for (int i = 0; i < FH.Count; i++)

        {

            for (int n = 0; n < i; n++)

            {

                if (FH[n] > FH[i])

                {

                    temp = FH[i];

                    FH[i] = FH[n];

                    FH[n] = temp;

                }

            }

        }

        S1.Stop();

        Console.WriteLine($"S1 Ticks = " + S1.ElapsedTicks);

/* 使用 ForLoop 的已用次数为 4000。而与FH。排序();均为 16000。因此,我希望带有元组的ForLoop版本对于我正在处理的短列表将表现得更好。*/


慕侠2389804
浏览 76回答 2
2回答

陪伴而非守候

由于您似乎不关心排序算法本身,因此您可以迭代元组并将排序卸载到排序列表,如下所示:对数字元组列表进行排序using System;using System.Collections.Generic;namespace ConsoleApp{&nbsp; &nbsp; public class Program&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<Tuple<double, int, int>> FHM = new List<Tuple<double, int, int>>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FHM.Add(Tuple.Create(2500.00, 1, 5));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FHM.Add(Tuple.Create(2400.00, 2, 300));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FHM.Add(Tuple.Create(2300.00, 4, 10));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FHM.Add(Tuple.Create(2600.00, 1, 325));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var sorted = new SortedList<double, Tuple<double, int, int>>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (Tuple<double, int, int> t in FHM)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sorted.Add(t.Item1, t);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

四季花海

&nbsp; &nbsp; &nbsp; &nbsp; var S2 = Stopwatch.StartNew();&nbsp; &nbsp; &nbsp; &nbsp; var temp = Tuple.Create(0.00, 0, 0);&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < FHM.Count; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int n = 0; n < i; n++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (FHM[n].Item1 > FHM[i].Item1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp = FHM[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FHM[i] = FHM[n];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FHM[n] = temp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; S2.Stop();&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Ticks S2 ForLoop = " + S2.ElapsedTicks); // 4000 ElapsedTicks&nbsp; &nbsp; &nbsp; &nbsp; S2.Reset();&nbsp; &nbsp; &nbsp; &nbsp; S2.Start();&nbsp; &nbsp; &nbsp; &nbsp; FHM.Sort();&nbsp; &nbsp; &nbsp; &nbsp; S2.Stop();&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Ticks S2 List.Sort(); = " + S2.ElapsedTicks); // 700000 ElapsedTicks
打开App,查看更多内容
随时随地看视频慕课网APP