C#:使用向量合并排序

时间:2016-04-02 13:18:15

标签: c# sorting merge

我正在编写一个程序,需要根据它们与欧几里德距离的坐标对房屋列表进行排序。要对列表进行排序,我使用合并排序算法。

Vector2坐标:是建筑物的坐标。 Vector2 house:是主楼的坐标。

以下是执行合并排序的类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;

namespace EntryPoint
{
public static class MergeSorter
{
    public static void DoMergeSort(this Vector2[] numbers)
    {
        var sortedNumbers = MergeSort(numbers);

        for (int i = 0; i < sortedNumbers.Length; i++)
        {
            numbers[i] = sortedNumbers[i];
        }
    }

    private static Vector2[] MergeSort(Vector2[] numbers)
    {
        if (numbers.Length <= 1)
        {
            return numbers;
        }

        var left = new List<Vector2>();
        var right = new List<Vector2>();

        for (int i = 0; i < numbers.Length; i++)
        {
            if (i % 2 > 0)
            {
                left.Add(numbers[i]);
            }
            else
            {
                right.Add(numbers[i]);
            }
        }

        left = MergeSort(left.ToArray()).ToList();
        right = MergeSort(right.ToArray()).ToList();

        return Merge(left, right);
    }

    private static Vector2[] Merge(List<Vector2> left, List<Vector2> right)
    {
        var result = new List<Vector2>();

        while (left.Count > 0 && right.Count > 0)
        {
            if (**left.First() <= right.First()**)
            {
                MoveValueToResult(left, result);
            }
            else
            {
                MoveValueToResult(right, result);
            }
        }
        while (left.Count > 0)
        {
            MoveValueToResult(left, result);
        }
        while (right.Count > 0)
        {
            MoveValueToResult(right, result);
        }
        return result.ToArray();
    }

    private static void MoveValueToResult(List<Vector2> list, List<Vector2> result)
    {
        result.Add(list.First());
        list.RemoveAt(0);
    }

    private static double GetEuclideanDistance(Vector2 coordinates, Vector2 house)
    {
        double distance = Math.Sqrt(Math.Pow((house.X - coordinates.X), 2) + Math.Pow((house.Y - coordinates.Y), 2));
        return distance;
    }
}

}

在带有双星号的行上:left.First()&lt; = right.First()。我认为需要GetEuclideanDistance()方法,但我不知道怎么做?

我尝试过以下内容:GetEuclideanDistance(左侧列表中的第一项)&lt; = GetEuclideanDistance(右侧列表中的第一项)。但后来我不知道我需要给出什么样的参数。

1 个答案:

答案 0 :(得分:1)

如果我理解正确的话,你想要达到的是Collection Vector2house坐标,按其欧几里德距离相对于//Coordinates - coordinates list to sort //housePosition - position to which Euclidean distance should be calculated for every coordinate private static List<Vector2> SortByDistance(IEnumerable<Vector2> coordinates, Vector2 housePosition) { return coordinates.OrderBy(coordinate => GetEuclideanDistance(coordinate, housePosition)).ToList(); } private static double GetEuclideanDistance(Vector2 coordinates, Vector2 house) { double distance = Math.Sqrt(Math.Pow((house.X - coordinates.X), 2) + Math.Pow((house.Y - coordinates.Y), 2)); return distance; } 坐标排序。 如果是这样,那就足够了:

var resultRow = [[isbn,title,subtitle,authors,printType,pageCount,publisher,publishedDate,webReaderLink]];
var sh = SpreadsheetApp.getActiveSheet();
sh.getRange(sh.getLastRow()+1,1,resultRow.length,resultRow[0].length).setValues(resultRow);