• 推荐
  • 评论
  • 收藏

C#中定义数组和一个简单排序算法实现。

2022-12-19    8443次浏览
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApp20110730
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = 18;
            int[] a = new int[18]{ 0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5};
            int[] b = new int[18] { 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5 };
            
            Console.WriteLine("排序前:");
            Console.WriteLine("初始化输入A【18】:");
            foreach (var item in a)
            {
                Console.Write(item);
            }
            Console.WriteLine("");
            Console.WriteLine("初始化输入A【18】结束。");
          
            Console.WriteLine("初始化输出B【18】:");
            foreach (var item in b)
            {
                Console.Write(item);
            }
            Console.WriteLine("");
            Console.WriteLine("初始化输入B【18】结束。");

            Console.WriteLine("开始排序。。。。");
            Sort(n,a,b);
            Console.WriteLine("排序结束。");
            Console.WriteLine("排序后:");
            Console.WriteLine("初始化输入A【18】:");
            foreach (var item in a)
            {
                Console.Write(item);
            }
            Console.WriteLine("");
            Console.WriteLine("初始化输入A【18】结束。");

            Console.WriteLine("初始化输出B【18】:");
            foreach (var item in b)
            {
                Console.Write(item);
            }
            Console.WriteLine("");
            Console.WriteLine("初始化输入B【18】结束。");
        }

        private static void Sort(int n, int[] a, int[] b)
        {
            try
            {
                int r = 6;
                int[] R = new int[r];
                for (int i = 0; i < r; i++)
                {
                    R[i] = 0;
                }

                for (int i = 0; i < n; i++)
                {
                    R[a[i]] = R[a[i]] + 1;
                }

                for (int i = 1; i < r; i++)
                {
                    R[i] = R[i] + R[i - 1];
                }

                for (int i =(n-1); i >= 0; i--)
                {
                    b[R[a[i]] - 1] = a[i];
                    R[a[i]] = R[a[i]] - 1;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}

  

C#中定义数组

一、一维:
int[] numbers = new int[]{1,2,3,4,5,6}; //不定长 
int[] numbers = new int[3]{1,2,3};//定长 
二、多维 
int[,] numbers = new int[,]{{1,2,3},{1,2,3}}; //不定长 
int[,] numbers = new int[2,2]{{1,2},{1,2}}; //定长 

三、例子
A:int[] mf1=new int[6]; 
      //注意初始化数组的范围,或者指定初值; //包含6个元素的一维整数数组,初值1,2,3,4,5,6 
      int[] mf2=new int[6]{1,2,3,4,5,6}; 

B://一维字符串数组,如果提供了初始值设定项,则还可以省略 new 运算符 
      string[] mf3={"c","c++","c#"}; 

C://一维对象数组 
      Object[] mf4 = new Object[5] { 26, 27, 28, 29, 30 }; 

D://二维整数数组,初值mf5[0,0]=1,mf5[0,1]=2,mf5[1,0]=3,mf5[1,1]=4 
      int[,] mf5=new int[,]{{1,2},{3,4}}; 

E://6*6的二维整型数组 
      int[,] mf6=new mf[6,6];  

四、取得数组元素个数:
        int   b;   
        b   =   sizeof   (a)/sizeof   (*a);   
原文地址:https://www.cnblogs.com/Leo_wl/p/2121892.html