qq_30137777 于 2015.07.27 22:49 提问

c#数组元素排序的问题

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

namespace 数组排序_升序排列_
{
class Program
{
static void Main(string[] args)
{
int[] myArray = new int[10] { 10, 54, 34, 45, 4, 8, 38, 9, 7, 11 };
Console.WriteLine("原始数组为： ");
foreach (int number in myArray)
{
Console.Write(number + " ");
}
Console.WriteLine();
int i, j, t, min;
for (i = 0; i < 10; i++)
{
min = i;
for (j = i + 1; j < 10; j++)
{
if (myArray[j] < myArray[min])
{
min = j;
}
}
if (min != i)
{
t = myArray[i];
myArray[i] = myArray[min];
myArray[min] = t;
}
Console.WriteLine("排序后数组为： ");
foreach (int number in myArray)
{
Console.Write(number +" ");
}
}
}
}
}

3个回答

guwei4037      2015.07.27 23:01

class Program
{
static void Main(string[] args)
{
int[] myArray = new int[10] { 10, 54, 34, 45, 4, 8, 38, 9, 7, 11 };
Console.WriteLine("原始数组为： ");
foreach (int number in myArray)
{
Console.Write(number + " ");
}
Console.WriteLine();
int i, j, t, min;
for (i = 0; i < 10; i++)
{
min = i;
for (j = i + 1; j < 10; j++)
{
if (myArray[j] < myArray[min])
{
min = j;
}
}
if (min != i)
{
t = myArray[i];
myArray[i] = myArray[min];
myArray[min] = t;
}
}

Console.WriteLine("排序后数组为： ");
foreach (int number in myArray)
{
Console.Write(number + " ");
}
}
}
qq_30137777 懂了，谢谢
2 年多之前 回复
CSDNXIAOD   2015.07.27 23:00

----------------------biu~biu~biu~~~在下问答机器人小D，这是我依靠自己的聪明才智给出的答案，如果不正确，你来咬我啊！

bailiningshuang   2015.07.28 08:50

int[] myArray = new int[10] { 10, 54, 34, 45, 4, 8, 38, 9, 7, 11 };
var query1 = myArray.OrderBy(val => val);//升序
var query2 = myArray.OrderByDescending(val => val);//降序
foreach (var item in query1)
{
Console.Write(item);
}
foreach (var item in query2)
{
Console.Write(item);
}