School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
You're Browsing As A Guest! Register Now...
Become an Expert!

Join 353,811 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 3,418 people online right now.Registration is fast and FREE... Join Now!



Sorting strings from text file using Radix Sort

52 Weeks of Code Challenge: WPF
Week #10 of the 52 Weeks of Code Challenge is WPF. If you're a .NET programmer, you should give it a shot. Click Here!

Sorting strings from text file using Radix Sort Rate Topic: -----

#1 angelvale  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 03-September 09


Dream Kudos: 0

Post icon  Posted 03 September 2009 - 02:50 AM

Hello. can someone help me about my project? I'm having a problem with my radix sort code that i got that sorts strings. It uses MSD radix sort. During run-time, I always get the IndexOutOfRange exception. I'm only inches away in finishing this program. If I solve this problem today, I'll be able to work with the binary search and finish this before saturday which is the passing date.

Here is the code:

 public static void SortByLastName(ref string[] arrayOfLastNames, ref string[] arrayOfFirstNames, ref string[] arrayOfPlateNum, ref string[] arrayOfType, ref string[] arrayOfYear)
		{
			Console.Clear();

			int vehicleCountInt = 0;
		   
			Console.WriteLine("SHOW DATABASE: SORTING BY LAST NAME\n");

			using (StreamReader countVehicle = new StreamReader("vehiclecount.txt"))
			{
				vehicleCountInt = Int32.Parse(countVehicle.ReadLine());
			}

			using (StreamReader outputLastNames = new StreamReader("lastnames.txt"))
			{
				int[] b = new int[vehicleCountInt];

				for (int i = 0; i < vehicleCountInt; i++)
				{
					arrayOfLastNames[i] = outputLastNames.ReadLine();

					Console.WriteLine();
				}

			}

			int N = arrayOfLastNames.Length;
			int W = arrayOfLastNames[0].Length;
			string[] temp = new string[vehicleCountInt];

			for (int d = W - 1; d >= 0; d--)
			{
				int[] count = new int[vehicleCountInt];
				for (int i = 0; i < N; i++)
				{
					count[arrayOfLastNames[i][d] + 1]++; //The part that causes the IndexOutOfRange exception
				}
				for (int k = 1; k < 256; k++)
				{
					count[k] += count[k - 1];
				}
				for (int i = 0; i < N; i++)
				{
					temp[count[arrayOfLastNames[i][d]]++] = arrayOfLastNames[i];
				}
				for (int i = 0; i < N; i++)
				{
					arrayOfLastNames[i] = temp[i];

					Console.WriteLine("{0}", arrayOfLastNames[i]);
				}
			}
		}



By the way this is one of the text file i'll be sorting:
Hibaler
Balde
Metin
Dela Vega
Dimagiba
Pederoso
Dinulos
Du
Gotengco
Lira
Guban
Garraez
Carpena
Reyes
Zulueta
Gonzalez
Isidoro
Marfa
Reyes
Guinhawa
Aaron
Lazaro
Obsum
Molino
Estenor
Nolasco
Paral
Porillo
Novillos
Alihan

This post has been edited by angelvale: 03 September 2009 - 03:54 AM

Was This Post Helpful? 0
  • +
  • -


#2 MentalFloss  Icon User is offline

  • Over-engineer Extraordinaire
  • Icon

Reputation: 131
  • View blog
  • Posts: 689
  • Joined: 02-September 09


Dream Kudos: 50

Expert In: C#

Re: Sorting strings from text file using Radix Sort

Posted 03 September 2009 - 10:31 AM

Perhaps I am going to end up going outside of the scope of your requirements, but really, all you need to accomplish this is:

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

namespace SortLastNameDemo
{
	class Program
	{
		static void Main(string[] args)
		{
			List<string> conveyances = LoadConveyanceList();
			PrintSortedConveyanceList(conveyances);
			Console.ReadLine();
		}

		static List<string> LoadConveyanceList()
		{
			List<string> conveyanceList = new List<string>();
			string path = Path.Combine(Environment.CurrentDirectory, "Conveyance.txt");

			using (StreamReader reader = new StreamReader(path))
			{
				string line = String.Empty;

				while ((line = reader.ReadLine()) != null)
				{
					conveyanceList.Add(line);
				}
			}
			return conveyanceList;
		}

		static void PrintSortedConveyanceList(List<string> conveyanceList)
		{
			conveyanceList.Sort();

			foreach (string conveyance in conveyanceList)
			{
				Console.WriteLine(conveyance);
			}
		}
	}
}



Output is:

Aaron
Alihan
Balde
Carpena
Dela Vega
Dimagiba
Dinulos
Du
Estenor
Garraez
Gonzalez
Gotengco
Guban
Guinhawa
Hibaler
Isidoro
Lazaro
Lira
Marfa
Metin
Molino
Nolasco
Novillos
Obsum
Paral
Pederoso
Porillo
Reyes
Reyes
Zulueta



Again, sorry if this is against your instructor's direction, but it just seems like what you've got is a lot of clutter code. Re-inventing the wheel if you will.
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month