我使用了以下代码 -
x[][]
是要排序的数组
void sort() {
int max1, max2, s;
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
max1 = i;
max2 = j;
for (int k = i; k < row; k++) {
if (k == i) { // need to improve this part
for (int l = j; l < column; l++) {
if (x[k][l] > x[max1][max2]) {
max1 = k;
max2 = l;
}
}
} else {
for (int l = 0; l < column; l++) {
if (x[k][l] > x[max1][max2]) {
max1 = k;
max2 = l;
}
}
}
}
s = x[max1][max2];
x[max1][max2] = x[i][j];
x[i][j] = s;
}
}
}
我想删除if
else
语句,或者这是使用选择排序执行此操作的另一种方法吗?
(我知道有更简单的方法可以做到这一点,但我正在尝试选择排序)
答案 0 :(得分:0)
我真的不喜欢你如何命名你的变量。我建议使用r,c作为行和列的前缀,v作为值,然后使用Outer和Inner作为数组上两个循环的后缀,最大值使用Max。
使用vMax会增加算法的内存局部性 - 通过避免查找到远处的内存部分,它在处理大型数组时应该执行得稍快一些。与quickSort相比,选择排序并不具有竞争力:渐近复杂性仍将是可怕的。
这使一切变得更容易阅读:
Severity Code Description Project File Line Suppression State
Error CS1705 Assembly 'Microsoft.Xrm.Sdk.Workflow' with identity 'Microsoft.Xrm.Sdk.Workflow, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' uses 'Microsoft.Xrm.Sdk, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' which has a higher version than referenced assembly 'Microsoft.Xrm.Sdk' with identity 'Microsoft.Xrm.Sdk, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' LVP.MadeUpOrganization.Activity C:\Users\Primary User\Desktop\Developer Training 2.0\LVP.MadeUpOrganization\LVP.MadeUpOrganization.Activity\CSC 1 Active
Error CS0006 Metadata file 'C:\Users\Primary User\Desktop\Developer Training 2.0\LVP.MadeUpOrganization\LVP.MadeUpOrganization.Activity\bin\Debug\LVP.MadeUpOrganization.Activity.dll' could not be found LVP.MadeUpOrganization.Activity.UnitTest C:\Users\Primary User\Desktop\Developer Training 2.0\LVP.MadeUpOrganization\LVP.MadeUpOrganization.Activity.UnitTest\CSC 1 Active
Warning Found conflicts between different versions of the same dependent assembly that could not be resolved. These reference conflicts are listed in the build log when log verbosity is set to detailed. LVP.MadeUpOrganization.Activity
Warning CS0168 The variable 'e' is declared but never used LVP.MadeUpOrganization.Activity C:\Users\Primary User\Desktop\Developer Training 2.0\LVP.MadeUpOrganization\LVP.MadeUpOrganization.Activity\Activity\WorkflowActivity1.cs 39 Active
Warning Found conflicts between different versions of the same dependent assembly that could not be resolved. These reference conflicts are listed in the build log when log verbosity is set to detailed. LVP.MadeUpOrganization.Activity.UnitTest
请注意,我已删除了最里面的条件:它应该执行一次,并且只执行一次,以在评估所有剩余行之前完成当前行。
另请注意 - 我还没有测试过这段代码。它看起来是正确的,但要小心对待它。
答案 1 :(得分:0)
就像我提到的那样,我目前没有媒体来测试这个,但我相信这应该有效。如果不让我知道,我会相应地改变它。
这是我的解决方案:
void sort() {
int max1, max2, count, minValue, rowHold, colHold;
int[][] tempArr = new int[row][column];
while(count != (row*column))
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
max1 = i;
max2 = j;
if(minValue>x[i][j] && x[i][j] != tempArry[i][j])
{
minvalue= x[i][j];
}
}
}
tempArr[rowHold][colHold] = minVlaue;
if(colHold<column)
{
colHold++;
}
else if(colHold=column)
{
colHold = 0;
rowHold++;
}
count++;
}
x = tempArry;
}
此解决方案预先形成选择排序,并且比当前解决方案更有效率。您当前解决方案的问题是它有太多嵌套循环。嵌套循环在效率方面产生了巨大的问题,因此您希望最大限度地减少它们的使用和深度。
希望这有效,如果没有,请对此进行评论,我会解决它!关于我做了什么的任何问题,我将评论和解释我做了什么。