私は、配列の行をソートするためにクイックソート法を採用しました。 ここでは、コードです:行列行 - クイックソート再帰問題
1を再帰的に実行されたときにループ「は、」何らかの理由のためにすべての[OK]バリを実行したときにここで問題のあるメソッドが最初に
static void QuickSortMatrix(int[,] a)
{
int n = a.GetLength(0);
int m = a.GetLength(1);
for (int i = 0; i < n; i++)
{
QuickSortRow(0, m - 1, i, a);
}
for (int j = 0; j < m; j++)
{
QuickSortRow(0, n - 1, j, a);
}
}
static void QuickSortRow(int lowBound, int highBound, int row, int[,] a)
{
int temp = 0;
int x = random.Next(lowBound, highBound);
int pivot = a[row,x];
int p = lowBound;
int q = highBound;
do
{
while (a[row,p] < pivot) p++;
while (pivot < a[row,q]) q--;
if (p <= q)
{
temp = a[row,p];
a[row,p] = a[row,q];
a[row,q] = temp;
p++; q--;
}
}
while (p <= q);
if (lowBound < q) { QuickSortRow(lowBound, q, row, a); }
if (p < highBound) { QuickSortRow(p, highBound,row, a); }
}
の罰金
static void QuickSort(int lowBound, int highBound, int[] a)
{
int temp = 0;
int x = random.Next(lowBound, highBound);
int pivot = a[x];
int i = lowBound;
int j = highBound;
do
{
while (a[i] < pivot) i++;
while (pivot < a[j]) j--;
if (i <= j)
{
temp = a[i]; //changes an element smaller than the pivot...
a[i] = a[j];//... with the greater one
a[j] = temp;
i++; j--;
}
}
while (i <= j);
if (lowBound < j) { QuickSort(lowBound, j, a); }//recursion
if (i < highBound){ QuickSort(i,highBound, a); }
}
の作品メソッドを呼び出すときに一定でなければならない行は、行列の境界の外側になります。 は、ここに私の配列だと、行は4
int[,] matrix =
{
{7,8,9,10,11,5},
{3,6,4,16,22,4},
{7,9,17,8,3,21},
{24,7,11,19,3,4}
};
の値に達した私は私の説明が十分に明らかだった願っています。
誰かに助言してもらえますか?ここで私は何が欠けているのですか?
ご協力いただきありがとうございます。
BRステファン
1つの場合、1文字以上の変数名が便利です。これは読むのが難しい –
読むのは不可能です。なぜ 'highBound'の代わりに 'q'を使うのですか?最初のものは説明的なものではありませんが、2番目のものはその変数の内容を一目でわかります。 'q'の代わりに、 'highBoundTmp'や 'currentHighBound'などを使用してみませんか? – Amy
ありがとうございます。私はそれを将来思いついてくれるでしょう – kidwon