Q:

Why does this merge sort give incorrect results?

My assignment is to merge two arrays using int arrays that the user fills and we have to assume that there will be a maximum of 10000 inputs from the user, and the user inputs a negative number to stop. Then sort the array from least to greatest and print it out. Initially i thought that this would be quite easy but when i finished, i began getting outputs such as:
Enter the values for the first array, up to 10000 values, enter a negative number to quit: 1  
3  
5  
-1  
Enter the values for the second array, up to 10000 values, enter a negative number to quit  
2   
4  
6  
-1  
First Array:  
1   
3   
5  
Second Array:   
2  
4   
6   
Merged Array:  
6 1 2 3 4 5     

as you can see, the six is out of place and i have no idea how to fix it. Here is the source code, i have included copious comments because I really want you guys to help me out to the best of your abilities. IF it's possible to use the same exact technique without implement new techniques and methods into the code please do so. I know there are methods in java that can do all of this in one line but it's for an assignment at a more basic level.
import java.util.Scanner;

public class Merge

{

    public static void main(String [] args)    
    {
        Scanner scan = new Scanner(System.in);

    int [] first = new int[10000];          //first array, assume 10k inputs max
    int [] second = new int[10000];         //first array, assume 10k inputs max

    boolean legal = true; //WILL IMPLIMENT LATER

    int end = 0; // set how many elements to put in my "both" array
    int end2 = 0;// set how many elements to put in my "both" array

    System.out.print("Enter the values for the first array, up to 10000 values, enter a negative number to quit");
    //get values
    for(int i = 0; i<first.length; i++)
    {
          first[i] = scan.nextInt(); //fill first with user input

          if(first[i] <0) //if negative number, stop loop
          {
            end = i; //get position of end of user input
            break;
          }
    }

    System.out.println("Enter the values for the second array, up to 10000 values, enter a negative number to quit");

    for(int i = 0; i<second.length; i++) //exact same as the first get values loop
    {
      second[i] = scan.nextInt();

      if(second[i] <0)
      {
        end2 = i;
        break;
      }
    }

    System.out.print("First Array:\n");

    for(int i = 0; i<first.length; i++) //print first array
    {
      if(i == end) //this prevents from printing thousands of zeros, only prints values that user inputed
        break;
      System.out.println(first[i] + " ");
    }

    System.out.print("Second Array:\n"); 

    for(int i = 0; i<second.length; i++) //same as printing first array 
    {
          if(i == end2)
            break;
          System.out.println(second[i] + " ");
    }

    int [] both = new int[(end)+(end2)]; //instanciate an int array to hold only inputted values from first[] and second[]
    int [] bothF = new int[(end)+(end2)]; //this is for my simple sorter algotithm loop

    for(int i = 0; i<both.length; i++) //fill both with the first array that was filled
    {
      both[i] = first[i];
    }

    int temp = end; // see below
    for(int i = 0;i<both.length; i++) //fill array with the second array that was filled(starting from the end of the first array so that the first set is not overwritten
    {
      if(temp<both.length){ //this prevents an out of bounds
      both[temp] = second[i];

        temp++;}
    }

    //simple sorting algorithm
    for(int d = both.length -1;d>=0;d--)
    {    
          for(int i = 0; i<both.length; i++)
           {
                if(both[d]<both[i])
                {
                      bothF[d] = both[d];
                      both[d] = both[i];
                      both[i] = bothF[d];          
                }      
          }
    }

    System.out.println("Merged Array:"); //print the results
    for(int i = 0; i<both.length; i++)
    {
        System.out.print(both[i] + " ");

    }

    //System.out.println("ERROR: Array not in correct order");    
}

A:

Your sorting algorithm is faulty.
It's similar to selection sort, in that you take two elements and swap them if they're out of place.  However, you don't stop the comparisons when you should: when the index d is less than the index i, the comparison-and-swap based on arr[d] > arr[i] is no longer valid.
The inner loop should terminate with i=d.
The logic of your sort goes something like this:
On the d-th loop, the elements at d+1 and to the right are correctly sorted (the larger numbers).  This is true at the beginning, because there are 0 elements correctly sorted to the right of the right-most element.
On each of the outer loops (with the d counter), compare the d-th largest element slot with every unsorted element, and swap if the other element is larger.
This is sufficient to sort the array, but if you begin to compare the d-th largest element slot with already-sorted elements to its right, you'll end up with a larger number in the slot than should be.  Therefore, the inner loop should terminate when it reaches d.

A:

Sure, you can do it like this
for (int i = 0; i < end; i++) {
    both[i] = first[i];
}
for (int i = 0; i < end2; i++) {
    both[i + end] = second[i];
}

// simple sorting algorithm
for (int d = both.length - 1; d >= 0; d--) {
    for (int i = 0; i < d; i++) {
        if (both[i] > both[d]) {
            int t = both[d];
            both[d] = both[i];
            both[i] = t;
        }
    }
}

Output(s) - 
Enter the values for the first array, up to 10000 values, enter a negative number to quit3
5
-1
Enter the values for the second array, up to 10000 values, enter a negative number to quit
2
4
6
-1
First Array:
3 
5 
Second Array: 
2 
4 
6
-1 
Merged Array:
2 3 4 5 6 

