Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
140 views
in Technique[技术] by (71.8m points)

I need to merge sort or quick sort my string in option b using c#.net

here are my codes i don't know how to enable the quick sort or merge sort using this code.

i got red lines in case b on foreach and userInput. but if i use string[] on my userInput my Console.Readline() will be red line and also the ToCharArray. how will i fix it?

i would like to know why isn't it working and help me to recode a better one.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SortString
{
    class SortStringCategory
    {
        public static void Main(string[] args)
        {

            Console.Write("Enter a string: ");

            String userInput = Console.ReadLine();

            char response;

            do
            {
                Console.WriteLine();
                Console.WriteLine("Choose between the two sorting strategies:");
                Console.WriteLine("a) - Bubble Sort");
                Console.WriteLine("b) - Quick Sort");
                Console.WriteLine();
                Console.Write("Your option: ");

                {
                    response = char.Parse(Console.ReadLine());
                }

                Console.WriteLine();

                switch (response.ToString().ToLower())
                {
                    case "a":

                        char temp;
                        char[] charStr = userInput.ToCharArray();
                        for (int i = 1; i < charStr.Length; i++)
                        {
                            for (int j = 0; j < charStr.Length - 1; j++)
                            {
                                if (charStr[j] > charStr[j + 1])
                                {
                                    temp = charStr[j];
                                    charStr[j] = charStr[j + 1];
                                    charStr[j + 1] = temp;
                                }
                            }
                        }

                        Console.Write("Bubble Sort: ");
                        Console.Write(charStr);
                        break;


                    case "b":

                        quicksort(userInput, 0, userInput.Length - 1);
                        foreach (string s in userInput)
                        {
                            Console.Write(s + " ");
                        }
                        Console.ReadKey();
                        break;


                    default:
                        Console.WriteLine("Invalid answer. Please enter a valid option.");
                        response = '';
                        break;
                }

            } while (response == '');

        }

        static int partition(string[] userInput, int start, int end)
        {
            int pivot = end;
            int i = start, j = end;
            string temp;
            while (i < j)
            {
                while (i < end && string.Compare(userInput[i], userInput[pivot]) < 0)
                    i++;
                while (j > start && string.Compare(userInput[j], userInput[pivot]) > 0)
                    j--;

                if (i < j)
                {
                    temp = userInput[i];
                    userInput[i] = userInput[j];
                    userInput[j] = temp;
                }
            }
            temp = userInput[pivot];
            userInput[pivot] = userInput[j];
            userInput[j] = temp;
            return j;
        }

        static void quicksort(string[] userInput, int start, int end)
        {
            if (start < end)
            {
                int pivotIndex = partition(userInput, start, end);
                quicksort(userInput, start, pivotIndex - 1);
                quicksort(userInput, pivotIndex + 1, end);
            }
        }

    }
}
question from:https://stackoverflow.com/questions/66060006/i-need-to-merge-sort-or-quick-sort-my-string-in-option-b-using-c-net

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So your methods will need to take a char[]

static int partition(char[] userInput, int start, int end)

static void quicksort(char[] userInput, int start, int end)

You will need to give them a char[]

// this is needed to have copy of the mutated array
var charArray = userInput.ToCharArray();
quicksort(charArray, 0, userInput.Length - 1);

You will need to change the for loop to char in case b

foreach (char s in charArray)

Your temp variable in partition will need to be a char

char temp;

And instead of string.Compare you can just use greater than or less than

while (i < end && userInput[i] < userInput[pivot])
      i++;
while (j > start && userInput[j] > userInput[pivot])
      j--;

This disregards any other problem in your code, and only focuses on getting this to compile with a char array


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...