![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Nov 2005
Posts: 35
Rep Power: 0
![]() |
Parameter Mutation
I have a static method (quicksort) that looks to me like it should return a new set of data thats sorted, but should not change the org. data set.
//dataCopy is a float[]
QuickSortStatic.quickSort(dataCopy);
......
public static float[] quickSort(float[] input)
{
data = input;
recQuickSort(0, data.Length - 1); //does the qsort
return data;
}I want to have a line float[] f = QuickSortStatic.quickSort(dataCopy); |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
What do you think this does: data = input;?
You are not creating a copy anywhere.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Nov 2005
Posts: 35
Rep Power: 0
![]() |
Quick sort is is a different class completly. The var "data" is encapsulated in that class as a static var that all the other quick sort methods modify when doing the sort. I don't see why the var "input" would be modified after setting "data = input;" because it is not used anywhere else in the qsort class.
Where is my understanding breaking down? |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
I'm not sure what you mean exactly. Could you post me some more code.
You are basing your quicksort off of this example?
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#5 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
Although float is value-type, an Array is not. Internally, an array of floats is actually an Array object. As such, arrays are passed by reference. When you data to input, the two array references then point to the same Array, not unlike any other object. Ever wondered why
float[] bob; float[] bob = new float[10]; May want to look into the Array.Copy method.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Nov 2005
Posts: 35
Rep Power: 0
![]() |
Well, my program runs sucessfully using a workaround with the Clone method, so for the time being, I need to leave it at that. There's still major other portions of it that need to get done, so I need to quit fine tuning what already works. :-)
|
|
|
|
|
|
#7 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Note that the clone method makes a shallow copy:
Quote:
data = input; Try: Array.Copy(input, data, input.Length);
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|