Greetings all.

I'm preparing for my java data structure/algorithms class and I'm starting a personal code repository of sorts and such.

I started out with c++ and my question is this: I'm implementing a controller class and a class full of sorting and search methods in java. Instead of copying the original array into a temp every time i sort it,
is there a way to deal with this array indirectly -- like using c++ pointers?
I know that there are no pointers in java, but please enlighten me as to what might be the most efficient way to run a lot of sorts on arrays or lists while keeping the original intact.

NOTE: the original array lives in the controller/main class, and is passed into the sorts.

Many Thanks

I know that there are no pointers in java,

Actually it's been a while but if my memory is right, pretty much everything in java IS a pointer, under the hood.

There are ways to hard copy or "clone" variables but by default, everything is gonna be referenced.
If you want to sort an array without altering the original, your best bet is gonna be to create a temp array and work with that.

Again, it has been a while, i could be wrong.

If you don't want to modify the original array, why not just copy that array into the result array for the function and perform the sort on it? If you're returning a different array, there's no reason not to simply use it for the computation.

Also you may be interested in reading this brief article about how Java handles passing parameters.

I guess my trouble here underlines the differences between the two languages. I wrote something that copied my original array into two temp arrays and after the first sort the original array had already been altered.

My reason for doing multiple sorts was just to compare efficiency, and along the way I rash into this interesting problem. I can definitely implement this another way.

still ...
as dmanw100 said: if you don't want your original array to be altered

copy that array into the result array for the function

empire, which way did you use to copy the array so far? maybe we can help figure out why the original was altered.

I see what I did. I wrote this:

int [] numbers = { 2, 3, 4, 5};
int [] numbersCopy = numbers;

when I should have done this:

int [] numbers = { 2, 3, 4, 5};
int [] numbersClone = (int[])numbers.clone();

No way for sort methods to leave arrays unaltered without a copy?

No way for sort methods to leave arrays unaltered without a copy?

what exactly do you mean by this?
sorting the array without changing the order in which the values are held in the array?
sorting ís intended to re-arrange the values in a certain order.

So after passing the array to my sorting function, I'd like to leave the original array intact. IE, the function returns a sorted array, original function remains unsorted. Like I said earlier, I could accomplish this in c++ with pointers-- how can I do this in java would be my main question. The only solution i've found would be to copy the array, but this seems very inefficient.

I see what I did. I wrote this:

int [] numbers = { 2, 3, 4, 5};
int [] numbersCopy = numbers;

when I should have done this:

int [] numbers = { 2, 3, 4, 5};
int [] numbersClone = (int[])numbers.clone();

No way for sort methods to leave arrays unaltered without a copy?

Hmm well i see what you mean, and im not sure if this is what dmanw100 was saying but... if you had a method to sort the array, just pass that array to the method and return the sorted array, this way the original will remain the same, and you wont necessarily be creating a temp array as passing it to the method that returns the sorted array will in all logic do this for you?

Here is some psuedo(of sorts) to try explain

public class arraysort {
static int[] arr= {1,2,3,4};

public static void main(string[] args) {
int[] sortedarray=sortarrays(arr);
}

static int[] sortarrays(int[] temp) {
//sort arrays
return temp;
}
}

corrupt: I implemented something like what you've got, but the original is still being altered. Looking over that article on how java passes arguments made things a bit clearer since they are all objects are being passed by value. Therefore, if i pass the array into the sort its going to be altered.

What I'm trying to accomplish is fairly silly/trivial. It was more of a curiosity that I ran into than an impossibility. This cleared things up, thanks guys. Copy it is!

How do I mark a thread as solved??

corrupt: I implemented something like what you've got, but the original is still being altered. Looking over that article on how java passes arguments made things a bit clearer since they are all objects are being passed by value. Therefore, if i pass the array into the sort its going to be altered.

What I'm trying to accomplish is fairly silly/trivial. It was more of a curiosity that I ran into than an impossibility. This cleared things up, thanks guys. Copy it is!

How do I mark a thread as solved??

oh yes, i tried it now and see what you mean, glad you worked it out though...

and to mark a thread as solved look at the bottom of this page is should be there :)

You'll need to have at least two copies of the array in the end (if you want one sorted and the original, unsorted one). The copy will take O(n) time, which in Computer Science theory is considered very respectable as only a constant or logarithmic runtime would be better.

Depending on the sorting algorithm, you may be able to simply write the values out to the new array as you go, but in the end you'll be doing the same amount of work (you still need to write n values to n spaces in another array).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.