简体   繁体   English

如何在没有排序 function 的情况下使用给定顺序对 C# 中的数组元素进行排序?

[英]How to Sort Array Elements in C# with a given order without sort function?

I've been searching about an algorithm that sorts "strings" with a given order.我一直在寻找一种按给定顺序对“字符串”进行排序的算法。

Input : [W, R, B, G, G, R, W, B] ( obviously, just random )输入[W, R, B, G, G, R, W, B]显然,只是随机的

Output : [R, R, W, W, B, B, G, G] Output[R, R, W, W, B, B, G, G]

I want to do like a " Two-Pass Algorithm ".我想做一个“两遍算法”。

Something like:就像是:

using System;
using System.Collections.Generic;
using System.Drawing;

namespace ConsoleApp5
{
    class Program2
    {
        static void Main(string[] args)
        {
            List<Color> colors = new List<Color>() { Color.White, Color.Red, Color.Green, Color.Blue }; // Random Ordered Array
            int j = 0;
            int k = colors.Count;
            int i = 0;
            while (j < k)
            {
                if (colors[j] == Color.White)
                {
                    j += 1;
                }
                else if (colors[j] == Color.Blue)
                {
                    swap(colors[j], colors[--k]);
                    Console.WriteLine(colors[j]);
                }
                else
                {
                    swap(colors[j++], colors[i++]);
                }
                i += 1;
            }

            void swap(Color a, Color b)
            {
                Color temp = a;
                a = b;
                b = temp;

            }
        }
    }
}

EDIT 1编辑 1

I was able to print "RRWWGG" from this code:我能够从此代码打印“RRWWGG”:

using System;
using System.Collections.Generic;
using System.Drawing;

namespace ConsoleApp5
{
    class Program2
    {
        static void Main(string[] args)
        {
            List<String> colors = new List<String>() { "W", "R", "G", "W", "R", "G"}; // Random Ordered Array
            int start = 0;
            int end = colors.Count - 1;
            int current = 0;

            while (current <= end && start < end)
            {
                if(colors[current] == "R")
                {
                    swap(colors, current, start);
                    start++;
                    current++;
                }
                else if (colors[current] == "G")
                {
                    swap(colors, current, end);
                    end--;
                }
                else
                {
                    current++;
                }

            }
            for(int i = 0; i < colors.Count; i++)
            {
                Console.WriteLine(i+colors[i]);
            }
        }
        static void swap(List<String> colors, int a, int b)
        {
            String temp = colors[a];
            colors[a] = colors[b];
            colors[b] = temp;

        }
    }
}

Now, I want to do the same algorithm to place W and B in the middle, given that R must be placed on the left and G on the right.现在,我想做同样的算法把WB放在中间,因为R必须放在左边, G放在右边。

I added B to the array with this code:我使用以下代码将B添加到数组中:

using System;
using System.Collections.Generic;
using System.Drawing;

namespace ConsoleApp5
{
    class Program2
    {
        static void Main(string[] args)
        {
            List<String> colors = new List<String>() { "W", "R", "G", "W", "R", "G", "B", "B" }; // Random Ordered Array
            int start = 0;
            int end = colors.Count - 1;
            int current = 0;

            while (current <= end && start < end)
            {
                if(colors[current] == "R")
                {
                    swap(colors, current, start);
                    start++;
                    current++;
                }
                else if (colors[current] == "G")
                {
                    swap(colors, current, end);
                    end--;
                }
                else
                {
                    current++;
                }

            }
            for(int i = 0; i < colors.Count; i++)
            {
                Console.WriteLine(i+colors[i]);
            }
        }
        static void swap(List<String> colors, int a, int b)
        {
            String temp = colors[a];
            colors[a] = colors[b];
            colors[b] = temp;

        }
    }
}

The result of the above code was: [R, R, B, W, W, B, G, G] .上述代码的结果是: [R, R, B, W, W, B, G, G]

I want the result to be [R, R, W, W, B, B, G, G] without " library's sort function " and only with this kind of algorithm.我希望结果是[R, R, W, W, B, B, G, G]没有“库的排序 function ”并且只有这种算法。

A typical approach would be to create a mapping between your letters and something that sorts according to your specifications.一个典型的方法是在你的字母和根据你的规范排序的东西之间创建一个映射。 For example例如

var sortorder = new Dictionary<char, int>(){
{'R', 0},
{'W', 1},
{'B', 2},
{'G', 3}};
var input = new []{'W', 'R', 'B', 'G', 'G', 'R', 'W', 'B'];
var sorted = input.OrderBy(i => sortorder[i]);

If you want to sort full strings instead of just characters you could instead create char->char mappings and replace all the characters in the strings before sorting.如果要对完整字符串进行排序而不仅仅是字符,则可以创建 char->char 映射并在排序之前替换字符串中的所有字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#中对数组列表的元素进行排序 - How to sort elements of array list in C# 如何在C#中基于自定义顺序对基于属性的对象数组进行排序? - How to sort an array of objects based on a property in custom order in C#? 在C#中以升序对字符串数组进行排序 - Sort an array of strings in ascending order in C# 如何基于C#中字符串中元素的出现对数组进行排序? - How to sort an array based on the occurrences of the elements in a string in C#? c#中的数组排序,偶数降序,奇数升序,不使用预定义的排序函数 - Sort Array in c#, even numbers in descending and odd numbers in ascending without using predefined sort function 在c#中按第一个元素的升序对二维数组行进行排序 - Sort 2d array rows in ascending order of their first elements in c# 所以我试图在不使用 C# 中的排序 function 的情况下按升序对列表进行排序,而且我有点新,所以我想知道我该怎么做? - So I'm trying to sort the list in ascending order without using the sort function in C# and I'm a bit new so I was wondering how could I do it? 在C#中对数组排序 - Sort an array in C# 我想在C#中以升序对矩阵进行排序而没有任何功能 - I want to sort matrix in ascending order in C# without any function 以与给定数组相同的顺序对数组进行排序 - Sort an array in the same order as a given array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM