简体   繁体   中英

Quick Sort using C#

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;
    
    
            }

View

public void QuickSort()
        {
            Sort word = new Sort();
            word.Word = sortView.WordText;
            sortView.result = word.QuickSort();
        }

Contoller

public interface ISort
    {
        string WordText { get; set; }
        string result { get; set; }

    }

Form

 public partial class Form1 : Form, ISort
    {
        public Form1()
        {
            InitializeComponent();
        }

        public string WordText { 
            
            get 
            { 
                return enterString.Text; 
            } 
            set 
            {
                enterString.Text = value;
            } 
        
        }
        public string result
        {

            get
            {
                return sortResult.Text;
            }
            set
            {
                sortResult.Text = value;
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnSort_Click(object sender, EventArgs e)
        {
            SortView view = new SortView(this);
            string selectSort = comSort.Text;
            switch (selectSort)
            {
                case "MergeSort": view.MergeSort();break;
                case "QuickSort": view.QuickSort(); break;

            }
           
        }
    }

This is the example output

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM