0

Hello I trying to do a QuickSort but didn't appear the result, For example the user input "cdabe" so the expected result is "abcde". May I know what is the cause why the result didn't display? because There is no error in the code. I'm using MVC. My MergeSort is working properly but my QuickSort didn't.

Model :

public string QuickSort()
            {
                string arrangedSort = "";
                string Word= "cdabe";
                List<string> ListLetters = new List<string>();
                for (int i = 0; i < Word.Length; i++)
                {
                    ListLetters.Add(Word.Substring(i, 1)); 
                }
               
                aQuicksort(Word, 0, ListLetters.Count - 1);
    
             
              void aQuicksort(string Word, int left, int right)
                {
                    int i = left;
                    int j = right;
    
                    var pivot = Word[(left + right) / 2];
    
                    while (i <= j)
                    {
                        while (char.Parse(ListLetters[i]) < pivot)
                            i++;
    
                        while (char.Parse(ListLetters[i]) > pivot)
                            j--;
    
                        if (i <= j)
                        {
                            var tmp = ListLetters[i];
                            ListLetters[i] = ListLetters[j];
                            ListLetters[j] = tmp;
    
                            i++;
                            j--;
                        }
                    }
    
                    if (left < j)
                        aQuicksort(Word, left, j);
    
                    if (i < right)
                        aQuicksort(Word, i, right);
    
                    foreach (var listLetter in ListLetters)
                    {
                        arrangedSort += listLetter;
                    }
                    
    
                }
    
                return arrangedSort;
    
    
            }
anony
  • 49
  • 4
  • what is the cause why the result didn't display? - because you did not include any `Console.writeLine` or such – inquisitive Oct 30 '21 at 05:25
  • Your code is very difficult to troubleshoot. `Word` is both a parameter *and* a variable in embedded function. `aquicksort` is local function for no particular reason. It is all syntactically legal, but makes it prone to errors. I assume that you are new to C# - so I recommend to clean / simplify your code, and then do some debugging – Felix Oct 30 '21 at 05:32
  • I am sorry, after your edit, the question doesn't make any sense any more. This is the model? controller? It was better earlier – Felix Oct 30 '21 at 05:46
  • In C# strings can be treated as if they are arrays of chars. You can ask for `"hello world"[4]` and get `'o'` You can also convert a string into an array of chars with `.ToCharArray` - so that entire bit where you sub string a string up into a list of strings and then parse them to chars - just throw it away and do one ToCharArray, and do all the work with char arrays, keep the array as chars all the way til it's sorted then turn it back into a string with `new string(theArray)`. At the moment all the string manipulation you're doing is making this a rather slow quick sort .. – Caius Jard Oct 30 '21 at 05:47
  • To do quicksort in C#, use `Array.Sort` or `List.Sort` – Charlieface Oct 30 '21 at 19:18
  • Well, your second while loop is an infinite loop. – Nemavhidi Oct 31 '21 at 10:45

1 Answers1

-1

Try this implementation, it uses LinQ using System.linq

public static IEnumerable<int> QSort3(IEnumerable<int> source)
    {
        if (!source.Any())
            return source;
        int first = source.First();

        QSort3Helper myHelper = 
          source.GroupBy(i => i.CompareTo(first))
          .Aggregate(new QSort3Helper(), (a, g) =>
              {
                  if (g.Key == 0)
                      a.Same = g;
                  else if (g.Key == -1)
                      a.Less = g;
                  else if (g.Key == 1)
                      a.More = g;
                  return a;
              });
        IEnumerable<int> myResult = Enumerable.Empty<int>();
        if (myHelper.Less != null)
            myResult = myResult.Concat(QSort3(myHelper.Less));
        if (myHelper.Same != null)
            myResult = myResult.Concat(myHelper.Same);
        if (myHelper.More != null)
            myResult = myResult.Concat(QSort3(myHelper.More));

        return myResult;
    }

    public class QSort3Helper
    {
        public IEnumerable<int> Less;
        public IEnumerable<int> Same;
        public IEnumerable<int> More;
    }

code from this post

Dean Van Greunen
  • 5,060
  • 2
  • 14
  • 28