C#数组反转与排序实例分析

所属分类: 软件编程 / C#教程 阅读数: 49
收藏 0 赞 0 分享

本文实例分析了C#数组反转与排序的方法。分享给大家供大家参考。具体实现方法如下:

C#数组反转

复制代码 代码如下:
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
 
namespace 数据反转 

    class Program 
    { 
        static void Main(string[] args) 
        { 
            string[] strAllay = { "毛泽东", "李世民", "秦始皇", "成吉思汗", "习近平","邓小平"}; 
            string s; 
            for (int i = 0; i < strAllay.Length / 2; i++)//strAllay.Length/2是因为经过(将数组的长度值除以2)次就可以将数组成员进行反转了 
            { 
                s = strAllay[i]; 
                strAllay[i] = strAllay[strAllay.Length - 1 - i];//如果i等于数组第一项值(毛泽东)的时候,将它与最后一个值(邓小平)互换。 
                strAllay[strAllay.Length - 1 - i] = s; 
            } 
            foreach (string ss in strAllay) 
            { 
                Console.Write(ss+" " ); 
            } 
            Console.ReadKey(); 
        } 
    } 
}

C#数组排序:
复制代码 代码如下:
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
 
namespace 数组 

    class Program 
    { 
        static void Main(string[] args) 
        { 
            //输出一个数组里的最大的数值; 
            /*
            int[] arr = new int[] { 10, 9, 15, 6, 24, 3, 0, 7, 19, 1 };
            int max = 0;
            for (int i = 0; i < arr.Length - 1; i++)
            {
                if (arr[i] > max)
                {
                    max = arr[i];
                }
            }
            Console.WriteLine(max);
             **/ 
            //按大小顺序输出数组的值 
            int[] list = new int[] { 10, 9, 15, 6, 24, 3, 0, 7, 19, 1 ,100,25,38}; 
            /*
                for (int i = 0; i < list.Length-1; i++)
                  {
                      for (int j = i+1; j < list.Length; j++)
                      {
                          if (list[i] > list[j])
                          {
                             int temp = list[i];
                             list[i] = list[j];
                             list[j] = temp;
                         }
                     }
                 }*/ 
                /// <summary> 
         /// 插入排序法 
         /// </summary> 
         /// <param name="list"></param> 
         
             for (int i = 1; i < list.Length; i++) 
              { 
                 int t = list[i]; 
                 int j = i; 
                 while ((j > 0) && (list[j - 1] > t)) 
                 { 
                     list[j] = list[j - 1]; 
                     --j; 
                 } 
                 list[j] = t; 
             } 
 
                foreach (int forStr in list) 
                { 
                    Console.Write(forStr + " "); 
                } 
            Console.ReadKey(); 
        } 
    } 
}

希望本文所述对大家的C#程序设计有所帮助。

更多精彩内容其他人还在看

C#使用oledb读取excel表格内容到datatable的方法

这篇文章主要介绍了C#使用oledb读取excel表格内容到datatable的方法,涉及C#操作oledb及datatable的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#使用oledb操作excel文件的方法

这篇文章主要介绍了C#使用oledb操作excel文件的方法,涉及C#中oledb操作excel的相关技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#使用IHttpModule接口修改http输出的方法

这篇文章主要介绍了C#使用IHttpModule接口修改http输出的方法,涉及C#操作IHttpModule接口的相关技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#给图片加水印的简单实现方法

这篇文章主要介绍了C#给图片加水印的简单实现方法,涉及C#操作图片的相关技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#生成随机数的方法小结

这篇文章主要介绍了C#生成随机数的方法,实例总结了C#生成随机数的相关技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#使用jQuery实现无刷新评论提交的方法

这篇文章主要介绍了C#使用jQuery实现无刷新评论提交的方法,涉及C#结合jQuery进行Ajax操作的相关技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#读取中文文件出现乱码的解决方法

这篇文章主要介绍了C#读取中文文件出现乱码的解决方法,涉及C#中文编码的操作技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#图像对比度调整的方法

这篇文章主要介绍了C#图像对比度调整的方法,涉及C#实现图像对比度操作的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#图像灰度级拉伸的方法

这篇文章主要介绍了C#图像灰度级拉伸的方法,涉及C#灰度操作的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#图像线性变换的方法

这篇文章主要介绍了C#图像线性变换的方法,涉及C#操作图像线性变换的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多