Algorithm Manual (5) Preliminary Sorting Algorithm

Sorting is the process of sorting a group of objects according to certain rules. Even if you can use the sorting functions in the standard library, learning the Sorting Algorithm is still of great significance: learning the sorting algorithm can help you fully understand the algorithm performance comparison methods; similar technologies can effectively solve other types of problems. sorting algorithms are usually the first step in solving problems. More importantly, these algorithms are classic, elegant, and efficient. Sorting plays an important role in business data processing and analysis and modern science. Among them, quick sorting algorithms are hailed as one of the top ten algorithms in science and engineering in the 20th century. Today we have to look at a relatively simple but classic preliminary sorting algorithm, including selecting sorting, inserting sorting, and Shell sorting. Before getting started, we first agree on the algorithm class template form, where we put the sorting code into the sort () method, and the less () method returns the bool value, exch () for the elements to be compared () the method is used for element location exchange in the following format: copy the code public class Example {public static void sort (IComparable []) {// sorting code} private static void exch (IComparable [] a, int I, int j) {IComparable temp = a [I]; a [I] = a [j]; a [j] = temp;} private static bool less (IComparable v, IComparable w) {return v. compareTo (w) <0;} private static void show (IComparable [] A) {// print data} private static bool isSorted (IComparable [] a) {for (int I = 1; I <. length; I ++) {if (less (a [I], a [I-1]) return false;} return true ;}} copy the code here I use C # implementation, the less () method uses the IComparable interface method, which is applicable to any data types that implement the IComparable interface, and the system Int, String, and other types. Select sorting Overview: first find the smallest element of the array and swap it with the first element of the array. Again, find the smallest element in the remaining element and exchange it with the second element. Until the entire array is sorted. Because it constantly selects the smallest of the remaining elements, it is called selection sorting. Analysis: For an array with a length of N, it takes about (N ^ 2)/Two comparisons and N exchanges to sort the array. It is a simple sorting algorithm that is easy to understand and implement. It has two distinctive features: 1. the running time is irrelevant to the input. Experiments show that the input of an ordered array is as long as the input of a random array. 2. Data movement is minimal. Only N exchanges are selected for sorting, which cannot be done by other sorting algorithms. Code implementation: copy the public class Selection {public static void sort (IComparable [] a) {// sort code for (int I = 0; I <. length; I ++) {int min = I; for (int j = I + 1; j <. length; j ++) {if (less (a [j], a [min]) min = j;} exch (a, min, I );}} private static void exch (IComparable [] a, int I, int j) {IComparable temp = a [I]; a [I] = a [j]; a [j] = temp;} private static bool less (IComparable v, IComparable w ){ Return v. compareTo (w) <0;} private static void show (IComparable [] a) {// print data} private static bool isSorted (IComparable []) {for (int I = 1; I <. length; I ++) {if (less (a [I], a [I-1]) return false;} return true ;}} copy the code. The Selection algorithm class is designed according to our agreed rules. After running the code, you can find the correct sorting result. After executing the arrays of, and respectively, it is found that it takes a long time to select sorting for large-scale sorting. Small and Medium Scale sorting can be basically used. Insert sort Overview: insert each element to an appropriate position in an ordered sequence just like a regular sort card, to free up space for the inserted element, we need to move all other elements right before insertion. This algorithm is called insert sorting. Analysis: Unlike the selected sorting, the time required to insert the sorting depends entirely on the initial position of the element in the input. For example, sorting a large ordered array is much faster than sorting a random or reverse array. For an array with a random length of N and NO duplicate primary keys, it is required to insert the sorting on average ~ (N ^ 2)/4 comparisons and ~ (N ^ 2)/4 exchanges. In the worst case ~ (N ^ 2)/2 comparisons and ~ (N ^ 2)/2 exchanges, preferably requires N-1 times to compare and 0 exchanges. Implementation: copy the public class Insertion {public static void sort (IComparable [] a) {// sort the code int N =. length; for (int I = 1; I <N; I ++) {for (int j = I; j> 0 & less (a [j], a [j-1]); j --) exch (a, j, j-1); }}// optimize the number of public static void fasterSort (IComparable []) {int N =. length; for (int I = 1; I <N; I ++) {IComparable temp = a [I]; int j = I-1; while (j> 0 & less (temp, a [j]) {a [j + 1] = [J]; j --;} a [j + 1] = temp;} private static void exch (IComparable [] a, int I, int j) {IComparable temp = a [I]; a [I] = a [j]; a [j] = temp;} private static bool less (IComparable v, IComparable w) {return v. compareTo (w) <0;} private static void show (IComparable [] a) {// print data for (int I = 1; I <. length; I ++) {Console. writeLine (a [I]) ;}} private static bool isSorted (IComparable [] a) {( Int I = 1; I <. length; I ++) {if (less (a [I], a [I-1]) return false;} return true ;}} by copying code multiple experiment inputs, it is found that insertion sorting is quite slow in processing large data volumes. However, unlike sorting, when the input is large-scale ordered data (such as 100000 in size), the speed is much faster, while selecting sorting still takes a long time, this is better than sorting by choice. Shell sorting: Hill sorting is based on the insert sorting algorithm, but it is faster. For large-scale disordered arrays, insertion sorting is slow. They only exchange adjacent elements, so the elements can only be moved from one end to the other at 1.1 points. In order to speed up the sorting, Hill simply improved the insertion sorting, exchanged non-Adjacent Elements to sort the partial part of the array, and finally sorted the partial ordered array using insertion sorting. Analysis: the reason why Hill sorting is more efficient is that it balances the size and ordering of sub-arrays. Each sub-array is very short, and the sub-arrays are partially ordered after sorting, these two cases are suitable for insertion sorting. The degree of order of the sub-array depends on the choice of incremental sequence. It is still a challenge to thoroughly understand the performance of hill sorting, in fact, it is the only sorting method that cannot accurately describe the performance features of the out-of-order array. Implementation: copy the public class Shell {public static void sort (IComparable [] a) {// sort the code int N =. length; int h = 1; int j; while (h <N/3) h = 3 * h + 1; while (h> = 1) {for (int I = h; I <N; I ++) {IComparable temp = a [I]; // for (j = I-h; j> = h & less (temp, a [j]); j-= h) {a [j + h] = a [j];} a [j + h] = temp; // while writing // j = I-h; // while (j> 0 & less (temp, a [j]) // {// a [j + h] = a [j]; // J-= h; //} // a [j + h] = temp;} h/= 3 ;}} private static void exch (IComparable [], int I, int j) {IComparable temp = a [I]; a [I] = a [j]; a [j] = temp ;} private static bool less (IComparable v, IComparable w) {return v. compareTo (w) <0;} private static void show (IComparable [] a) {// print data for (int I = 1; I <. length; I ++) {Console. writeLine (a [I]) ;}} private static bool isSorted (IComparab Le [] a) {for (int I = 1; I <. length; I ++) {if (less (a [I], a [I-1]) return false;} return true ;}} after I copied the code for many experiments, I was amazed at the efficiency of Shell sorting. It takes only 4 s to sort the array with a size of 1 million, but the insertion sorting and selection sorting are not completed in 5 minutes. This is amazing, I really feel the power of mathematics, and a simple improvement can let a short piece of code burst into such a powerful force. After learning and implementing the preliminary sorting algorithm, I began to clearly understand the idea that it is the main reason to study the algorithm to solve problems that cannot be solved by other methods by improving the speed.