Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 8th, 2006, 6:52 PM   #1
elford
Programmer
 
elford's Avatar
 
Join Date: Nov 2005
Posts: 35
Rep Power: 0 elford is on a distinguished road
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);
I want f to be an array that is sorted, and dataCopy to be the org., unsorted data. Instead, both f and dataCopy come back as sorted. Somehow dataCopy is being mutated by quicksort. How can I maintain the org. array?
elford is offline   Reply With Quote
Old Mar 9th, 2006, 4:45 AM   #2
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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
nnxion is offline   Reply With Quote
Old Mar 9th, 2006, 5:05 PM   #3
elford
Programmer
 
elford's Avatar
 
Join Date: Nov 2005
Posts: 35
Rep Power: 0 elford is on a distinguished road
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?
elford is offline   Reply With Quote
Old Mar 10th, 2006, 4:48 AM   #4
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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
nnxion is offline   Reply With Quote
Old Mar 10th, 2006, 5:54 PM   #5
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
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;
is null? Ever wondered why you use the new keyword to instantiate an array?
float[] bob = new float[10];

May want to look into the Array.Copy method.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270
Dameon is offline   Reply With Quote
Old Mar 11th, 2006, 12:50 PM   #6
elford
Programmer
 
elford's Avatar
 
Join Date: Nov 2005
Posts: 35
Rep Power: 0 elford is on a distinguished road
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. :-)
elford is offline   Reply With Quote
Old Mar 11th, 2006, 3:18 PM   #7
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Note that the clone method makes a shallow copy:
Quote:
Originally Posted by MSDN
A shallow copy of an Array copies only the elements of the Array, whether they are reference types or value types, but it does not copy the objects that the references refer to. The references in the new Array point to the same objects that the references in the original Array point to.

In contrast, a deep copy of an Array copies the elements and everything directly or indirectly referenced by the elements.
Instead of:
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
nnxion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:14 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC