C# program problems[Sorting two massives into one]

-3

I need to sort two arrays into one,but 0000000 happens in the middle of output. Whats the problem? Cant find a mistake, its in the bubble sort? Here is the code

'ConsoleApplication5.vshost.exe' (CLR v4.0.30319: ConsoleApplication5.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Data.DataSetExtensions\v4.0_4.0.0.0__b77a5c561934e089\System.Data.DataSetExtensions.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'ConsoleApplication5.vshost.exe' (CLR v4.0.30319: ConsoleApplication5.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Microsoft.CSharp\v4.0_4.0.0.0__b03f5f7f11d50a3a\Microsoft.CSharp.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'ConsoleApplication5.vshost.exe' (CLR v4.0.30319: ConsoleApplication5.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\System.Data\v4.0_4.0.0.0__b77a5c561934e089\System.Data.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'ConsoleApplication5.vshost.exe' (CLR v4.0.30319: ConsoleApplication5.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'ConsoleApplication5.vshost.exe' (CLR v4.0.30319: ConsoleApplication5.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\mscorlib.resources\v4.0_4.0.0.0_ru_b77a5c561934e089\mscorlib.resources.dll'. Module was built without symbols. The thread 0x1264 has exited with code 259 (0x103). The thread 0xc4c has exited with code 259 (0x103). The thread 0xdc4 has exited with code 0 (0x0). The thread 0x16f4 has exited with code 259 (0x103). 'ConsoleApplication5.vshost.exe' (CLR v4.0.30319: ConsoleApplication5.vshost.exe): Loaded 'c:\users\vas\documents\visual studio 2013\Projects\ConsoleApplication5\ConsoleApplication5\bin\Debug\ConsoleApplication5.exe'. Symbols loaded. The program '[1112] ConsoleApplication5.vshost.exe: Program Trace' has exited with code 0 (0x0). The program '[1112] ConsoleApplication5.vshost.exe' has exited with code -1073741510 (0xc000013a).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] array1 = new int[20];
            int[] array2 = new int[20];
            int[] array = new int[40];
            int k = 0;
            Random rnd = new Random();
            Console.WriteLine("Исходный массив №1:");
            for (int i = 0; i < 20; i++)
            {
                array1[i] = rnd.Next(-100, +100);
                Console.Write("{0} ", array1[i]);
            }
            Console.WriteLine();
            Console.WriteLine("Исходный массив №2:");
            for (int i = 0; i < 20; i++)
            {
                array2[i] = rnd.Next(-100, +100);
                Console.Write("{0} ", array2[i]);
            }
            Console.WriteLine();
            for (; k < 20; k++)
            {
                array[k] = array1[k];
            }
            for (; k > 20; k++)
            {
                array[k] = array2[k - 20];
            }

            Console.WriteLine("Отсортированный массив:");

            BubbleSort(ref array);
            for (int i = 0; i < 40; i++)
            {
                Console.Write("{0} ", array[i]);
            }
            Console.ReadKey();
        }
        static void BubbleSort(ref int[] A)
        {
            for (int i = 0; i < A.Length; i++)
            {
                for (int j = i + 1; j < A.Length; j++)
                {
                    if (A[j] < A[i])
                    {
                        var temp = A[i];
                        A[i] = A[j];
                        A[j] = temp;
                    }
                }
            }
        }
    }
}
c#
arrays
sorting
asked on Stack Overflow Nov 11, 2014 by Sergey Volynkin

1 Answer

1

This should be:

        for (int k = 0; k < 20; k++)
        {
            array[k] = array1[k];
        }
        for (int k = 0; k < 20; k++)
        {
            array[k + 20] = array2[k];
        }

You could do

        for (int k = 0; k < 20; k++)
        {
            array[k] = array1[k];
            array[k + 20] = array2[k];
        }

but due to memory pipeline caching, 2 loops are usually faster.

answered on Stack Overflow Nov 11, 2014 by Margus • edited Nov 11, 2014 by Margus

User contributions licensed under CC BY-SA 3.0