6 Replies Latest reply: Jul 19, 2007 1:23 AM by 807605

# Sorting arrays without the Arrays.sort

I need to create a program which has two integer arrays, one with 12 integers, the other with 17. I then need to send these two arrays into a new method and return another array made up of the two original ones. This new array must be in numerical order but I'm not allowed to call sort on this array. Is there a way to do this without having to use conditionals to test each of the 29 integers?

Thanks.
• ###### 1. Re: Sorting arrays without the Arrays.sort
I need to create a program which has two integer
arrays, one with 12 integers, the other with 17. I
then need to send these two arrays into a new method
and return another array made up of the two original
ones. This new array must be in numerical order but
I'm not allowed to call sort on this array.
Why not?
Is there
a way to do this without having to use conditionals
to test each of the 29 integers?
• ###### 2. Re: Sorting arrays without the Arrays.sort
use sorting algorithm like:
bubble sort
selection sort
Insertion sort
quick sort
merge sort

• ###### 3. Re: Sorting arrays without the Arrays.sort
Ok, so I've gotten this far:
``````import java.util.*;

public class unit7asmntb
{
public static void main (String [] args)
{
int[] arrayTwelve = {1, 23, 29, 99, 5, 17, 73, 34, 50, 8, 81, 82};
int[] arraySeventeen = {2, 4, 9, 75, 83, 14, 98, 56, 65, 63, 12, 88, 55, 27, 78, 94, 30};
Arrays.sort(arrayTwelve);
Arrays.sort(arraySeventeen);
sorter(arrayTwelve, arraySeventeen);
}
public static void sorter (int[] firstArray, int[] secondArray)
{
int[] finale = new int[firstArray.length + secondArray.length];
int firstNumber = 0;
int secondNumber = 0;
int count = 0;
int display = 0;
while ((firstNumber < firstArray.length)&&(secondNumber < secondArray.length))
{
if (firstArray[firstNumber] < secondArray[secondNumber]) {
finale[count] = firstArray[firstNumber];
firstNumber++;
}
else {
finale[count] = secondArray[secondNumber];
secondNumber++;
}
count++;
}
for (display=0; display<finale.length; display++) {
System.out.print(finale[display] + " ");
}
}
}``````
but when I run it, the last or highest number always appears as 0, no matter what the number is. Why?
• ###### 4. Re: Sorting arrays without the Arrays.sort
but when I run it, the last or highest number always appears as 0, no matter what the number is. Why?>
Because somewhere in your algorithm there is a bug. Once you find and fix it, it'll go away.
• ###### 5. Re: Sorting arrays without the Arrays.sort
``while ((firstNumber < firstArray.length)&&(secondNumber < secondArray.length))``
This is the cause of your problem. Say you have 2 arrays, size 3 and size 5. Your final array will be size 8 but due to your condition in the while loop the max amount of numbers that will be stored in the array will be 5 (all the numbers in the array of 5 for example). At this point secondNumber will no longer be less than secondArray.length and the loop will stop executing and the last 3 elements will all hold the default values of 0.

Another point if you do manage to get all values inserted into the new array, it wont be sorted as you only do a comparison between two numbers at any one time. one array holds 1 8 3 and second array holds 2 7 4 9 1. Your "sorted" array will hold 1 2 7 4 8 3 9 1.

I suggest you place ALL numbers into you new array and once that has been done, sort it.
• ###### 6. Re: Sorting arrays without the Arrays.sort
K, I fixed it now and it seems to be working just fine. Thanks everybody!