在VS代码中,对于C#来说是全新的,而对于C#来说则是全新的。
我不确定我的代码或应用程序文件是否存在问题,无论哪种情况,我收到的返回值都是System.Int32 [],而不是实际的数组内容。
我创建了一个控制台项目-dotnet新控制台-n“ algos”
我添加了一个解决方案文件-dotnet new sln -n“ algorythems_solution”
我将项目添加到解决方案-dotnet sln algorythems_solution.sln添加./algos/algos.csproj
要运行该程序,我使用了f5和-dotnet run
using System;
namespace algos
{
class sortingAlgorythems
{
static void Main()
{
int[] test = {-4,5,10,8,-10,-6,-4,-2,-5,3,5,-4,-5,-1,1,6,-7,-6,-7,8};
int[] sorted = bubbleSort(test);
Console.WriteLine(sorted.ToString());
}
public static int[] bubbleSort(int[] array)
{
bool isSorted = false;
int toValue = array.Length - 1;
int fromValue = 0;
while (isSorted == false)
{
isSorted = true;
int cnt = 0;
for (int i = fromValue; i < toValue; i++)
{
if (array[i] > array[i+1])
{
cnt = i;
int temp1 = array[i];
int temp2 = array[i+1];
array[i] = temp2;
array[i+1] = temp1;
if (isSorted == true)
{
isSorted = false;
if (i > 0)
{
fromValue = i -1;
}
}
}
}
toValue = cnt;
}
return array;
}
}
}
解决方案:
int[] sorted = bubbleSort(test);
for(int i = 0; i < sorted.Length; ++i)
{
Console.WriteLine($"{i}: {sorted[i]}");
}
本文翻译至stackoverflow,原文地址:https://stackoverflow.com/questions/60593537/c-sharp-bubble-sort-function-returning-data-type-not-results-code-issue-or-fil,侵删。
相关阅读 >>
c#冒泡排序功能返回数据类型不一致?代码问题还是文件设置问题?
更多相关阅读请进入《冒泡排序》频道 >>
C#高级编程(第11版) C# 7 & .NET Core 2.0(.NET开发经典名著)
作者:[美]克里斯琴·内格尔(Christian Nagel)著。出版时间:2019年3月。