C#數(shù)組反轉(zhuǎn)與排序?qū)嵗治?/h1>
更新時間:2015年01月26日 15:35:54 投稿:shichen2014
這篇文章主要介紹了C#數(shù)組反轉(zhuǎn)與排序,實例分析了數(shù)組反轉(zhuǎn)與常見的排序技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例分析了C#數(shù)組反轉(zhuǎn)與排序的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
C#數(shù)組反轉(zhuǎn)
復制代碼 代碼如下:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 數(shù)據(jù)反轉(zhuǎn)
{
class Program
{
static void Main(string[] args)
{
string[] strAllay = { ""};
string s;
for (int i = 0; i < strAllay.Length / 2; i++)//strAllay.Length/2是因為經(jīng)過(將數(shù)組的長度值除以2)次就可以將數(shù)組成員進行反轉(zhuǎn)了
{
s = strAllay[i];
strAllay[i] = strAllay[strAllay.Length - 1 - i];//如果i等于數(shù)組第一項值()的時候,將它與最后一個值()互換。
strAllay[strAllay.Length - 1 - i] = s;
}
foreach (string ss in strAllay)
{
Console.Write(ss+" " );
}
Console.ReadKey();
}
}
}
C#數(shù)組排序:
復制代碼 代碼如下:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 數(shù)組
{
class Program
{
static void Main(string[] args)
{
//輸出一個數(shù)組里的最大的數(shù)值;
/*
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);
**/
//按大小順序輸出數(shù)組的值
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#程序設(shè)計有所幫助。
相關(guān)文章
-
C#編程中使用ref和out關(guān)鍵字來傳遞數(shù)組對象的用法
這篇文章主要介紹了C#編程中使用ref和out關(guān)鍵字來傳遞數(shù)組對象的用法,在C#中數(shù)組也是對象可以被傳遞,需要的朋友可以參考下 2016-01-01
-
C# 中的 IReadOnlyDictionary 和 IReadOnlyLis
C# 中的IReadOnlyDictionary和IReadOnlyList是接口,用于表示只讀的字典和只讀的列表,這些接口提供了對集合的只讀訪問權(quán)限,即不允許對集合進行修改操作,這篇文章主要介紹了C# 中的 IReadOnlyDictionary 和 IReadOnlyList實例詳解,需要的朋友可以參考下 2024-03-03
-
C#將數(shù)字轉(zhuǎn)換成字節(jié)數(shù)組的方法
這篇文章主要介紹了C#將數(shù)字轉(zhuǎn)換成字節(jié)數(shù)組的方法,涉及C#字符串操作的技巧,非常具有實用價值,需要的朋友可以參考下 2015-04-04
-
利用Distinct()內(nèi)置方法對List集合的去重問題詳解
這篇文章主要給大家介紹了關(guān)于利用Distinct()內(nèi)置方法對List集合的去重問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧 2019-06-06
最新評論
本文實例分析了C#數(shù)組反轉(zhuǎn)與排序的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
C#數(shù)組反轉(zhuǎn)
復制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 數(shù)據(jù)反轉(zhuǎn)
{
class Program
{
static void Main(string[] args)
{
string[] strAllay = { ""};
string s;
for (int i = 0; i < strAllay.Length / 2; i++)//strAllay.Length/2是因為經(jīng)過(將數(shù)組的長度值除以2)次就可以將數(shù)組成員進行反轉(zhuǎn)了
{
s = strAllay[i];
strAllay[i] = strAllay[strAllay.Length - 1 - i];//如果i等于數(shù)組第一項值()的時候,將它與最后一個值()互換。
strAllay[strAllay.Length - 1 - i] = s;
}
foreach (string ss in strAllay)
{
Console.Write(ss+" " );
}
Console.ReadKey();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 數(shù)據(jù)反轉(zhuǎn)
{
class Program
{
static void Main(string[] args)
{
string[] strAllay = { ""};
string s;
for (int i = 0; i < strAllay.Length / 2; i++)//strAllay.Length/2是因為經(jīng)過(將數(shù)組的長度值除以2)次就可以將數(shù)組成員進行反轉(zhuǎn)了
{
s = strAllay[i];
strAllay[i] = strAllay[strAllay.Length - 1 - i];//如果i等于數(shù)組第一項值()的時候,將它與最后一個值()互換。
strAllay[strAllay.Length - 1 - i] = s;
}
foreach (string ss in strAllay)
{
Console.Write(ss+" " );
}
Console.ReadKey();
}
}
}
C#數(shù)組排序:
復制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 數(shù)組
{
class Program
{
static void Main(string[] args)
{
//輸出一個數(shù)組里的最大的數(shù)值;
/*
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);
**/
//按大小順序輸出數(shù)組的值
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();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 數(shù)組
{
class Program
{
static void Main(string[] args)
{
//輸出一個數(shù)組里的最大的數(shù)值;
/*
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);
**/
//按大小順序輸出數(shù)組的值
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#程序設(shè)計有所幫助。
相關(guān)文章
C#編程中使用ref和out關(guān)鍵字來傳遞數(shù)組對象的用法
這篇文章主要介紹了C#編程中使用ref和out關(guān)鍵字來傳遞數(shù)組對象的用法,在C#中數(shù)組也是對象可以被傳遞,需要的朋友可以參考下2016-01-01C# 中的 IReadOnlyDictionary 和 IReadOnlyLis
C# 中的IReadOnlyDictionary和IReadOnlyList是接口,用于表示只讀的字典和只讀的列表,這些接口提供了對集合的只讀訪問權(quán)限,即不允許對集合進行修改操作,這篇文章主要介紹了C# 中的 IReadOnlyDictionary 和 IReadOnlyList實例詳解,需要的朋友可以參考下2024-03-03C#將數(shù)字轉(zhuǎn)換成字節(jié)數(shù)組的方法
這篇文章主要介紹了C#將數(shù)字轉(zhuǎn)換成字節(jié)數(shù)組的方法,涉及C#字符串操作的技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04利用Distinct()內(nèi)置方法對List集合的去重問題詳解
這篇文章主要給大家介紹了關(guān)于利用Distinct()內(nèi)置方法對List集合的去重問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-06-06