The methods of array sorting used in C # are: Select sort, bubble sort, insert sort, and hill sort.
First, the choice of sorting method
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Namespace Test
{
Class Program
{
static void Main (string[] args)
{
C # Selection Sorting method-www.baike369.com
int[] array = new int[] {2, 18, 9, 26, 3, 7, 5, 10};
Console.Write ("The result before array sorting is:");
foreach (int n in array)
{
Console.Write ("{0}", n + "");
}
Console.WriteLine ();
int min;
for (int i = 0; i < array. Length-1; i++)
{
min = i;
for (int j = i + 1; j < Array. Length; J + +)
{
if (Array[j] < array[min])
min = j;
}
int t = array[min];
Array[min] = Array[i];
Array[i] = t;
}
Console.Write ("The result of sorting the array is:");
foreach (int n in array)
{
Console.Write ("{0}", n + "");
}
Console.ReadLine ();
}
}
}
Operation Result:
Before the array is sorted, the result is: 2 18 9 26 3 7 5 10
After the array is sorted, the result is: 2 3 5 7 9 10 18 26
Second, bubble Sort method
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Namespace Test
{
Class Program
{
static void Main (string[] args)
{
int[] array = new int[] {2, 18, 9, 26, 3, 7, 5, 10};
Console.Write ("The result before array sorting is:");
foreach (int n in array)
{
Console.Write ("{0}", n + "");
}
Console.WriteLine ();
for (int i = 0; i < array. Length; i++)
{
for (int j = i; J < Array. Length; J + +)
{
if (Array[i] < array[j])
{
int temp = Array[i];
Array[i] = Array[j];
ARRAY[J] = temp;
}
}
}
Console.Write ("The result of sorting the array is:");
foreach (int n in array)
{
Console.Write ("{0}", n + "");
}
Console.ReadLine ();
}
}
}
Operation Result:
Before the array is sorted, the result is: 2 18 9 26 3 7 5 10
After the array is sorted, the result is: 26 18 10 9 7 5 3 2
Three, inserting the sorting method
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Namespace Test
{
Class Program
{
static void Main (string[] args)
{
int[] array = new int[] {2, 18, 9, 26, 3, 7, 5, 10};
Console.Write ("The result before array sorting is:");
foreach (int n in array)
{
Console.Write ("{0}", n + "");
}
Console.WriteLine ();
for (int i = 1; i < array. Length; i++)
{
int t = array[i];
int j = i;
while ((J > 0) && (array[j-1] > t))
{
ARRAY[J] = array[j-1];
--j;
}
ARRAY[J] = t;
}
Console.Write ("The result of sorting the array is:");
foreach (int n in array)
{
Console.Write ("{0}", n + "");
}
Console.ReadLine ();
}
}
}
Operation Result:
Before the array is sorted, the result is: 2 18 9 26 3 7 5 10
The result of sorting the array is: 2 3 5 7 9 10 18 264, Hill Sort method
The hill sort is the segment of the component that is then inserted into the sort.
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Namespace Test
{
Class Program
{
static void Main (string[] args)
{
int[] array = new int[] {2, 18, 9, 26, 3, 7, 5, 10};
Console.Write ("The result before array sorting is:");
foreach (int n in array)
{
Console.Write ("{0}", n + "");
}
Console.WriteLine ();
INT Inc;
for (inc = 1; inc <= array. LENGTH/9; inc = 3 * inc + 1);
for (; Inc > 0; inc/= 3)
{
for (int i = inc + 1; I <= array. Length; i + = Inc)
{
int t = array[i-1];
int j = i;
while ((J > Inc) && (Array[j-inc-1] > t))
{
ARRAY[J-1] = array[j-inc-1];
J-= Inc;
}
Array[j-1] = t;
}
}
Console.Write ("The result of sorting the array is:");
foreach (int n in array)
{
Console.Write ("{0}", n + "");
}
Console.ReadLine ();
}
}
}
Operation Result:
Before the array is sorted, the result is: 2 18 9 26 3 7 5 10
After the array is sorted, the result is: 2 3 5 7 9 10 18 26
C # Array Sorting method