C array inversion and sorting instance analysis
- 2020-12-13 19:04:22
- OfStack
An example of C# array inversion and sorting is presented in this paper. Share to everybody for everybody reference. Specific implementation methods are as follows:
C# array inversion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Data inversion
{
class Program
{
static void Main(string[] args)
{
string[] strAllay = { " Mao Zedong ", " On account ", " Qin shi huang ", " Genghis khan ", " Xi Jinping "," Deng Xiaoping "};
string s;
for (int i = 0; i < strAllay.Length / 2; i++)//strAllay.Length/2 It's going through (dividing the length of the array by the value of the array 2 ) to reverse the array members
{
s = strAllay[i];
strAllay[i] = strAllay[strAllay.Length - 1 - i];// if i Is equal to array control 1 Item value (MAO Zedong) when it comes to the end 1 Individual value (Deng Xiaoping) interchange.
strAllay[strAllay.Length - 1 - i] = s;
}
foreach (string ss in strAllay)
{
Console.Write(ss+" " );
}
Console.ReadKey();
}
}
}
C# array sort:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace An array of
{
class Program
{
static void Main(string[] args)
{
// The output 1 The largest value in an array;
/*
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);
**/
// Outputs the values of the array in size order
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>
/// Insertion sort
/// </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();
}
}
}
Hopefully this article has helped you with your C# programming.