按升序对数组进行排序 [英] Sorting an array into ascending order
问题描述
我对编码非常陌生,并且几个小时都在盯着这段代码...
我理解for语句中的参数如何工作当只是将一系列数字打印到控制台时,但在此示例中,它按升序对值进行排序。
对我而言,参数看起来非常相似,我可以看出它们是不同的,但它看起来好像是使用相同的原理。我只是希望有人可以解释这实际上如何按顺序对数组进行排序,我无法解决这个问题。
我尝试了什么:
const int arrayLength = 5 ;
int [] sortArray = new int < /跨度> [arrayLength];
for ( int i = 0 ; i < arrayLength; i ++)
{
Console.WriteLine( 输入值:);
sortArray [i] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine( 现在排序:);
for ( int j = 0 ; j < arrayLength; j ++)
{
for (< span class =code-keyword> int k = j + 1 ; k < arrayLength - 1 ; k ++)
{
if (sortArray [j] > sortArray [k])
{
int big = sortArray [j ]。
sortArray [j] = sortArray [k];
sortArray [k] = big;
}
}
}
Console.WriteLine( 现在打印出来:);
for ( int l = 0 ; l < sortArray.Length; l ++)
{
Console.WriteLine(sortArray [l]);
}
< blockquote>这是你的作业,但是...这是一种奇怪的排序算法。
我这样做的方式(好吧,如果我必须写一个基本的排序算法)就是做一个冒泡排序:冒泡排序 - 维基百科 [ ^ ]
你们不这样做 - 它有点停止,所以输出顺序有点奇怪。 br />
但它很容易解决!只需改变这个:
for(int k = j + 1; k< arrayLength - 1; k ++)
对此:
for(int k = j + 1; k< arrayLength; k ++)
它被称为冒泡排序 ,现在你知道它叫做什么你可以google了,有很多文章解释它的动画
算法第1课:Bubblesort - YouTube [ ^ ]
您可以使用 Linq :
使用系统;
使用System.Collections.Generic;
使用System.Linq;
公共课程
{
public static void Main()
{
List< int> li = new List< int>();
li.Add(1);
li.Add(2);
var lidesc = li.OrderByDescending(i => i);
foreach(l l中的var l)
{
Console.WriteLine(number+ l);
}
}
}
I'm very new to coding and have been staring at this bit of code for hours...
I understand how the parameters in the for statement work when just printing out a range of numbers to the console but in this example it is sorting the values in ascending order.
To me the parameters look very similar, I can see that they're different but it does look as though it is using the same principle. I was just hoping someone could explain how this actually sorts the array into ascending order, I cannot work it out.
What I have tried:
const int arrayLength = 5;
int[] sortArray = new int[arrayLength];
for (int i = 0; i < arrayLength; i++)
{
Console.WriteLine("Enter value: ");
sortArray[i] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("Now sort it: ");
for (int j = 0; j < arrayLength; j++)
{
for (int k = j + 1; k < arrayLength - 1; k++)
{
if (sortArray[j] > sortArray[k])
{
int big = sortArray[j];
sortArray[j] = sortArray[k];
sortArray[k] = big;
}
}
}
Console.WriteLine("Now print it: ");
for (int l = 0; l < sortArray.Length; l++)
{
Console.WriteLine(sortArray[l]);
}
This is your homework, but ... that's an odd sort algorithm.
The way I'd do it (well, if I had to write a basic sort algorithm) would be to do a bubble sort: Bubble sort - Wikipedia[^]
Yours doesn't do that - it kinda stops too early, so the output order is a little odd.
But it's easy to solve! Just change this:
for (int k = j + 1; k < arrayLength - 1; k++)To this:
for (int k = j + 1; k < arrayLength; k++)
It's called a "bubble sort", now you know what it's called you can google for it, there are plenty of articles\animations that explain it
Algorithms Lesson 1: Bubblesort - YouTube[^]
You can use Linq:
using System; using System.Collections.Generic; using System.Linq; public class Program { public static void Main() { List<int> li = new List<int>(); li.Add(1); li.Add(2); var lidesc = li.OrderByDescending(i => i); foreach (var l in lidesc) { Console.WriteLine("number " + l); } } }
这篇关于按升序对数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!