Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
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>
Posted
Updated 1-Jun-13 11:50am
v3
Comments
Christian Graus 1-Jun-13 17:54pm    
You're going to fail your class. You need to able to do this stuff yourself, that's what a class is for. Once you know how to code, you can use these forums to ask questions about specific areas you get stuck on things not well documented on the web. But, we're not going to do your assignment for you, because we want you to be able to program, if you ever manage to graduate and, heaven forbid, end up in the desk next to us
TnTinMn 1-Jun-13 20:59pm    
I wish you would have posted this comment as a solution so that I could have given you a +5. :)
Christian Graus 1-Jun-13 21:47pm    
*grin* well, I'd like to, to get this out of 'unanswered questions', but it's not really an answer, so it seemed better to not mess with how the site indexes such things.
TnTinMn 1-Jun-13 23:11pm    
Understood, and for what it is worth have another gratuitous +5 for your integrity. It frustrates me seeing so many supposed solutions that are nothing more than comments from those who should know and act better.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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