1. Bubble Sorting
Bubble Sorting is the slowest sorting algorithm. In practice, it is the most efficient algorithm. It compares every element in the array one by one, causing large data to sink and small data to rise. It is an O (n ^ 2) algorithm.
2. Insert sorting
Sort by inserting the values in the sequence into a sorted sequence until the end of the sequence.
3. Shell sorting
Shell sorting divides data into different groups, sorts each group first, and then inserts and sorts all elements once to reduce the number of data exchanges and moves. The average efficiency is O (nlogn ).
Bubble sort C # implementation:
/// <Summary> /// bubble Sort /// </summary> public class BubbleSort: ISort {public int [] Sort (int [] array) {if (array! = Null) {for (int I = 0; I <array. length; I ++) {for (int j = 1; j <array. length-I; j ++) {Swap (ref array [j-1], ref array [j]) ;}} return array ;} public static void Swap (ref int int1, ref int int2) {if (int1> int2) {int temp = int1; int1 = int2; int2 = temp ;}}}Bubble Sorting
Insert sort C # implementation:
/// <Summary> /// insert sorting /// </summary> public class InsertSort: ISort {public int [] Sort (int [] array) {if (array! = Null) {int k = 1; // use the k variable, which can be better extended to Shell sorting for (int I = k; I <array. length; I ++) {int current = array [I]; int preIndex = I-k; while (preIndex> = 0 & preIndex <array. length & current <array [preIndex]) {array [preIndex + k] = array [preIndex]; preIndex = preIndex-k ;} array [preIndex + k] = current;} return array ;}}Insert sort
Shell sorting C # implementation:
/// <Summary> /// shell sorting /// </summary> public class ShellSort: ISort {public int [] Sort (int [] array) {if (array! = Null) {int [] list = {9, 5, 3, 2, 1}; foreach (int k in list) {for (int I = k; I <array. length; I ++) {int current = array [I]; int preIndex = I-k; while (preIndex> = 0 & preIndex <array. length & current <array [preIndex]) {array [preIndex + k] = array [preIndex]; preIndex = preIndex-k ;} array [preIndex + k] = current ;}} return array ;}}Shell sorting
Performance test code:
Class Program {public static Random re = new Random (); static void Main (string [] args) {Stopwatch stw1 = new Stopwatch (); Stopwatch stw2 = new Stopwatch (); stopwatch stw3 = new Stopwatch (); int [] intArray1 = GetArray (int. maxValue/100000); int [] intArray2 = GetArray (int. maxValue/100000); int [] intArray3 = GetArray (int. maxValue/100000); ISort sort1 = new BubbleSort (); // Bubble Sorting stw1.Start (); int [] result1 = sort1.Sort (intArray1); stw1.Stop (); Console. writeLine ("output sorting result (Bubble Sorting)"); Console. writeLine ("Total program running time:" + stw1.Elapsed. toString (); ISort sort2 = new InsertSort (); // insert sort stw2.Start (); int [] result2 = sort2.Sort (intArray2); stw2.Stop (); Console. writeLine ("output sorting result (insert sorting)"); Console. writeLine ("Total program running time:" + stw2.Elapsed. toString (); ISort sort3 = new ShellSort (); // Shell sorting stw3.Start (); int [] result3 = sort3.Sort (intArray3); stw3.Stop (); Console. writeLine ("output sorting result (Shell sorting)"); Console. writeLine ("Total program running time:" + stw3.Elapsed. toString (); // output the sorting result // OutputResult (result1, result2, result3); Console. readKey ();}}Performance Testing
Result: