Author: Sabine [Introduction] This article introduces four sort algorithms of C #: bubble sort, select sort, insert sort, and Hill sort.
Bubble Sorting
Using System;
Namespace BubbleSorter
{Public class BubbleSorter
{Public void Sort (int [] list)
{Int I, j, temp;
Bool done = false;
J = 1;
While (j <list. Length )&&(! Done ))
{Done = true;
For (I = 0; I <list. Length-j; I ++)
{
If (list [I]> list [I + 1])
{
Done = false;
Temp = list [I];
List [I] = list [I + 1];
List [I + 1] = temp;
}}
J ++ ;}
}}
Public class MainClass
{Public static void Main ()
{
Int [] iArrary = new int };
BubbleSorter sh = new BubbleSorter ();
Sh. Sort (iArrary );
For (int m = 0; m <iArrary. Length; m ++)
Console. Write ("{0}", iArrary [m]);
Console. WriteLine ();
}}
}
Select sort
Using System;
Namespace SelectionSorter
{Public class SelectionSorter
{Private int min;
Public void Sort (int [] list)
{For (int I = 0; I <list. Length-1; I ++)
{Min = I;
For (int j = I + 1; j <list. Length; j ++)
{If (list [j] <list [min])
Min = j;
}
Int t = list [min];
List [min] = list [I];
List [I] = t;
}}
}
Public class MainClass
{Public static void Main ()
{
Int [] iArrary = new int };
SelectionSorter ss = new SelectionSorter ();
Ss. Sort (iArrary );
For (int m = 0; m <iArrary. Length; m ++)
Console. Write ("{0}", iArrary [m]);
Console. WriteLine ();
}}
}
Insert sort
Using System;
Namespace InsertionSorter
{Public class InsertionSorter
{Public void Sort (int [] list)
{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 ;}
}
}
Public class MainClass
{Public static void Main ()
{
Int [] iArrary = new int };
InsertionSorter ii = new InsertionSorter ();
Ii. Sort (iArrary );
For (int m = 0; m <iArrary. Length; m ++)
Console. Write ("{0}", iArrary [m]);
Console. WriteLine ();
}}
}
Hill sorting
Hill sorting is to segment the group for insertion sorting.
Using System;
Namespace ShellSorter
{
Public class ShellSorter
{
Public void Sort (int [] list)
{
Int inc;
For (inc = 1; inc <= list. Length/9; inc = 3 * inc + 1 );
For (; inc> 0; inc/= 3)
{
For (int I = inc + 1; I <= list. Length; I + = inc)
{
Int t = list [I-1];
Int j = I;
While (j> inc) & (list [j-inc-1]> t ))
{
List [J-1] = list [j-inc-1];
J-= inc;
}
List [J-1] = t;
}}
}}
Public class MainClass
{Public static void Main ()
{
Int [] iArrary = new int };
ShellSorter sh = new ShellSorter ();
Sh. Sort (iArrary );
For (int m = 0; m <iArrary. Length; m ++)
Console. Write ("{0}", iArrary [m]);
Console. WriteLine ();
}}
}
Quick sorting
Using System;
Using System. Collections. Generic;
Using System. Text;
Namespace SoloDataStructure
{
Class MyQuickSort
{
/** // <Summary>
/// Quick Sorting Algorithm
/// </Summary>
/// The fast sorting method is unstable, and the time complexity is O (nlog2n), which is the fastest sorting method of the same order of magnitude.
/// <Param name = "arr"> partitioned array </param>
/// <Param name = "low"> low-end array tagging </param>
/// <Param name = "high"> array high-end subscript </param>
/// <Returns> </returns>
Static int Partition (int [] arr, int low, int high)
{
// Perform a quick sorting and return the recorded position of the central axis
// Arr [0] = arr [low];
Int direction = arr [low]; // place the center axis in arr [0]
While (low {
While (low -- High;
// Move records smaller than the central axis to the lower end
Swap (ref arr [high], ref arr [low]);
While (low ++ Low;
Swap (ref arr [high], ref arr [low]);
// Move the record larger than the central axis to the high end
}
Arr [low] = middle; // The center axis is moved to the correct position.
Return low; // return the center axis position
}
Static void Swap (ref int I, ref int j)
{
Int t;
T = I;
I = j;
J = t;
}
Static void QuickSort (int [] arr, int low, int high)
{
If (low {
Int Partition = Partition (arr, low, high );
QuickSort (arr, low, skip-1 );
QuickSort (arr, latency + 1, high );
}
}
Static void Main (string [] args)
{
Int [] arr = new int [] {110 };
QuickSort (arr, 0, arr. Length-1 );
Console. Write ("Data After QuickSort :");
Foreach (int I in arr)
{
Console. Write (I + ",");
}
Console. ReadLine ();
}
}
}