Advertisement
Musical_Muze

Method for Merging and Sorting Two Arrays of Integers

Nov 8th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.89 KB | None | 0 0
  1. public static int[] merge(int[] array1, int[] array2){
  2.    
  3.     int[] fin = new int[array1.length+array2.length];
  4.     int count = 0;
  5.     int lowNum = 100000;
  6.     int lowPos = 0;
  7.     int temp = 0;
  8.    
  9.  
  10.     for (int h=0; h<fin.length; h++){
  11.         if ((count<array1.length)&&(count<array2.length)){
  12.             if((h%2) == 0){
  13.                 fin[h] = array1[count];
  14.             } else {
  15.                 fin[h] = array2[count];
  16.                 count++;
  17.             };
  18.         } else if ((!(count<array1.length))&&(count<array2.length)){
  19.             fin[h] = array2[count];
  20.             count++;
  21.         } else if ((count<array1.length)&& (!(count<array2.length))){
  22.             fin[h] = array1[count];
  23.             count++;
  24.         };
  25.     };
  26.    
  27.     count = 0;
  28.    
  29.     while(count<fin.length){
  30.         lowNum = 1000000;
  31.         for(int i=count; i<fin.length; i++){
  32.             if(fin[i]<lowNum){
  33.                 lowNum = fin[i];
  34.                 lowPos = i;
  35.             };
  36.         };
  37.         temp = fin[lowPos];
  38.         fin[lowPos] = fin[count];
  39.         fin[count] = temp;
  40.         count++;
  41.     };
  42.  
  43.     return fin;
  44.    
  45. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement