Sort (C # Edition)

Source: Internet
Author: User

1. Bubble Sorting

 

Using System;
Using System. Collections. Generic;
Using System. Text;
/// <Summary>
/// Bubble sort
/// </Summary>
Public class BubbleSorter
{
Private static int [] myArray;
Private static int arraySize;

Public static void Sort (int [])
{
MyArray =;
ArraySize = myArray. Length;
BubbleSort (myArray );
}
/// <Summary>
/// Bubble Algorithm
/// </Summary>
/// <Param name = "myArray"> </param>
Public static void BubbleSort (int [] myArray)
{
For (int I = 0; I <myArray. Length; I ++) // Number of loops
{
For (int j = 0; j <myArray. Length-1-I; j ++) // number of cycles
{
If (myArray [j]> myArray [j + 1])
{
Swap (ref myArray [j], ref myArray [j + 1]);
}
}
}
}
/// <Summary>
/// Exchange
/// </Summary>
/// <Param name = "left"> </param>
/// <Param name = "right"> </param>
Private static void Swap (ref int left, ref int right)
{
Int temp;
Temp = left;
Left = right;
Right = temp;
}

Public static void Main (string [] args)
{
Int [] a = new int [] {4, 2, 1, 6, 3, 6, 0,-5, 1, 1 };
BubbleSorter. Sort ();

For (int I = 0; I <a. Length; I ++)
{
System. Console. WriteLine (a [I]);
}
}
}

 

2. Cocktail sorting (double Bubble sorting, stirring sorting, or ripple sorting)

 

Using System;
Using System. Collections. Generic;
Using System. Text;

Public class CockTailSorter
{
Private static int [] myArray;
Private static int arraySize;

Public static void Sort (int [])
{
MyArray =;
ArraySize = myArray. Length;
CockTailSort (myArray );
}

Public static void CockTailSort (int [] myArray)
{
Int low, up, index, I;
Low = 0;
Up = myArray. Length-1;
Index = low;

While (up> low)
{
For (I = low; I <up; I ++) // scan from top to bottom
{
If (myArray [I]> myArray [I + 1])
{
Swap (ref myArray [I], ref myArray [I + 1]);
Index = I;
}
}

Up = index; // record the last swap location
For (I = up; I> low; I --) // scan from the bottom up of the last Switch
{
If (myArray [I] <myArray [I-1])
{
Swap (ref myArray [I], ref myArray [I-1]);
Index = I;
}
}

Low = index; // record the last swap location
}

}
/// <Summary>
/// Exchange
/// </Summary>
/// <Param name = "left"> </param>
/// <Param name = "right"> </param>
Private static void Swap (ref int left, ref int right)
{
Int temp;
Temp = left;
Left = right;
Right = temp;
}

Public static void Main (string [] args)
{
Int [] a = new int [] {4, 2, 1, 6, 3, 6, 0,-5, 1, 1 };
CockTailSorter. Sort ();

For (int I = 0; I <a. Length; I ++)
{
System. Console. WriteLine (a [I]);
}
}
}

 

3. Select sort

 

 

Using System;
Using System. Collections. Generic;
Using System. Text;
Public class SelectSorter
{
Private static int [] myArray;
Private static int arraySize;

Public static void Sort (int [])
{
MyArray =;
ArraySize = myArray. Length;
SelectSort (myArray );
}
/// <Summary>
///
/// </Summary>
/// <Param name = "myArray"> </param>
Public static void SelectSort (int [] myArray)
{
Int I, j, smallest;
For (I = 0; I <myArray. Length-1; I ++) // data start position, from 0 to the second to the last
{
Smallest = I; // subscript of the minimum recorded data
For (j = I + 1; j <myArray. Length; j ++)
{
If (myArray [j] <myArray [smallest]) // find the minimum data in the remaining data
{
Smallest = j; // if there is something smaller than it, record the subscript
}
}
Swap (ref myArray [I], ref myArray [smallest]); // Swap the minimum data with the first unordered data
}
}
/// <Summary>
/// Exchange
/// </Summary>
/// <Param name = "left"> </param>
/// <Param name = "right"> </param>
Private static void Swap (ref int left, ref int right)
{
Int temp;
Temp = left;
Left = right;
Right = temp;
}

Public static void Main (string [] args)
{
Int [] a = new int [] {4, 2, 1, 6, 3, 6, 0,-5, 1, 1 };
SelectSorter. Sort ();

For (int I = 0; I <a. Length; I ++)
{
System. Console. WriteLine (a [I]);
}
}
}

 

 

4. Insert sorting

 

 

Using System;
Using System. Collections. Generic;
Using System. Text;

Public class InsertSorter
{
Private static int [] myArray;
Private static int arraySize;

Public static void Sort (int [])
{
MyArray =;
ArraySize = myArray. Length;
InsertSort (myArray );
}
/// <Summary>
///
/// </Summary>
/// <Param name = "myArray"> </param>
Public static void InsertSort (int [] myArray)
{
Int I, j, temp;

For (I = 1; I <myArray. Length; I ++)
{
Temp = myArray [I]; // Save the current data
J = I-1;

// Insert data into an ordered table. If the data in the table is larger than the data in the table
// Move the data in the ordered table backward until the first data smaller than it is found.
// Put it behind the data

While (j> = 0 & myArray [j]> temp)
{
MyArray [j + 1] = myArray [j];
J --;
}
MyArray [j + 1] = temp;
}
}

Public static void Main (string [] args)
{
Int [] a = new int [] {4, 2, 1, 6, 3, 6, 0,-5, 1, 1 };
InsertSorter. Sort ();

For (int I = 0; I <a. Length; I ++)
{
System. Console. WriteLine (a [I]);
}
}
}

 

 

5. Hill sorting

 

 

Using System;
Using System. Collections. Generic;
Using System. Text;

Public class ShellSorter
{
Private static int [] myArray;
Private static int arraySize;

Public static void Sort (int [])
{
MyArray =;
ArraySize = myArray. Length;
ShellSort (myArray );
}
/// <Summary>
///
/// </Summary>
/// <Param name = "myArray"> </param>
Public static void ShellSort (int [] myArray)
{
Int I, j, increment;
Int temp;

For (increment = myArray. Length/2; increment> 0; increment/= 2)
{
For (I = increment; I <myArray. Length; I ++)
{
Temp = myArray [I];
For (j = I; j> = increment; j-= increment)
{
If (temp <myArray [j-increment])
MyArray [j] = myArray [j-increment];
Else
Break;
}
MyArray [j] = temp;
}
}
}

Public static void Main (string [] args)
{
Int [] a = new int [] {4, 2, 1, 6, 3, 6, 0,-5, 1, 1 };
ShellSorter. Sort ();

For (int I = 0; I <a. Length; I ++)
{
System. Console. WriteLine (a [I]);
}
}
}

 

6. Quick sorting

 

 

Using System;
Using System. Collections. Generic;
Using System. Text;

Public class QuickSorter
{
Private static int [] myArray;
Private static int arraySize;

Public static void Sort (int [])
{
MyArray =;
ArraySize = myArray. Length;
QuickSort (myArray, 0, myArray. Length-1 );
}
/// <Summary>
/// Quick Sorting Algorithm
/// </Summary>
/// <Param name = "myArray"> </param>
Public static void QuickSort (int [] myArray, int left, int right)
{
Int I, j, s;
If (left <right)
{
I = left-1;
J = right + 1;
S = myArray [(I + j)/2];
While (true)
{
While (myArray [++ I] <s );
While (myArray [-- j]> s );
If (I> = j)
Break;
Swap (ref myArray [I], ref myArray [j]);
}
QuickSort (myArray, left, I-1 );
QuickSort (myArray, j + 1, right );
}
}
/// <Summary>
/// Exchange
/// </Summary>
/// <Param name = "left"> </param>
/// <Param name = "right"> </param>
Private static void Swap (ref int left, ref int right)
{
Int temp;
Temp = left;
Left = right;
Right = temp;
}

Public static void Main (string [] args)
{
Int [] a = new int [] {4, 2, 1, 6, 3, 6, 0,-5, 1, 1 };
QuickSorter. Sort ();

For (int I = 0; I <a. Length; I ++)
{
System. Console. WriteLine (a [I]);
}
}
}

 

7. Merge Sorting

 

 

Using System;
Using System. Collections. Generic;
Using System. Text;

Public class MergeSorter
{
Private static int [] myArray;
Private static int arraySize;

Public static void Sort (int [])
{
MyArray =;
ArraySize = myArray. Length;
MergeSort ();
}
/// <Summary>
/// Sort the Array Using the merge method. First, divide the sequence
/// Then merge the series
/// The time is O (nlog n)
/// </Summary>
/// <Param name = "myArray"> </param>
Public static void MergeSort ()
{
Int [] temp = new int [arraySize];
Msort (temp, 0, arraySize-1 );
}
Private static void Msort (int [] temp, int left, int right)
{
Int mid;
If (right> left)
{
Mid = (right + left)/2;
Msort (temp, left, mid); // split the sequence on the left
Msort (temp, mid + 1, right); // splits the sequence on the right.
Merge (temp, left, mid + 1, right); // Merge Sequence

}
}
Private static void Merge (int [] temp, int left, int mid, int right)
{
Int I, left_end, num_elements, tmp_pos;
Left_end = mid-1;
Tmp_pos = left;
Num_elements = right-left + 1;
While (left <= left_end) & (mid <= right ))
{
If (myArray [left] <= myArray [mid]) // merge the left-side sequence into the temp array.
{
Temp [tmp_pos] = myArray [left];
Tmp_pos = tmp_pos + 1;
Left = left + 1;
}
Else
{
Temp [tmp_pos] = myArray [mid];
Tmp_pos = tmp_pos + 1;
Mid = mid + 1;
}
}
While (left <= left_end) // copy the remaining data on the left to the temp array.
{
Temp [tmp_pos] = myArray [left];
Left = left + 1;
Tmp_pos = tmp_pos + 1;
}
While (mid <= right) // copy the remaining data on the right to the temp Array
{
Temp [tmp_pos] = myArray [mid];
Mid = mid + 1;
Tmp_pos = tmp_pos + 1;
}

For (I = 0; I <num_elements; I ++)
{
MyArray [right] = temp [right];
Right = right-1;
}
}

Public static void Main (string [] args)
{
Int [] a = new int [] {4, 2, 1, 6, 3, 6, 0,-5, 1, 1 };
MergeSorter. Sort ();

For (int I = 0; I <a. Length; I ++)
{
System. Console. WriteLine (a [I]);
}
}
}

 

8. Heap sorting

 

 

Using System;
Using System. Collections. Generic;
Using System. Text;

Public class HeapSorter
{
Private static int [] myArray;
Private static int arraySize;

Public static void Sort (int [])
{
MyArray =;
ArraySize = myArray. Length;
HeapSort ();
}

Public static void HeapSort ()
{
BuildHeap (); // build the original sequence into a heap
While (arraySize> 1)
{
ArraySize --;
Exchange (0, arraySize); // put the maximum value at the end of the array
TraversingHeap (0); // views the sequence from 0 to n-1 as a new sequence and reconstructs the heap.
}
}
Private static void BuildHeap ()
{
For (int vNode = arraySize/2-1; vNode> = 0; vNode --)
{
TraversingHeap (vNode );
}
}
// Create a team by traversing down the subnode
Private static void TraversingHeap (int vNode)
{
Int wNode = 2 * vNode + 1; // The Node wNode is the first subnode of the node vNode.
While (wNode <arraySize)
{
If (wNode + 1 <arraySize) // if there is a second subnode under the vNode of the node
If (myArray [wNode + 1]> myArray [wNode])
WNode ++; // set the sub-node wNode to the sub-node with the highest nominal value under the node vNode
If (myArray [vNode]> = myArray [wNode])
Return;
Exchange (vNode, wNode); // If not, the values of vNode and wNode are exchanged.
VNode = wNode;
WNode = 2 * vNode + 1; // continue to find the child node
}
}
Private static void Exchange (int I, int j)
{
Int t = myArray [I];
MyArray [I] = myArray [j];
MyArray [j] = t;
}

Public static void Main (string [] args)
{
Int [] a = new int [] {4, 2, 1, 6, 3, 6, 0,-5, 1, 1 };
HeapSorter. Sort ();

For (int I = 0; I <a. Length; I ++)
{
System. Console. WriteLine (a [I]);
}
}
}

 

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.