c# - 我需要使用 c#.net 在选项 b 中合并排序或快速排序我的字符串
问题描述
这是我的代码,我不知道如何使用此代码启用快速排序或合并排序。
我在 foreach 和 userInput 的情况下得到红线。但是如果我在我的 userInput 上使用 string[],我的 Console.Readline() 将是红线,也是 ToCharArray。我将如何解决它?
我想知道为什么它不起作用并帮助我重新编写一个更好的代码。
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("\ta) - Bubble Sort");
Console.WriteLine("\tb) - 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 = '\0';
break;
}
} while (response == '\0');
}
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);
}
}
}
}
解决方案
所以你的方法需要采取char[]
static int partition(char[] userInput, int start, int end)
static void quicksort(char[] userInput, int start, int end)
你需要给他们一个char[]
// this is needed to have copy of the mutated array
var charArray = userInput.ToCharArray();
quicksort(charArray, 0, userInput.Length - 1);
您需要将 for 循环更改为 char 以防万一
foreach (char s in charArray)
您的temp
变量 inpartition
将需要是char
char temp;
而不是string.Compare
你可以只使用大于或小于
while (i < end && userInput[i] < userInput[pivot])
i++;
while (j > start && userInput[j] > userInput[pivot])
j--;
这忽略了代码中的任何其他问题,只专注于让它用 char 数组编译
推荐阅读
- ubuntu - 如何构建旧版本的 glibc?
- javascript - 在哪里可以找到 Google Maps API 库源代码?
- javascript - 此代码中的位运算符“OR”(JavaScript)
- android - “无法到达 Firestore 后端”,多个 Firebase 应用,Android
- ios - 如何将箭头附加到 UIBezierPath
- java - Kafka 生产者给出 TimeoutException
- java - 什么是类 InfoChyb 的方法返回类型
- c++ - 循环调用函数一次 C++
- laravel - Laravel 多态 hasMany 关系
- react-native - 当我想过滤列表时,哪种方式更好?