pleases tell me how to swap numbers in this case. I try to do but the value of A[i] and A[0] cannot swap each other.
namespace HapInCSharp
{
class heap
{
int heap_size_;
int priority_level_;
public int []A = new int[50];
public void Swap(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
public void HeapSort(int[] A, int n)
{
...
for (int i = n ; i >= 1; i--)
{
...
Console.WriteLine(" {0} {1} ", A[i], A[0]);
Swap(A[i], A[0]);
Console.WriteLine(" {0} {1} ", A[i], A[0]);
...
}
}
}
Loading
kPosted Oct 28, 2008, 10:37 PM
kPosted Oct 28, 2008, 10:37 PM
AlanPosted Oct 28, 2008, 6:08 PM
You need to pass parameters to the Swap() method by reference rather than by value:
public void Swap(ref int a, ref int b)
{
int temp = a;
a = b;
b = temp;
}
and call with:
Swap(ref A[i], ref A[0]);