I have written a program for merge sort and The program works fine until at a point of merging that it doesn't merge properly
such as Example:
mergesort.in: // all number is one array and not use temp array for sorting
- 2 //-->This is Array Number
- 5 //-->This is Array(next line) Length
- 4 6 98 8 24
- 8 //-->This is Array(next line) Length
- 12 14 89 21 4 7 9 41
mergesort.Out:
//Output in file mergesort.Out
- 4 6 8 24 98
- 4 7 9 12 14 21 41 89
I'm not understand how to work mergeArray function for in-place merge Sort
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class ReadFromFile
{
static void Main()
{
ArrayList lines = new ArrayList();
var input = System.IO.File.ReadAllLines(@"C:\mergesort.in");
System.Console.WriteLine("Contents of mergesort.in = ");
foreach (string line in input)
{
Console.WriteLine("\t" + line);
}
foreach (var i in input)
{
if (Convert.ToInt32(i.Length) > 1)
{
var counter = 0;
foreach (var item in input)
{
if (input[counter].Length > 1)
{
string[] t = input[counter].Split(' ');
foreach (string word in t)
{
lines.Add(word);
}
ReadFromFile.mergesort(t, 0, 5, lines);
}
counter++;
}
foreach (string line in lines)
{
Console.WriteLine("\t" + line);
}
break;
}
}
Console.WriteLine("Press any key to exit...");
System.Console.ReadKey();
}
private static void mergesort(string[] t, int p1, int p2, ArrayList lines)
{
if (p1 < p2)
{
int mid = (p2 + p1) / 2;
ReadFromFile.mergesort(t, p1, mid, lines);
Console.WriteLine(p1);
ReadFromFile.mergesort(t, mid + 1, p2, lines);
Console.WriteLine(t);
foreach (var item in t)
{
Console.WriteLine(item);
}
ReadFromFile.mergeArray(t, p1, mid, p2, lines);
}
}
private static void mergeArray(string[] arr, int start, int mid, int end, ArrayList lines)
{
//I'm not understand how to work this function for in-place merge Sort
}
}
}