简体   繁体   中英

Merge sort 2d array c#

I have task to create merge sort algorithm in c# for 2d array. Array looks like that

X1 Y1
X2 Y2
…
Xn Yn

I need to take array from file and sort the lines in ascending x order, also the program should check for the absence of pairs of coordinates with the same X values and at the same time different Y values, when array was sorted, the programm should write it in the file. I had created algorithm for 1d array, but can't understand how to rewrite it for 2d array, here is my code, please help me

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;

class Program
{
    //array merging
    static void Merge(int[] num, int lowIndex, int middleIndex, int highIndex)
    {
        var left = lowIndex;
        var right = middleIndex + 1;
        var tempArray = new int[highIndex - lowIndex + 1];
        var index = 0;

        while ((left <= middleIndex) && (right <= highIndex))
        {
            if (num[left] < num[right])
            {
                tempArray[index] = num[left];
                left++;
            }
            else
            {
                tempArray[index] = num[right];
                right++;
            }

            index++;
        }

        for (var j = left; j <= middleIndex; j++)
        {
            for (var i=0;;) {
                tempArray[index] = num[j];
                index++; }
        }

        for (var j = right; j <= highIndex; j++)
        {
            for (var i = 0; ;)
            {
                tempArray[index] = num[j];
                index++;
            }
        }

        for (var j = 0; j < tempArray.Length; j++)

        {
            for(var i=0; ;)
            { 
            num[lowIndex + j] = tempArray[j];
                }
        }
    }

    //merge sorting
    static int[] MergeSort(int[] num, int lowIndex, int highIndex)
    {
        if (lowIndex < highIndex)
        {
            var middleIndex = (lowIndex + highIndex) / 2;
            MergeSort(num, lowIndex, middleIndex);
            MergeSort(num, middleIndex + 1, highIndex);
            Merge(num, lowIndex, middleIndex, highIndex);
        }

        return num;
    }

    public static int[] MergeSort(int[] num)
    {
        return MergeSort(num, 0, num.Length - 1);
        
    }

    static void Main(string[] args)
    {
        Console.WriteLine("Merge sorting");
        string[] lines = File.ReadAllLines(@"C:\Users\glebk\source\repos\ConsoleApp18\ConsoleApp18\file.txt");
         int[,] num = new int[lines.Length, lines[0].Split(' ').Length];
        for (int i = 0; i < lines.Length; i++)
        {
            string[] temp = lines[i].Split(' ',',');
            for (int j = 0; j < temp.Length; j++)
            {
                num[i, j] = Convert.ToInt32(temp[j]);
                Console.Write(num[i, j]+" ");

                
            }
            Console.ReadKey();
        }

        Console.WriteLine("Sorted array: {0}", string.Join(",", MergeSort(num)));

    }

}`

The most direct way of doing it is replacing

tempArray[index] = num[left];

with something like this

static void CopyLine(int[,] destArr, int destIndex, int[,] sourceArr, int sourceIndex)
{
    for (int i = 0; i < destArr.GetLength(1); ++i)
    {
        destArr[destIndex, i] = sourceArr[sourceIndex, i];
    }
}

here is the whole code

static void CopyLine(int[,] destArr, int destIndex, int[,] sourceArr, int sourceIndex)
{
    for (int i = 0; i < destArr.GetLength(1); ++i)
    {
        destArr[destIndex, i] = sourceArr[sourceIndex, i];
    }
}


//array merging
static void Merge(int[,] num, int lowIndex, int middleIndex, int highIndex)
{
    var left = lowIndex;
    var right = middleIndex + 1;
    var tempArray = new int[highIndex - lowIndex + 1, num.GetLength(1)];
    var index = 0;

    while ((left <= middleIndex) && (right <= highIndex))
    {
        if (num[left, 0] < num[right, 0])
        {
            CopyLine(tempArray, index, num, left);
            left++;
        }
        else
        {
            CopyLine(tempArray, index, num, right);
            right++;
        }

        index++;
    }

    for (var j = left; j <= middleIndex; j++)
    {
        CopyLine(tempArray, index, num, j);
        index++;
    }

    for (var j = right; j <= highIndex; j++)
    {
        CopyLine(tempArray, index, num, j);
        index++;
    }

    for (var j = 0; j < tempArray.GetLength(0); j++)
    {
        CopyLine(num, lowIndex + j, tempArray, j);
    }
}

//merge sorting
static void MergeSort(int[,] num, int lowIndex, int highIndex)
{
    if (lowIndex < highIndex)
    {
        var middleIndex = (lowIndex + highIndex) / 2;
        MergeSort(num, lowIndex, middleIndex);
        MergeSort(num, middleIndex + 1, highIndex);
        Merge(num, lowIndex, middleIndex, highIndex);
    }
}

public static void MergeSort(int[,] num)
{
    MergeSort(num, 0, num.GetLength(0) - 1);
}

static void WriteArray(string description, int[,] arr)
{
    Console.WriteLine(description);
    for (int i = 0; i < arr.GetLength(0); i++)
    {
        for (int j = 0; j < arr.GetLength(1); j++)
        {
            Console.Write(arr[i, j] + " ");
        }
        Console.WriteLine();
    }
    Console.WriteLine();
}

static void Main(string[] args)
{
    int[,] num = new int[,] { { 3, 5 }, { 1, 10 }, { 7, 3 }, { 2, 1 }, { 5, 2 } };
    WriteArray("Original array:", num);
    MergeSort(num);
    WriteArray("Sorted array:", num);
    Console.ReadKey();
}

There are also other options, you can fuse two int values into one long value and sort it as 1D array. But it would need additional steps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM