TheBulgarianWolf

Sorting of 2 arrays

Apr 16th, 2020
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace OnlineExercise15._4
  8. {
  9.     class Program
  10.     {        
  11.         static void Main(string[] args)
  12.         {
  13.             int[] arrayNum1 = new int[] { 1, 3, 5, 7 };
  14.             int[] arrayNum2 = new int[] { 2, 4, 6, 8 };
  15.  
  16.             int[] arrayNumber3 = new int[arrayNum1.Length + arrayNum2.Length];
  17.  
  18.             int i = 0;
  19.             int j = 0;
  20.             int k = 0;
  21.  
  22.             while(k < arrayNumber3.Length)
  23.             {
  24.                 if(i >= arrayNum1.Length )
  25.                 {
  26.                     arrayNumber3[k] = arrayNum2[j];
  27.                     j++;
  28.                 }
  29.                 else if(j >= arrayNum2.Length)
  30.                 {
  31.                     arrayNumber3[k] = arrayNum1[i];
  32.                     i++;
  33.                 }
  34.  
  35.                 else if(arrayNum1[i] < arrayNum2[j])
  36.                 {
  37.                     arrayNumber3[k] = arrayNum1[i];
  38.                     i++;
  39.                 }
  40.                 else
  41.                 {
  42.                     arrayNumber3[k] = arrayNum2[j];
  43.                     j++;
  44.                 }
  45.                 k++;
  46.             }
  47.  
  48.             for(int m = 0; m < arrayNumber3.Length; m++)
  49.             {
  50.                 Console.Write(arrayNumber3[m] + " ");
  51.             }
  52.             Console.ReadKey();
  53.         }
  54.        
  55.        
  56.     }
  57. }
Add Comment
Please, Sign In to add comment