C#數組反轉與排序實例剖析。本站提示廣大學習愛好者:(C#數組反轉與排序實例剖析)文章只能為提供參考,不一定能成為您想要的結果。以下是C#數組反轉與排序實例剖析正文
本文實例剖析了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#法式設計有所贊助。