why doesn't the element get swapped
public static void SwapArray(int[,] arr)
{
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(0); j++)
{
int temp = arr[i, j];
arr[i, j] = arr[j, i];
arr[j, i] = temp;
}
}
}
even if the parameter is without a ref modifier the array doesn't change.
a copy of the reference is passed as a parameter right?
There is an error in your algorithm. For every i and j, your loop swaps arr[i,j] and arr[j,i] twice.
For example arr[3,1] gets swapped with arr[1,3] once for i=3, j=1 and once for i=1, j=3. So the result is the original matrix. You should change the j-loop to
for (int j = 0; j < i; j++)
The second arr.GetLength(0) should be arr.GetLength(1). Because you want to use the 2nd dimension.
try this.
I have changed the second for loop.
u r actually swapping and again reswapping. so u stand where u were.
public static void SwapArray(int[,] arr)
{
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = i+1; j < arr.GetLength(0); j++)
{
int temp = arr[i, j];
arr[i, j] = arr[j, i];
arr[j, i] = temp;
}
}
}
a copy of the reference is passed as a parameter right?
Arrays are passed by reference.
SwapArray(ref int[,] arr)
Here you are passing a reference by reference (sorry, for tautology), this means, that you can even reassign a reference:
arr = new int [10,20];
Related
I can't figure out why it's telling me the object is null when I'm trying to fill the array with an object that I'm constructing.
public int size;
// Use this for initialization
public void Start()
{
size = 10;
Unit[,] array = new Unit[size,size];
int i, j;
for(i = 1; i<=size; i++)
{
for(j=0;j<size;j++)
{
if(i>=1&&j>=1)
{
array[i, j] = new Unit(array[i - 1, j].getHeight(), array[i, j - 1].getHeight());
}
else if(i>=1)
{
array[i, j] = new Unit(array[i - 1, j].getHeight(), 0);
}
else
{
array[i, j] = new Unit(0, 0);
}
}
}
for (i = 0; i <= size; i++)
{
for (j = 0; j <= size; j++)
{
Debug.Log(array[i,j].getHeight());
}
}
}
the Objects I am creating come from a constructor; Unit(int,int)
It says line 23. I'm assuming that it means that the object doesn't exist in the the array but i'm creating the object so IDK
Two erros that I see, the first is that the first for loop should be for(i = 0; i<size; i++) as c# is base 0. This would give you the null object because when you try to assign
if(i>=1&&j>=1)
{
array[i, j] = new Unit(array[i - 1, j].getHeight(), array[i, j - 1].getHeight());
}
when i=1, array[0,j] was never assign (since i started in 1), so it's value is null.
The other is in the last group of loops, as the conditions should be the following:
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
Debug.Log(array[i,j].getHeight());
}
}
Because since C# is base 0, the indexes goes from 0 to size-1
I have a quick question: I know that the complexity of both snippets is same. Yet, I want to know which one is comparatively better and why? This is the selection sort code:
This is what I wrote:
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j <= n - 1; j++)
{
if (a[j] < a[i])
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
This is what my friend wrote:
for (int i = 0; i < n - 1; i++)
{
int iMin = i;
for (int j = i + 1; j <= n - 1; j++)
{
if (a[j] < a[i])
{
iMin = j;
}
int temp = a[i];
a[i] = a[iMin];
a[iMin] = temp;
}
}
Your code is slightly faster because you make swaps only when a[j] < a[i], whereas your friend's code is always making a swap. So, in most cases, your code will make less swaps.
The complexity of both codes are indeed the same, but your "constants" are smaller, so your code is faster.
The following code sorts rows by the first element using bubble method.
I can't change it to counting sort.
public void SortStack(double[,] n)
{
for (int i = 0; i < n.GetLength(0) - 1; i++)
{
for (int j = i; j < n.GetLength(0); j++)
{
if (n[i, 0] > n[j, 0])
{
for (int k = 0; k < n.GetLength(1); k++)
{
var temp = n[i, k];
n[i, k] = n[j, k];
n[j, k] = temp;
}
}
}
}
}
Please help.
As you do bubble sort based on first element of each row. you should do counting sort like that too. so you just need to count first item of each row.
private static int[,] CountingSort2D(int[,] arr)
{
// find the max number by first item of each row
int max = arr[0, 0];
for (int i = 0; i < arr.GetLength(0); i++)
{
if (arr[i, 0] > max) max = arr[i, 0];
}
// we want to get count of first items of each row. thus we need 1d array.
int[] counts = new int[max + 1];
// do the counting. again on first index of each row
for (int i = 0; i < arr.GetLength(0); i++)
{
counts[arr[i, 0]]++;
}
for (int i = 1; i < counts.Length; i++)
{
counts[i] += counts[i - 1];
}
// result is sorted array
int[,] result = new int[arr.GetLength(0), arr.GetLength(1)];
for (int i = arr.GetLength(0) - 1; i >= 0; i--)
{
counts[arr[i, 0]]--;
// now we need to copy columns too. thus we need another loop
for (int j = 0; j < arr.GetLength(1); j++)
{
result[counts[arr[i, 0]], j] = arr[i, j];
}
}
return result;
}
Here is the test.
static void Main()
{
Random rand = new Random();
int[,] arr = new int[3,3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
arr[i, j] = rand.Next(0, 100);
}
}
int[,] newarr = CountingSort2D(arr);
for (int i = 0; i < arr.GetLength(0); i++)
{
Console.Write("{ ");
for (int j = 0; j < arr.GetLength(1); j++)
{
Console.Write(arr[i, j] + " ,");
}
Console.Write("} ,");
}
Console.WriteLine();
for (int i = 0; i < newarr.GetLength(0); i++)
{
Console.Write("{ ");
for (int j = 0; j < newarr.GetLength(1); j++)
{
Console.Write(newarr[i, j] + " ,");
}
Console.Write("} ,");
}
Console.WriteLine();
}
Example Output:
{ 49,66,8 },{ 33,39,64 },{ 65,52,76 } // Original array
{ 33,39,64 },{ 49,66,8 },{ 65,52,76 } // Sorted array
public static void Sort2DArray(int[,] matrix)
{
var numb = new int[matrix.GetLength(0) * matrix.GetLength(1)];
int i = 0;
foreach (var n in matrix)
{
numb[i] = n;
i++;
}
Array.Sort(numb);
int k = 0;
for (i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
matrix[i, j] = numb[k];
k++;
}
}
}
I'm curious how can I make this method generic. I wish that it could sort double matrices, string matrices and so on and so forth.
You can use IComparable interface as a generic type T specifier.
See those links
How to Sort 2D Array in C#
How do I sort a two-dimensional array in C#?
http://www.informit.com/guides/content.aspx?g=dotnet&seqNum=151
I have solved it. the method looks like this:
public static void Sort2DArray<T>(T[,] matrix)
{
var numb = new T[matrix.GetLength(0) * matrix.GetLength(1)];
int i = 0;
foreach (var n in matrix)
{
numb[i] = n;
i++;
}
Array.Sort(numb);
int k = 0;
for (i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
matrix[i, j] = numb[k];
k++;
}
}
}
I am trying to learn two dimensional array and I wrote some basic code, but I am getting this exception. Could you tell me what am I doing wrong?
static void Main(string[] args)
{
Random rnd = new Random();
int[,] array = new int[2, 2];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; i++)
{
array[i, j] = rnd.Next(0, 100);
}
}
for (int i = 0; i < array.GetLength(0); i++)
{
Console.WriteLine(array[i, 0] + "---" + array[i, 1]);
}
Console.ReadLine();
}
The problem is in your inner for-loop. In the iterator section, you're incrementing the i variable, but it should be j. Try this:
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
array[i, j] = rnd.Next(0, 100);
}
}