/*************************************** **************************************** ****************************************: Various sorting algorithms * Author: JarvisChu * hour: 2010-05-01 * knowledge point: insert directly sort: 1. starting from I = 2, if L [I]> L [I-1], enter 2. L [0] = L [I], temporarily store L [I]. We Convert L [I] to the element to be inserted. 3. L [I] = L [I-1], L [I-1] after a shift, occupying the original L [I] position 4. move all the data before the I-1 that is larger than the element to be inserted 5. insert the elements to be inserted into a Shell sorting logic behind a number no greater than it: first, divide the table into several groups, and each group performs direct insertion and sorting, next, let's take a look at the overall direct insertion sorting. Understanding: the direct insertion sorting in the group enables small key elements to quickly jump to the front, key elements can jump to the next ******************************* **************************************** **************************************** * *****************/# include <iostream> using namespace std; # define MAX_ARRAY_SIZE 300 typedef int ElemType; // element type/* sequence storage structure of the static search table */typedef struct {ElemType elem [MAX_ARRAY_SIZE]; int length;} SSTable; /* initialize the static search table */bool InitSSTable (SSTable * pSSTable) {pSSTable-> length = 8; // the actual number of elements pSSTable-> elem [0] = 0; // monitoring whistle pSSTable-> elem [1] = 49; // pSSTable-> elem [2] = 38; // pSSTable-> elem [3] = 65; // pSSTable-> elem [4] = 97; // pSSTable-> elem [5] = 76; // pSSTable-> elem [6] = 13; // pSSTable-> elem [7] = 27; // pSSTable-> elem [8] = 49; // return true ;} /* display the static table */void DisplaySSTable (SSTable table) {for (int I = 1; I <= table. length; I ++) {cout <table. elem [I] <"," ;}cout <endl ;}/ * Insert sort O directly (n * n) */void InsertSort (SSTable * pSSTable) {for (int I = 2; I <= pSSTable-> length; I ++) {if (pSSTable-> elem [I] <pSSTable-> elem [I-1]) {pSSTable-> elem [0] = pSSTable-> elem [I]; pSSTable-> elem [I] = pSSTable-> elem [I-1]; int j = I-2; while (pSSTable-> elem [0] <pSSTable-> elem [j]) {// move pSSTable-> elem [j + 1] = pSSTable-> elem [j]; j --;}/* int j; for (j = I-2; pSSTable-> elem [0] <pSSTable-> elem [j]; j --) {// cout <"move" <pSSTable-> elem [j] <"to" <pSSTable-> elem [j + 1] <endl; pSSTable-> elem [j + 1] = pSSTable-> elem [j];} */pSSTable-> elem [j + 1] = pSSTable-> elem [0] ;}}/* semi-insert sorting O (n * n) */void BInsertSort (SSTable * pSSTable) {for (int I = 2; I <= pSSTable-> length; I ++) {pSSTable-> elem [0] = pSSTable-> elem [I]; int low = 1; int high = I-1; while (low <= high) {int mid = (low + high)/2; if (pSSTable-> elem [mid]> pSSTable-> elem [0]) high = mid-1; // In the lower half else low = mid + 1; // In the upper half} // high + 1 is the position that should be inserted for (int j = I-1; j> = high + 1; j --) {pSSTable-> elem [j + 1] = pSSTable-> elem [j];} pSSTable-> elem [high + 1] = pSSTable-> elem [0];} /* sort Shell inserts incremental with dk */void ShellInsert (SSTable * pT, int dk) {for (int group = 1; group <= dk; group ++) {// divide into dk groups for (int I = group + dk; I <= pT-> length; I + = dk) {// insert and sort each group directly if (pT-> elem [I] <pT-> elem [I-dk]) {// The current element value, smaller than the value of the previous element in the group pT-> elem [0] = pT-> elem [I]; // temporarily place the current element at 0 and save pT-> elem [I] = pT-> elem [I-dk]; // move the previous element into the current position int j = I-2 * dk; // within the group, the first and second while in the current position (j> = group + dk & pT-> elem [j]> pT-> elem [0]) {// j> = the position of the first element of the Group, and, pT [j] is greater than the element pT-> elem [j + dk] = pT-> elem [j]; j-= dk ;} pT-> elem [j + dk] = pT-> elem [0] ;}}/ * Shell Insert sorting, incremental sequence dlta, directly insert and sort the table pointed to by pSSTable t times */void ShellSort (SSTable * pSSTable, int dlta [], int t) {for (int k = 0; k <t; k ++) {ShellInsert (pSSTable, dlta [k]) ;}/ * bubble sort (exchange sort) */void BubbleSort (SSTable * pT) {for (int I = 1; I <pT-> length; I ++) {// control the number of sorted workers, total pT-> length-1 trip for (int j = 1; j <= pT-> length-I; j ++) {if (pT-> elem [j]> pT-> elem [j + 1]) {ElemType temp = pT-> elem [j + 1]; pT-> elem [j + 1] = pT-> elem [j]; pT-> elem [j] = temp ;}}}/* Partition function, complete a quick sorting and return the Axis Position */int Partition (SSTable * pT, int low, int high) {int Partition = pT-> elem [low]; // The while (low