DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

1. Registered User
Join Date
Mar 2007
Posts
4

I have an unique sorting problem in JAVA.

Assume an array of 10 random numbers ..
say.. [1,2,3,4,5,6,7,8,9,10]

Sort the array.. in the follwoing manner
1) the least number should come in the middle
2) the next one should come to the left
3) the next one should cometo right.

so the result would be somethinglike this.

[10,8,6,4,2,1,3,5,7,9]

P.S... Preferably using a single array...and not taking help of any buffer/temporary array...
Last edited by Subhra_786; 03-12-2007 at 03:44 AM. Reason: urgency

2. Registered User
Join Date
May 2005
Location
Posts
173

## Okeh

It makes me think..........
I would store the first ele. @ i=0 then the second @ -1, third @ +1, fourth
-2, fifth @ +2, ...etc.
It is just a thought.
Good luck,

3. Registered User
Join Date
Mar 2007
Posts
4
Originally Posted by Kinda Electroni
It makes me think..........
I would store the first ele. @ i=0 then the second @ -1, third @ +1, fourth
-2, fifth @ +2, ...etc.
It is just a thought.
Good luck,
store and where....in the same array

4. Registered User
Join Date
May 2005
Location
Posts
173
oh Okay,
Give me some time.

5. Registered User
Join Date
May 2005
Location
Posts
173

## Try This

Code:
```public class Sort{

static public void main(String args[]) {

double[] x = rndDoubleArray(7);
fancyPrint1("Random Not Sorted Array");
printArray(x);
fancyPrint1("Random Increase Sorted Array");
x = sortIncrease(x);
printArray(x);

fancyPrint1("Random Decrease Sorted Array");
x = sortDecrease(x);
printArray(x);

fancyPrint1("Random BinarryTree Sorted Array");
x = sortBinarry(x);
printArray(x);

}

static public double[] rndDoubleArray(int len) {
double[] array = new double[len];

for (int i = 0; i < array.length; i++){
array[i] = Math.random() * 100;
}
return array;
}

static public double[] sortIncrease(double[] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 1; j < array.length; j++) {
if (array[j] < array[j - 1]) {
double temp = array[j - 1];
array[j - 1] = array[j];
array[j] = temp;
}
}
}
return array;
}
static public double[] sortDecrease(double[] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 1; j < array.length; j++) {
if (array[j] > array[j - 1]) {
double temp = array[j - 1];
array[j - 1] = array[j];
array[j] = temp;
}
}
}
return array;
}
static public double[] sortBinarry(double[] array) {

for (int i = 0; i < array.length; i++) {
for (int j = 1; j < array.length; j++) {
if (array[j] < array[j - 1]) {
double temp = array[j - 1];
array[j - 1] = array[j];
array[j] = temp;
}
}
}
int j = array.length/2;
int k = 1+(array.length/2);

double[] B = rndDoubleArray(7);
for (int l = 0; l < 1+array.length && j>-2 && k<array.length; ) {

B[j] = array[l]; j--;l++;
B[k] = array[l];
k++; l++;
}
for (int i = 0; i < array.length; i++) {
array[i] = B[i];
}
return array;
}

public static void printArray( double[] array) {
System.out.println("           ");
for (int i = 0; i < array.length; i++) {
System.out.println("i=" + i + "   " + array[i]);
}
System.out.println("         ");
System.out.println("****************************");
}
public static void fancyPrint1(String s) {
System.out.println("****************************");
System.out.println(s);
System.out.println("****************************");
return;
}
}//The End of Sort.java```

6. Registered User
Join Date
Mar 2007
Posts
4
why not use the sort method to sort the array

7. Registered User
Join Date
May 2005
Location
Posts
173
If I have understood your question, Sort would sort it either inc. or Decrease.
But, in this stuation, it is a special case.
I am sure there are many ways to do it.
I did it by sorting it increase then take the first but it mid and so on.
Always, try to find another way. It is there.
I would like to see more ideas.

8. Registered User
Join Date
Mar 2007
Posts
4

## thnx a lot but there is a small problem

thnx a lot but there is a small problem..the out put from the above code i as such...
****************************
Random Not Sorted Array
****************************

i=0 83.77529678373216
i=1 94.64655886092646
i=2 63.18769349656511
i=3 94.03274583838417
i=4 28.20465940271396
i=5 80.19877995404732
i=6 34.416278521983465

****************************
****************************
Random Increase Sorted Array
****************************

i=0 28.20465940271396
i=1 34.416278521983465
i=2 63.18769349656511
i=3 80.19877995404732
i=4 83.77529678373216
i=5 94.03274583838417
i=6 94.64655886092646

****************************
****************************
Random Decrease Sorted Array
****************************

i=0 94.64655886092646
i=1 94.03274583838417
i=2 83.77529678373216
i=3 80.19877995404732
i=4 63.18769349656511
i=5 34.416278521983465
i=6 28.20465940271396

****************************
****************************
Random BinarryTree Sorted Array
****************************

i=0 33.09028376051183
i=1 83.77529678373216
i=2 63.18769349656511
i=3 28.20465940271396
i=4 34.416278521983465
i=5 80.19877995404732
i=6 94.03274583838417

****************************

the red colored number is misplaced...

9. Registered User
Join Date
May 2005
Location
Posts
173

## It is not

I am afraid it is not, but it is not sorted.
I tried many ways to get it sorted but I was unable to do so.
Either the first one got sorted then I missed the last, or the last one get sorted I missed the first.
I have done this modification:

B[0] = array[6];
B[0] is the first item in B = to the last item in array.
Please if you can do it better let me know. Thanks.

Code:
```int j = (array.length/2);
int k = 1+(array.length/2);

double[] B = rndDoubleArray(7);
for (int l = 0; l < array.length && k< 7 && j>-1; ) {

B[j] = array[l]; j--;l++;
//B[j] = array[i];
//j--; i++;
B[k] = array[l]; k++; l++;
}
B[0] = array[6];
for (int i = 0; i < array.length; i++) {
array[i] = B[i];
}
return array;
}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise
 Questions? Contact us. C++ Web Development Wireless Latest Tips Open Source

×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.