Quick sorting involves a large amount of data, random distribution of data, and small amount of duplicate data, which provides better performance. Major improvements are made as follows: 1. Non-Recursive Implementation to prevent stack overflow when the data volume is large. 2. improvement when all data is equal. 3. If the data is smaller than a certain number, insert the data in the sorting order. 4. Select the median. Test results: 1. 10 million random data records are different. List <T> class Sort () method: 1.3 seconds List <T> class Sort (Comparison <T> comparison) method: 6.2 seconds self-written algorithm: 3.7 seconds, 10 million random data, with a small number of identical elements. List <T> class Sort () method: 1.1 seconds List <T> class Sort (Comparison <T> comparison) method: 5.7 seconds self-written algorithm: 2.7 seconds, 10 million random data, with a large number of identical elements. List <T> class Sort () method: 0.8 seconds List <T> class Sort (Comparison <T> comparison) method: 5.3 seconds self-written algorithm: 1.7 seconds, 4 and 10 million identical data records. List <T> class Sort () method: 0.59 seconds List <T> class Sort (Comparison <T> comparison) method: 4.7 seconds self-written algorithm: 0.42 s non-recursive quick sorting algorithm: copy the code namespace Sort {// <summary> // quick sorting (non-recursive) /// December 25, 2013 /// </summary> public class QuickSort {// <summary> /// non-Recursive Implementation of quick sorting /// </summary> /// <param name = "array"> Sort array </param> public static void Sort (int [] array) {int start = 0; int end = array. length-1; int [] stackLeft = new int [array. L Ength]; int [] stackRight = new int [array. length]; int left; int right; int index = 0; SortUnit (array, start, end, out left, out right); if (left> start) {stackLeft [index] = start; stackRight [index] = left; index ++;} if (right <end) {stackLeft [index] = right; stackRight [index] = end; index ++;} while (index> 0) {index --; start = stackLeft [index]; end = stackRight [index]; if (end-start <5) // When the Sorting Algorithm is executed to a certain depth, the entire array is ordered and partially unordered. In this case, the quick sorting is stopped and the insert sorting {continue;} SortUnit (array, start, end, out left, out right); if (left> start) {stackLeft [index] = start; stackRight [index] = left; index ++;} if (right <end) {stackLeft [index] = right; stackRight [index] = end; index ++ ;}} InsertionSort (array );} // end of Sort // <summary> // This method is completed. The array is divided into three parts, which are smaller than the key on the left and greater than the key on the right, the center is equal to the key. /// </summary> /// <Param name = "array"> sort array </param> /// <param name = "start"> start position </param> /// <param name = "end "> sorting end position </param> // <param name =" left "> when the key has the same element, after sorting, the left edge of the middle part </param> /// <param name = "right"> when the key has the same element, right edge of the middle part </param> private static void SortUnit (int [] array, int start, int end, out int left, out int right) {int key = array [(start + end)/2]; int [] stackRight = new int [end-start + 1]; // The subscript int [] stackLeft = new int [end-start + 1] of the elements on the right of the key in the original array. // subscript int stackRightCount = 0; int stackLeftCount = 0; while (start <end) {while (start <end & array [end]> = key) {if (array [end] = key) {stackRight [stackRightCount ++] = end; // mark the bottom of the element equal to the key} end --;} if (start! = End) array [start] = array [end]; while (start <end & array [start] <= key) {if (array [start] = key) {stackLeft [stackLeftCount ++] = start; // record the lower mark of the element equal to the key} start ++;} if (start! = End) array [end] = array [start];} array [start] = key; // put the elements on the right of the key that are equal to the key on the right of the key int I; int index = 0; for (I = start + 1; I <= start + stackRightCount; I ++) {if (array [I]! = Key) {int temp = array [I]; array [I] = array [stackRight [index]; array [stackRight [index] = temp; index ++;} // place the elements on the left of the key in the index = 0; for (I = start-1; I> = start-stackLeftCount; I --) {if (array [I]! = Key) {int temp = array [I]; array [I] = array [stackLeft [index]; array [stackLeft [index] = temp; index ++ }}right = start + stackRightCount + 1; left = start-stackLeftCount-1 ;} // end of SortUnit /// <summary> /// insert sorting // </summary> private static void InsertionSort (int [] array) {int I, j; int temp; for (I = 1; I <array. length; I ++) {temp = array [I]; j = I-1; // compare with the number of sorted values one by one. If the value is greater than temp, after this number is removed, while (j> = 0 & array [j]> temp) {array [j + 1] = array [j]; j --;} array [j + 1] = temp ;}}} copy the code test code: copy the code using System; using System. collections. generic; using System. linq; using System. windows. forms; namespace Sort {public partial class Form1: Form {public Form1 () {InitializeComponent ();} /// <summary> // Generate Test Data /// </summary> private int [] CreateData () {Random rnd = new Random (); int count = 1000000; int _ max = 100000; int [] array = new int [count]; for (int I = 0; I <count; I ++) {array [I] = rnd. next (1, _ max);} return array;} private void button#click (object sender, EventArgs e) {DateTime dt1 = DateTime. now; // ================================================ ========================================================== ======= int [] array = CreateData (); list <int> list = array. toList <int> (); list. sort (); // ================================================ ========================================================== ======= DateTime dt2 = DateTime. now; // ================================================ ========================================================== ======= array = CreateData (); list = array. toList <int> (); list. sort (a, B) => a-B ); // ================================================ ========================================================== ======= DateTime dt3 = DateTime. now; // ================================================ ========================================================== ======= array = CreateData (); quickSort. sort (array ); // ================================================ ========================================================== ======= DateTime dt4 = DateTime. now; string t1 = dt2.Subtract (dt1 ). totalMilliseconds. toString (); string t2 = dt3.Subtract (dt2 ). totalMilliseconds. toString (); string t3 = dt4.Subtract (dt3 ). totalMilliseconds. toString (); MessageBox. show (t1 + "-" + t2 + "-" + t3 );}}}