【发布时间】:2021-08-01 21:37:51
【问题描述】:
这是我的截图 output 我的选择排序算法的降序排列
我的问题是我的代码不接受任何负数,我不知道我的 IDE 是否有问题,因为它接受升序但不降序的负数...
int[] SelSort = new int[1000];
int num;`enter code here`
Console.Clear();
Console.Write("Enter the number of elements: ");
int elements = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\nEnter that " + elements + " elements you want to put:");
for (int B = 0; B < elements; B++)
{
SelSort[B] = Convert.ToInt32(Console.ReadLine());
}
// Shows the Unsorted Elements you put in selection sort
Console.WriteLine("\n");
Console.Write("The Unsorted Elements are: ");
for (int S = 0; S < elements; S++)
{
Console.Write(SelSort[S] + " ");
}
Console.WriteLine("\n");
Console.WriteLine("Would you like to sort it to the following:" +
"\n[1] - Ascending Order" +
"\n[2] - Descending Order\n");
Console.Write("Choose what to do on the inputed elements: ");
int selection = Convert.ToInt32(Console.ReadLine());
for (int A = 0; A < SelSort.Length - 1; A++)
{
for (int P = A + 1; P < SelSort.Length; P++)
{
if (SelSort[A] < SelSort[P])
{
num = SelSort[A];
SelSort[A] = SelSort[P];
SelSort[P] = num;
}
}
}
Console.WriteLine("\n");
Console.Write("Sorted Elements using Selection Sort in Descending Order: ");
for (int i = 0; i < elements; i++)
{
Console.Write(SelSort[i] + " ");
}
【问题讨论】:
-
您是否考虑过使用内置排序选项?
-
int[]存储签名号码。它怎么可能不接受负数?您是否收到某种错误消息? -
如果您输入例如5 个元素,数组中还有 995 个值为 0 的元素。也许这就是你所困惑的?将
A < SelSort.Length替换为A < elements(同样适用于P 循环)。 -
请提供文本作为文本,而不是文本的屏幕截图。
-
关闭这个问题的原因毫无意义。 OP 提供的代码完美地重现了他的问题,并且可以很容易地回答这个问题。因此,我投票决定重新提出问题。
标签: c# sorting negative-number selection-sort negative-integer