当前位置:编程学习 > C#/ASP.NET >>

怎样用c#语言编写一个有20个数从大到小的函数(帮我检查一下我的这个函数怎么不对),谢

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

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] a = new int[20];
            for (int i = 0; i < 20; i++)
                for (int j = 0; j < 20 - i; j++)
                    if (a[i] > a[i + 1])
                    {
                        int t;
                        t = a[i];
                        a[i] = a[i + 1];
                        a[i + 1] = t;
                    }

            {
                for (int i = 0; i < 20; i++)
                {
                    for (int j = 0; j < 20 - i; j++)
                        Console.Write(a[i]);



                }

                Console.WriteLine();

            }
        }
    }
}
--------------------编程问答-------------------- 冒泡排序?
测试10个数。

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

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] a = new int[10] { 1, 63, 55, 18, 35, 2, 66, 84, 98, 21 };
            for (int i = 0; i < a.Length; i++)
                for (int j = 0; j < a.Length - i - 1; j++)
                    if (a[j] < a[j + 1])
                    {
                        int t = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = t;
                    }
            for (int i = 0; i < a.Length; i++)
                Console.WriteLine(a[i] + "");
            Console.ReadLine();

        }
    }
}

--------------------编程问答--------------------
引用楼主 cq18790864967 的回复:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication4
{
  class Program
  {
  static void Main(string[] args)
  {
  int[] a = new int[20]

你这是长度为20,但是数组里面没东西! 可以考虑楼上的做法. 排序有比较跟冒泡等几种做法 --------------------编程问答-------------------- 使用 SortedList,它已经实现了排序算法。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,