Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Sorting String Using Radix Sort

Sorting String Using Radix Sort

Scheduled Pinned Locked Moved C#
questioncsharpjavaalgorithms
14 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G gamer1127

    This is the source code that I got. It's a msd Radix sort:

    public static void msd(String[] a)
    {
    msd(a, 0, a.Length, 0);
    }

        private static void msd(String\[\] a, int l, int r, int d)
        {
            int N = a.Length;
    
            if (r <== l + 1) return;
            int\[\] count = new int\[256\];
            for (int i = 0; i < N; i++)
                count\[a\[i\].charAt(d) + 1\]++;
            for (int k = 1; k < 256; k++)
                count\[k\] += count\[k-1\];
            for (int i = 0; i < N; i++)
                temp\[count\[a\[i\].charAt(d)\]++\] = a\[i\];
            for (int i = 0; i < N; i++)
                a\[i\] = temp\[i\];
            for (int i = 1; i < 255; i++)
                msd(a, l + count\[i\], l + count\[i+1\], d+1);
        }
    

    How will I revise this so I can use it in c#?

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #5

    count[a[i].charAt(d) + 1] -> count[a[i][d] + 1]

    1 Reply Last reply
    0
    • G gamer1127

      Okay. So my first attempt in sorting the strings using Radix Sort is a failure(The one with converting all strings into ASCII code). Now, what i want to do is use the strings directly to sort them out using Radix Sort. Is there any available source code that can sort strings using Radix Sort? I already got one source code but I think it's implemented in Java. I don't have any time to make my own code since it will be passed on saturday(GMT +8:00). One question though. What programming language that uses CharAt() method? Is that Java? If so, what is the equivalent method of that in C#?

      G Offline
      G Offline
      gamer1127
      wrote on last edited by
      #6

      I'm still having a problem with my string sorting. I always get the IndexOutOfRange exception during runtime. How will I solve that?

      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 + 1\]; ;
      
                  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 run-time error
                  }
                  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\]);
                  }
              }
          }
      
      L 1 Reply Last reply
      0
      • G gamer1127

        I'm still having a problem with my string sorting. I always get the IndexOutOfRange exception during runtime. How will I solve that?

        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 + 1\]; ;
        
                    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 run-time error
                    }
                    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\]);
                    }
                }
            }
        
        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #7

        You don't need ref for those arrays, you're not assigning to them But wait, how can you be sure that those arrays are long enough? tip: always tell where you get the exception. I suspect it happens here: arrayOfLastNames[i] = outputLastNames.ReadLine(); Since there is no guarantee that arrayOfLastNames is long enough (its length is unknown)

        G 1 Reply Last reply
        0
        • L Lost User

          You don't need ref for those arrays, you're not assigning to them But wait, how can you be sure that those arrays are long enough? tip: always tell where you get the exception. I suspect it happens here: arrayOfLastNames[i] = outputLastNames.ReadLine(); Since there is no guarantee that arrayOfLastNames is long enough (its length is unknown)

          G Offline
          G Offline
          gamer1127
          wrote on last edited by
          #8

          Ok..Is there a way on how to solve that problem? I'm thinking of declaring an array that is large enough for me to store those names.

          L 1 Reply Last reply
          0
          • G gamer1127

            Ok..Is there a way on how to solve that problem? I'm thinking of declaring an array that is large enough for me to store those names.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #9

            I hope you aren't asking for a way to declare an array of a given size.. int[] b = new int[vehicleCountInt + 1]; is one such declaration, but probably 1 too big (why are you adding one?) If you are asking how to assign to an array which is a function parameter, well, ref is used for exactly that purpose, but out may be sufficient. But this "problem" is as trivial as the first.

            G 1 Reply Last reply
            0
            • L Lost User

              I hope you aren't asking for a way to declare an array of a given size.. int[] b = new int[vehicleCountInt + 1]; is one such declaration, but probably 1 too big (why are you adding one?) If you are asking how to assign to an array which is a function parameter, well, ref is used for exactly that purpose, but out may be sufficient. But this "problem" is as trivial as the first.

              G Offline
              G Offline
              gamer1127
              wrote on last edited by
              #10

              Well I'm referring to how will I solve the problem about the uncertainty of the array's size. I've declared 5 text files with 30 data inside. Now what my groupmate did was create a text file and put there the total number of data inside the text files, and that's the vehicleCount. Now what I'm thinking is declare an array that has a definite size (say 50) which will hold the data inside the text file(the 30 predefined data and the user's additional inputs). Will this solve the problem about the IndexOutOfRange exception?

              L 1 Reply Last reply
              0
              • G gamer1127

                Okay. So my first attempt in sorting the strings using Radix Sort is a failure(The one with converting all strings into ASCII code). Now, what i want to do is use the strings directly to sort them out using Radix Sort. Is there any available source code that can sort strings using Radix Sort? I already got one source code but I think it's implemented in Java. I don't have any time to make my own code since it will be passed on saturday(GMT +8:00). One question though. What programming language that uses CharAt() method? Is that Java? If so, what is the equivalent method of that in C#?

                G Offline
                G Offline
                ghelhei
                wrote on last edited by
                #11

                [Message Deleted]

                G 1 Reply Last reply
                0
                • G ghelhei

                  [Message Deleted]

                  G Offline
                  G Offline
                  ghelhei
                  wrote on last edited by
                  #12

                  string strRaw = "Thisisastring"; char[] astrSorted = strRaw.ToCharArray().ToArray(); Array.Sort(astrSorted); string strSorted = String.Empty; foreach (char strChar in astrSorted) { strSorted += strChar; } eto try mo dn tong code nato

                  1 Reply Last reply
                  0
                  • G gamer1127

                    Well I'm referring to how will I solve the problem about the uncertainty of the array's size. I've declared 5 text files with 30 data inside. Now what my groupmate did was create a text file and put there the total number of data inside the text files, and that's the vehicleCount. Now what I'm thinking is declare an array that has a definite size (say 50) which will hold the data inside the text file(the 30 predefined data and the user's additional inputs). Will this solve the problem about the IndexOutOfRange exception?

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #13

                    Declaring an array to be intentionally too big is not really nice You could make it a List or read the file all at once and then split on newlines

                    G 1 Reply Last reply
                    0
                    • L Lost User

                      Declaring an array to be intentionally too big is not really nice You could make it a List or read the file all at once and then split on newlines

                      G Offline
                      G Offline
                      gamer1127
                      wrote on last edited by
                      #14

                      Your right...It didn't solve the problem..I'm sticking to the original code that we created (having a text file that acts like a counter).

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups