• 0

C# programming problem


Question

Anybody can help me with the programming problem here? I have problem in sorting the arraylist, and this is my work:

problem: I use the infile consists of people's names, and have to sort them ascending order, and store them in the ouput file.

using System;

using System.IO;

using System.Collections;

namespace Lab_6

{

class Class1

{

  static void Main(string[]args)

  {

  Console.Write("This program will read all names from the file, eliminate the duplicate names, ");

  Console.WriteLine("sort them in ascending orders, and save the result in another file.");

  Console.WriteLine("");

  ArrayList names = new ArrayList();

  string infile, filename;

  Console.Write("Enter the input file name:");

  infile = Console.ReadLine();

 

  if (!File.Exists(infile))

    return;

  Console.WriteLine("File Exists.");

  StreamReader sr = File.OpenText(infile);

  Console.Write("Enter the output file:");

  filename = Console.ReadLine();

  if (File.Exists(filename))

    Console.Write("Do you want to overwrite(Y/N)?");

  string ans = (Console.ReadLine());

 

  if (!ans.Equals("Y"))

    return;

  StreamWriter op = File.CreateText(filename);

    string name1;

I'm having problem with the folowing segments, kep showing the araylist is 0

 

  while ((name1 = sr.ReadLine())!=null)

  {

  for (int i=0;name1 != null;i++)

  {   

  Console.WriteLine(name1);

  if (names.Count == 0)

    names.Add(name1);

  if (name1.CompareTo (names[i-1])<0)

    names.Insert(i,name1);

  if (name1.CompareTo (names[i-1]) > 0)

    names.Add(name1);

       

    }

    } 

  Console.WriteLine(names.Count);

  StreamWriter op = File.CreateText(filename);

  for (int y=1;y<names.Count;y++)

    {

    op.WriteLine(names[y]);

    }

    op.Close();

 

 

  }

}

}

 

Edited by meifungnano
Link to comment
Share on other sites

Recommended Posts

  • 0
Who woulda guessed, this one is faster than the one I wrote last night!

Total time: 00:00:00.2812500 vs. Total time: 00:00:00.6875000 (maybe because I used sort() )

It is quite a difference too, hmph.??:sleep:p:

I got full credit for what I did last night, unfortunately my friend the author of this thread, she wasn't so fortunate, I think the professor gave her partial credit!

Anyway, i'm about to give the Linear, and Binary searches a try. And i've already been warned about the terribly slow bubbleSort, which is supposedly only most efficient when 99% of the elements are in the correct order, leaving only 1 to be sorte:):)

I thought that was funny when the prof told us so. Thank goodness I only have 1 more Final then i'm going for a vacation, I need one of those every year!

Yeah, BubbleSort is terribly slow.

void BubbleSort(int a[], int n) {
int i, j;
 ?for (i = n - 1; i &gt;= 1; --i) {
 ? ?for (j = 0; j &lt; i; ++j) {
 ? ? ?if (a[j] &gt; a[j+1]) {
 ? ? ? ?int temp = a[j];
 ? ? ? ?a[j] = a[j+1];
 ? ? ? ?a[j+1] = temp;
 ? ? ?}
 ? ?}
 ?}
}

Which turns out to be O(n^2). This would take more than a day to sort 1.7 million elements. QuickSort, which has O(n log n), can do the same job in 2 seconds. So;)t lives up to its name ;)

Edited by kjordan2001
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.