[英]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.现在,我想做同样的算法把W和B放在中间,因为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.