C#2 D配列を実現するソートアルゴリズム(コード)
18375 ワード
C#2 D配列を実現するソートアルゴリズム(コード)
//
class toDimSort
{
// row ,
public object[] GetRowByID(object[,] lists, int row)
{
if (row > (lists.GetLength(0) - 1))
throw new Exception("row ");
object[] tmp = new object[lists.GetLength(1)]
;
for (int i = 0; i < lists.GetLength(1); i++)
tmp[i] = lists[row, i];
return tmp;
}
//
public void CopyToRow(object[,] lists, int row, object[] tmp)
{
if (row > lists.GetLength(0) - 1)
throw new Exception("row ");
if (tmp.Length > lists.GetLength(1))
throw new Exception("row ");
for (int i = 0; i < lists.GetLength(1); i++)
lists[row, i] = tmp[i];
}
// :lists- ,orderCols- ,type-0 |1
public object[,] orderBy(object[,] lists, int[] orderCols, int type)
{
// , lists.GetLength(1) ,GetLength(0)
// ,
object[] small = new object[lists.GetLength(1)];
object[] big = new object[lists.GetLength(1)];
int cmpResult;
for(int i = 0; i < lists.GetLength(0); i++)
{
for(int k = i+1; k < lists.GetLength(0); k++)
{
if(type.Equals(1)) //
{
for(int h = 0; h < orderCols.Length; h++)
{
int col = orderCols[h];
// k i
object kh = lists[k, col];
object ih = lists[i, col];
cmpResult = Comparer.Default.Compare(kh, ih);
if (cmpResult.Equals(1)) // k , i ,
{
small = GetRowByID(lists, i);
big = GetRowByID(lists, k);
CopyToRow(lists, i, big);
CopyToRow(lists, k, small);
}
if(cmpResult != 0)
break;
}
}
else //
{
for(int h =0; h <orderCols.Length; h++)
{
int col = orderCols[h];
// k i
object kh = lists[k, col];
object ih = lists[i, col];
cmpResult = Comparer.Default.Compare(kh, ih);
if (cmpResult.Equals(-1)) // i , k ,
{
small = GetRowByID(lists, k);
big = GetRowByID(lists, i);
CopyToRow(lists, i, small);
CopyToRow(lists, k, big);
}
if(cmpResult != 0)
break;
}
}
}
}
return lists;
}
}