我已经阅读了很多关于排序2D数组的帖子,但我仍然无法掌握它,所以我想知道是否有人可以给我一些建议......
我有一个列出字母和数量的aray(我正在对一段文字进行频率分析).我已将这些数据读入矩形数组,需要先按最高频率对其进行排序.到目前为止,这是我的代码:
//create 2D array to contain ascii code and quantities
int[,] letterFrequency = new int[26, 2];
//fill in 2D array with ascaii code and quantities
while (asciiNo <= 90)
{
while ((encryptedText.Length - 1) > counter)
{
if (asciiNo == (int)encryptedText[index])
{
letterCount++;
}
counter++;
index++;
}
letterFrequency[(storeCount), (0)] = (char)(storeCount+66);
letterFrequency[(storeCount), (1)] = letterCount;
storeCount++;
counter=0;
index=0;
letterCount = 0;
asciiNo++;
}
Run Code Online (Sandbox Code Playgroud)
Mar*_*ell 14
您正在使用2D数组来表示2个单独的向量 - 符号和计数.相反,使用2个单独的数组.Array.Sort有一个重载,需要2个数组,并在一个数组上排序,但将更改应用于两者,实现您想要的.
这也允许你对字符而不是int []使用char []:
char[] symbols = ...
int[] counts = ...
...load the data...
Array.Sort(counts, symbols);
// all done!
Run Code Online (Sandbox Code Playgroud)
此时,计数已经被排序,并且符号仍然将索引与它们相关的计数匹配.
| 归档时间: |
|
| 查看次数: |
19992 次 |
| 最近记录: |