简体   繁体   中英

Return error for Quick Sort algorithm using Strategy Design Patterns C#

I have a hw on using strategy design patterns for the different sorting algorithms. The bubble sort has already been done but I get an error in my Quick Sort Algorithm

    **The error code is:    CS0161  'QuickSort.Sort(string)': not all code paths return a value**   
    
        

for the main menu

        namespace Yousource.Strategy.App
        {
            using System;
        
            /**
             * Instructions:
             * Use the Strategy Pattern to implement the different Sorting Algorithms: BubbleSort (given as an example), Quick Sort and Merge Sort
             */
            class Program
            {
                static void Main(string[] args)
                {
                    Console.WriteLine("Enter Sort Strategy (bubblesort, quicksort, mergesort). Defaults to bubblesort");
                    ISortStrategy strategy = default;
        
                    var input = Console.ReadLine();
                    input = input.ToLower();
        
                    switch (input)
                    {
                        case "bubblesort":
                            strategy = new BubbleSort();
                            break;
        
        
                        case "mergesort":
                            strategy = new MergeSort();
                            break;
        
        
                        case "quicksort":
                            strategy = new QuickSort();
                            break;
                        
                    
        
                        default:
                            strategy = new BubbleSort();
                            break;
        
                    }
                    Console.WriteLine("Enter String to Sort");
                    var value = Console.ReadLine();
        
                    Console.Write("The sorted string is: " + strategy.Sort(value));
        
                    Console.ReadKey();
                }
            }
        }
        

for the Strategy Pattern

    namespace Yousource.Strategy.App
    {
        public interface ISortStrategy
        {
            string Sort(string input);
        }
    }

for the Quick Sort process

        namespace Yousource.Strategy.App
        {
            public class QuickSort : ISortStrategy
            {
                public string Sort(string input)
                {
                    var result = "";
                    var arr = input.ToCharArray();
                    char temp;
        
                    for (int write = 0; write < arr.Length; write++)
                    {
                        for (int sort = 0; sort < arr.Length - 1; sort++)
                        {
                            if (arr[sort] > arr[sort + 1])
                            {
                                temp = arr[sort + 1];
                                arr[sort + 1] = arr[sort];
                                arr[sort] = temp;
                            }
                        }
                    }
        
                    for (int i = 0; i < arr.Length; i++)
                        result += arr[i];
        
                    return result;
                }
            }
        }

Confused compiler. Put using System; outside the namespace.

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