Click here to Skip to main content
15,885,877 members

How to sort the content of file using merge sort..

Member 8771282 asked:

Open original thread
write a program that sorts a content of txt file using an external merge sort????????
we r done with the coding of browse a file and shown in text file..
we dont knw how to apply the merge sort on txt file and then again sort it in array.
In Form we used brows button,append button,2 textview,merge button,write button and sort in array button,,,,,,,,,
plz anyone help us
here our coding

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Splitfiles
{

    public partial class Form1 : Form
    {
        public FileStream fs;
        System.IO.StreamWriter writer;

        string file_write;

        public Form1()
        {
            InitializeComponent();
        }


        //List<string> Packets = new List<string>();

        //Merge file is stored in drive
        //string SaveFileFolder = @"c:\";

        private void brows_Click(object sender, EventArgs e)
        {
            try
            {
                openFileDialog1.ShowDialog();
                txtBrowsFile.Text = openFileDialog1.FileName;

                fs = new FileStream(txtBrowsFile.Text, FileMode.Open, FileAccess.Read);
                string name = Path.GetFileName(txtBrowsFile.Text);
                //int FileLength = (int)fs.Length / 1024;

            }
            catch (Exception ex)
            {
                lblSendingResult.Text = "EXCEPTION:" + ex;
            }
        }



        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //string fpath="E:\\one.txt";
            string name = Path.GetFileName(txtBrowsFile.Text);
            StreamReader dr = new StreamReader(Path.GetFileName(txtBrowsFile.Text));



            while (dr.Peek() != -1)
            {
                string contents = dr.ReadToEnd();
                /*string[] mer = new string[contents.Length];
                int len = contents.Length;
                int i = 0;
                for (i = 0; i < len; i++)
                {
                    mer[i] = (contents);*/


                textBox1.Text = contents;


            }

            txtBrowsFile.Clear();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            //StreamWriter dw = new StreamWriter(Path.GetFileName(txtBrowsFile.Text));


        }

        private void button3_Click(object sender, EventArgs e)
        {
            string name = Path.GetFileName(txtBrowsFile.Text);
            StreamReader dr = new StreamReader(Path.GetFileName(txtBrowsFile.Text));
            string content;

            content = dr.ReadToEnd().ToString();

            textBox2.Text = textBox1.Text + (content);
            textBox1.Clear();


        }

        private void button4_Click(object sender, EventArgs e)
        {


            //string name = Path.GetFileName(txtBrowsFile.Text);
            //StreamWriter dw = new StreamWriter (Path.GetFileName(txtBrowsFile.Text),true);
            //StreamWriterfile_write = "Path.GetFileName(txtBrowsFile.Text)";
            //writer = new System.IO.StreamWriter(file_write, true);
            //dw.Write(textBox2.Text);
            //dw.Close();
            file_write = "C:\\Users\\asadali\\Documents\\file.txt";
            writer = new System.IO.StreamWriter(file_write);
            writer.Write(textBox2.Text);
            
            writer.Close();
            MessageBox.Show("wrote File");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string name = "C:\\Users\\asadali\\Documents\\file.txt";
            StreamReader dr = new StreamReader(name);
            while (dr.Peek() != -1)
            {
                string contents = dr.ReadToEnd();
                string[] mer = new string[contents.Length];
                int len = contents.Length;
                int i = 0;
                for (i = 0; i < len; i++)
                {
                    mer[i] = (contents);
                    textBox1.Text = contents;

                }
                /*   string[] arr = textBox2.Lines;
                   int i;
                   for (i = 0; i < arr.Length; i++)
                   {

                       textBox2.Text = arr[i];

                       i++;
                       arr[i] = textBox1.Text;
                   }*/

            }
            //string [] contentss = textBox2.Lines.ToArray();

            // for( int i=0;i<contentss.length;i++)>
            //{

            //string[] lines = File.ReadAllLines("C:\\Users\\asidali\\Documents\\file.txt");

            //string[] a = new string[lines.Length];
            //int i;
            //for ( i = 0; i < lines.Length; i++)
            //{
            //  lines[i] = a[i];
            //a[i] = textBox1.Text;
            //}
            //.textBox1a[i] = textBox1.Text;
        }
    }
}
            /*string name = Path.GetFileName(txtBrowsFile.Text);
            StreamReader dr = new StreamReader(Path.GetFileName(txtBrowsFile.Text));

            while (dr.Peek() != -1)
            {
                string contents = dr.ReadToEnd();
                string[] mer = new string[contents.Length];
                int len = contents.Length;
                int i = 0;
                for (i = 0; i < len; i++)
                {
                    mer[i] = (contents);


                    textBox1.Text = contents;

                }

                //string[] lines = this.textBox1.Text.Split('\n');
                //MessageBox.Show(lines.Length.ToString());

        }
    }
            /*private string[] Mergeso                                                                                                                                            rt(string[] mer)
 {
     if (mer.Length <= 1)
     {
         return mer;
     }

     int firstPart = mer.Length / 2;
     string[] strings1 = new string[firstPart];
     string[] strings2 = new string[mer.Length - firstPart];

     string[] sorted = new string[mer.Length];

     // Split the array into two;
     for (int i = 0; i < firstPart; i++)
     {
                                                                                                                                                                                                                                                                                                                                                                                                                  strings1[i] = mer[i];
     }

     for (int i = firstPart; i < mer.Length; i++)
     {
         strings2[i - firstPart] = mer[i];
     }

     strings1 = Mergesort(strings1);
     strings2 = Mergesort(strings2);

     int j = 0;
     int k = 0;

     for (int i = 0; i < sorted.Length; i++)
     {
         if (j == strings1.Length)
         {
             sorted[i] = strings2[k];
             k++;
         }
         else if (k == strings2.Length)
         {
             sorted[i] = strings1[j];
             j++;
         }
         else if (stringcompare(strings1[j], strings2[k]))
         {
             sorted[i] = strings1[j];
             j++;
         }
         else
         {
             sorted[i] = strings2[k];
             k++;
         }
     }

     return sorted;
 }
        
}*/</string></string>
Tags: C#, Visual Studio, Forms

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900