Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Arrays.sort(int[], int, int)

Harikrishna Gorrepati
Ranch Hand
Posts: 423
Hi, What is the meaning of the method (Arrays.sort(int[], int, int))declared in this class ? I did not understand from the Javadocs

Paul Clapham
Sheriff
Posts: 21443
33

Harikrishna Gorrepati
Ranch Hand
Posts: 423
Hi Paul, I see this in the Javadocs "Sorts the specified range of the specified array of ints into ascending numerical order. The range to be sorted extends from index fromIndex, inclusive, to index toIndex, exclusive. (If fromIndex==toIndex, the range to be sorted is empty.)"..I did not understand the meaning of it.

Paul Clapham
Sheriff
Posts: 21443
33
Do you understand the meaning of the first sentence? The first word? Surely you don't mean to say you don't understand anything at all about it?

If you explain what you do understand and what you don't understand then we can get started on explaining the latter parts.

Harikrishna Gorrepati
Ranch Hand
Posts: 423
Sorts the specified range (1-2 index, but 2 is exclusive, means 1-1) of the specified array (intArray) of ints (33, 2, 5, 1) into ascending numerical order..I thought sorting will be done from index 1 to index 1. Because it is only 1 element, I thought output should be (33, 2, 5, 1)..If the range is 0-4(4 exclusive means 3)..which becomes 0-3, that's why sorting should be done from index 0 to 3. the output should be 1, 2, 5, 33..but my assumption is completely wrong after executing multiple options.

Paul Clapham
Sheriff
Posts: 21443
33
So it looks like you do understand what it says, after all. But now you seem to be referring to the execution of some code and to the output of that code, which we haven't seen. And you think that the output should be something else, which you also haven't told us. That isn't very helpful.

Harikrishna Gorrepati
Ranch Hand
Posts: 423
Hi Paul, I mentioned what the output should be "Because it is only 1 element, I thought output should be (33, 2, 5, 1)..". May be I do not understand what you are expecting from me. I gave everything I know. All I do not understand is, how output is different than my assumption.

Paul Clapham
Sheriff
Posts: 21443
33
Harikrishna Gorrepati wrote:Hi Paul, I mentioned what the output should be "Because it is only 1 element, I thought output should be (33, 2, 5, 1)..". May be I do not understand what you are expecting from me. I gave everything I know. All I do not understand is, how output is different than my assumption.

What do I expect from you? Well, you told us what you thought the output should be. And you don't understand how the actual output is different than that. Well, I don't understand that either because I can't see the actual output. I hope that helps you to understand what I expect.

Harikrishna Gorrepati
Ranch Hand
Posts: 423
Actual output is 1 2 5 33. but I am expecting (33, 2, 5, 1). Yes. I don't understand how the actual output is different than that. Don't know where I am going wrong.

Paul Clapham
Sheriff
Posts: 21443
33
I agree with you about what the output of the posted code should be. And what you are actually seeing is what would happen if the entire array were sorted, right? So you should make sure that you are actually running the code you think you ran, and not some other code with "Arrays.sort(intArray)" in it instead.

Sumit Khurana
Ranch Hand
Posts: 68
Actual output is 1 2 5 33. but I am expecting (33, 2, 5, 1). Yes. I don't understand how the actual output is different than that. Don't know where I am going wrong.

I thing you are executing some wrong code as paul said...the actual output is what you are expecting...means 33,2,5,1

Harikrishna Gorrepati
Ranch Hand
Posts: 423
I am completely lost and not in sync with Paul. It would be really helpful if you/somebody can provide valid code with small explanation because you said I am executing wrong code.

Paul Clapham
Sheriff
Posts: 21443
33
The code you posted at the beginning of the thread will produce the output you expect it to produce. You understand the documentation correctly. It will not produce the output you said it produced.

Therefore you must be running some other code.

Sumit Khurana
Ranch Hand
Posts: 68

The output of this code is : 33,2,5,1
Explanation:Sorts the specified range of the specified array of ints into ascending numerical order. The range to be sorted extends from index fromIndex, inclusive, to index toIndex, exclusive. (If fromIndex==toIndex, the range to be sorted is empty).

Henry Wong
author
Marshal
Posts: 21751
85
Harikrishna Gorrepati wrote:I am completely lost and not in sync with Paul. It would be really helpful if you/somebody can provide valid code with small explanation because you said I am executing wrong code.

It's not much easier to show that you are not "executing wrong code" by showing us the exact codes and there outputs? What you are describing doesn't make sense, hence, we are saying you have a bug in your code. Show us the code and output for that code.

Henry

Henry Wong
author
Marshal
Posts: 21751
85
Harikrishna Gorrepati wrote:Sorts the specified range (1-2 index, but 2 is exclusive, means 1-1) of the specified array (intArray) of ints (33, 2, 5, 1) into ascending numerical order..I thought sorting will be done from index 1 to index 1. Because it is only 1 element, I thought output should be (33, 2, 5, 1)..If the range is 0-4(4 exclusive means 3)..which becomes 0-3, that's why sorting should be done from index 0 to 3. the output should be 1, 2, 5, 33..but my assumption is completely wrong after executing multiple options.

My code... modified from your code...

My output...

33 2 5 1
1 2 5 33

Looks correct to me.

Henry

Harikrishna Gorrepati
Ranch Hand
Posts: 423
Thanks Henry. This is what I was looking for. You made it so simple

Henry Wong
author
Marshal
Posts: 21751
85
Harikrishna Gorrepati wrote:Thanks Henry. This is what I was looking for. You made it so simple

Yeah, but you should still show us the code ...

Harikrishna Gorrepati wrote:If the range is 0-4(4 exclusive means 3)..which becomes 0-3, that's why sorting should be done from index 0 to 3. the output should be 1, 2, 5, 33..but my assumption is completely wrong after executing multiple options.

that led to to draw the conclusion that your assumption is completely wrong. Heck, you should show us the multiple options that you tried.

Henry

Harikrishna Gorrepati
Ranch Hand
Posts: 423
Here is the updated code with comments.