C # Array Sorting method

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.