using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
冒泡排序升序排序,降序变换大于小于的符号即可,Array.Sort(nums);是直接对数组进行升序的方法,但是Array.Reverse(nums);不是对数组进行
降序的方法,而是一个将数组反转输出的方法。
int[] nums = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
Array.Sort(nums);
//只能针对数组做一个升序的排列
Array.Reverse(nums);
//对数组进行反转
//纯冒泡排序
for (int i = 0; i < nums.Length - 1; i++)
{
for (int j = 0; j < nums.Length - 1 - i; j++)
{
if (nums[j] > nums[j + 1])
{
int temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
// 输出,升序排序
for (int i = 0; i < nums.Length; i++)
{
Console.WriteLine(nums[i]);
}
数组反转的具体代码
string[] sentence = { "北京", "要去", "吗", "你" };
for (int i = 0; i < sentence.Length / 2; i++)//N个元素完全颠倒 要交换N/2次
{
string temp = sentence[i];
sentence[i] = sentence[sentence.Length - 1 - i];
sentence[sentence.Length - 1 - i] = temp;
}
//输出反转后的数组
for (int i = 0; i < sentence.Length; i++)
{
Console.WriteLine(sentence[i]);
}