Sorting algorithm--c# language

Source: Internet
Author: User

Insert Sort: (Direct insert sort & Hill Sort)

Using System;

Using System.Collections.Generic;

Using System.Linq;

Using System.Text;

?

Namespace Sort

{

public class Insertsort

{

public static int k;

?

<summary>

Direct Insert Sort

</summary>

<param name= "A" > Arrays </param>

<param name= "n" > length of array </param>

public static void Directinsertsort (int[] A, int n)

{

for (int i = 0; i < n; i++)

{

int t = a[i];

for (int j = i-1; J >= 0; j--)

{

if (T < A[j])

{

A[j + 1] = A[j];

A[J] = t;

}

Else

{

Break

}

k++;

}

k++;

}

}

?

<summary>

Hill sort

The hill sort is to divide the array into groups by a certain increment, and then make a direct insert order for each group

Increments by (N/2,N/4,... ) to decrement, and finally to sort by an increment of 1,

And the direct insert sort is the hill sort that sets the increment to 1 directly

</summary>

<param name= "A" > Arrays </param>

<param name= "n" > Array length </param>

public static void Shellinsertsort (int[] A, int n)

{

for (double d = Math.floor (double) (N/2)), d >= 1; d = Math.floor (D/2))

{

Shellsort (A, n, (int) d);

k++;

}

}

?

<summary>

Quick Insert Sort by Delta D

</summary>

<param name= "A" > Arrays </param>

<param name= "n" > Array length </param>

<param name= "D" > Delta </param>

public static void Shellsort (int[] A, int n, int d)

{

for (int m = 0; m < D; m++)

{

for (int i = d + m; i < n; i = i + D)

{

int t = a[i];

for (int j = i-d; J >= 0; j = j-d)

{

if (T < A[j])

{

A[j + d] = a[j];

A[J] = t;

}

k++;

}

k++;

}

k++;

}

}

}

}

?

Swap sort (bubble sort & quick Sort)

Using System;

Using System.Collections.Generic;

Using System.Linq;

Using System.Text;

?

Namespace Sort

{

public class Exchangesort

{

public static int k = 0;

<summary>

Bubble sort

</summary>

<param name= "A" > Arrays </param>

<param name= "n" > Array length </param>

public static void Bubblesort (int[] A, int n)

{

for (int m = 1; M <= N; m++)

{

int j = 0;

for (int i = 0; i < n-m; i++)

{

if (A[i] > a[i + 1])

{

int t = a[i];

A[i] = a[i + 1];

A[i + 1] = t;

j + +;

}

k++;//Count

}

k++;

if (j = = 0)

Break If J equals 0, it means that there is no data exchange in this sort of trip, and the sequence is already sorted.

}

}

?

<summary>

Quick Sort

</summary>

<param name= "A" > Arrays </param>

<param name= "n" > Array length </param>

public static void QuickSort (int[] A, int n)

{

QSort (A, 0, n-1);

}

?

<summary>

Recursion for quick sorting

</summary>

<param name= "A" > Arrays </param>

<param name= "Low" > lowest index number </param>

<param name= "High" > Highest index number </param>

public static void QSort (Int[] A, int., int high)

{

if (Low < high)

{

int i = low;

int j = high;

int t = a[i];

while (I < j)

{

while (I < J && A[j] >= t)

{

j--;

k++; Count

}

A[i] = A[j];

while (I < J && A[i] <= t)

{

i++;

k++;

}

A[J] = A[i];

k++;

}

if (i = = j)

{

A[i] = t;

QSort (A, low, i-1);

QSort (A, i + 1, high);

}

}

}

}

}

Select sort (Simple select sort & heap Sort)

Using System;

Using System.Collections.Generic;

Using System.Linq;

Using System.Text;

?

Namespace Sort

{

public class Selectionsort

{

public static int k = 0;

?

<summary>

Simple selection sorting

</summary>

<param name= "A" > Arrays </param>

<param name= "n" > Array length </param>

public static void Simpleselectionsort (int[] A, int n)

{

for (int i = 0; i < n; i++)

{

int t = a[i];

for (int j = i + 1; j < N; j + +)

{

if (A[j] < T)

{

A[i] = A[j];

A[J] = t;

t = A[i];

}

k++;

}

k++;

}

}

?

<summary>

Heap Sort

First step: Build the heap

Step two: Take the first number of the built-up heap out to the last one, then re-create the heap in front and then remove the first maximum

</summary>

<param name= "a" ></param>

<param name= "M" ></param>

public static void Heapsort (int[] A, int n)

{

for (int j = n; j > 1; j--)

{

for (int i = J/2-1; I >= 0; i--)

{

Createheap (A, I, j-1);

}

int t = a[0];

A[0] = a[j-1];

A[j-1] = t;

k++;

}

}

?

<summary>

Build heap (large top heap)

</summary>

<param name= "A" > Arrays </param>

<param name= The starting node index of the "low" > Array </param>

<param name= Maximum index of "high" > Array </param>

public static void Createheap (Int[] A, int., int high)

{

int i = low;

Int J = 2 * i + 1;

int t = a[i];

while (J <= High)

{

k++;

if (j + 1 <= high && a[j] < A[j + 1])//left child small less right operand child

{

j++;//Pointing right Child

}

if (A[i] < a[j])

{

A[i] = A[j];

A[J] = t;

i = j;//with the Swap child node as the root node, proceed to create heap

j = 2 * i + 1;

}

Else

{

Break

}

}

}

}

}

Merge sort

<summary>

Merge sort

</summary>

<param name= "A" > Arrays </param>

<param name= "Low" > lowest index </param>

<param name= "High" > Top index </param>

private static void MergeSort (int[] a,int low,int High)

{

if (Low

{

int mid = low+ (high-low)/2;

MergeSort (A, low, mid);

MergeSort (A, mid + 1, high);

Merge (A, low,mid,high);

}

}

?

<summary>

Merging the arrays

</summary>

<param name= "a" ></param>

<param name= "Low" ></param>

<param name= "Mid" ></param>

<param name= "High" ></param>

private static void Merge (int[] a,int low,int mid,int High)

{

Int[] B = new Int[high-low + 1];

int i = low, J = mid + 1, k = 0;

while (I <= mid && J <= High)

{

b[k++] = (A[i] < a[j])? A[i++]: a[j++];

}

while (I <= mid)

{

b[k++] = a[i++];

}

while (J <= High)

{

b[k++] = a[j++];

}

for (k = 0, i = low; I <= high; k++, i++)

{

A[i] = b[k];

}

}

?

Main program:

private static int k = 0;

static void Main (string[] args)

{

Int[] A = new int[17] {18, 15, 14,100, 13, 3, 4, 5, 10, 2, 7, 21, 9, 8, 23, 54, 30};

Print (a);

Console.WriteLine ("Direct Insert sort:");

Insertsort.directinsertsort (A, a.length);

Shellsort (A, a.length, 1);

Print (a);

Console.WriteLine ("Direct insert sort requires {0} times calculation.") ", INSERTSORT.K);

?

Console.WriteLine ("Hill Sort:");

Insertsort.shellinsertsort (A, a.length);

Print (a);

Console.WriteLine ("Hill sort requires {0} times calculation.") ", INSERTSORT.K);

?

Console.WriteLine ("Bubble Sort:");

Exchangesort.bubblesort (A, a.length);

Print (a);

Console.WriteLine ("bubble sort requires {0} times calculation.") ", EXCHANGESORT.K);

?

Console.WriteLine ("Quick sort:");

Exchangesort.quicksort (A, a.length);

Print (a);

Console.WriteLine ("Quick sort requires {0} times.") ", EXCHANGESORT.K);

?

Console.WriteLine ("Simple selection Sort:");

Selectionsort.simpleselectionsort (A, a.length);

Print (a);

Console.WriteLine ("Simple select Sort requires {0} times calculation.") ", SELECTIONSORT.K);

?

Console.WriteLine ("Heap Sort:");

Selectionsort.heapsort (A, a.length);

Print (a);

Console.WriteLine ("Heap ordering requires {0} calculations.") ", SELECTIONSORT.K);

?

Console.WriteLine ("Merge sort:");

MergeSort (A, 0,a.length-1);

Print (a);

Console.WriteLine ("Merge sort requires {0} times calculation.") ", SELECTIONSORT.K);

?

Console.ReadLine ();

}

Sorting algorithm--c# language

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.