template <typename t> void list<t>::sort (Listnodeposi (T) p, Span class= "Hljs-keyword" >int N) {//list interval sort The Switch (rand ()% 3 ) {//randomly selects the sorting algorithm. Flexible selection or expansion of case 1 : Insertionsort, depending on the characteristics of the problem (P, N); break ; //insert sort case 2 : Selectionsort (P, N); break ; //Select sort default : MergeSort (P, N); break ; //merge sort }}
Let's take a look at an ordered list lookup algorithm:
Template<TypeNameT>//In N (true) precursors of node P (possibly trailer) within the sequence list, find the last person not greater than eListnodeposi (t) list<t>::search (tConst& E,intN, Listnodeposi (T) p)Const{//assert:0 <= n <= rank (p) < _size /*dsa*/printf("Searching for"); Print (e);printf(": \ n"); while(0<= n--)//For the nearest n precursor of P, compare right to left /*dsa*/{printf("<%4d>", P->pred->data);if((p = p->pred)->data) <= e) Break;//until hit, value out of bounds, or range crossed /*dsa*/}printf("\ n");//Assert: At this point P must conform to the output semantics convention--Although the last key code comparison may not be meaningful (equivalent to-inf comparison) returnP//Return to the location where the find was terminated}///failure, return the precursor of the left boundary of the interval (possibly the header)--the caller can determine success by valid ()
Insert Sort
Template<TypeName T> Insert Sort algorithm for list: Sort n elements starting at position pvoid List<T>:: Insertionsort(Listnodeposi (T) p, int n) {//valid (p) && rank (p) + N <= size/*dsa*/printf"Insertionsort ... \ n"); for (int r= 0; R<N R++) {//For each node individuallyInsertAfter (Search (p -Data, R, p), p -Data);//Find the appropriate location and insertP=P -Succ Remove (p -PRED);//Turn to next node}}
Let's take a look at the list of algorithms for finding the largest node:
template <typenamestaticboolreturn//less than
Template<TypeName T> //Select the largest from n elements starting at position pListnodeposi (T)List<T>:: Selectmax(Listnodeposi (T) p, int n) {Listnodeposi (T)Max =P//MAX person Tentative first node PFor (Listnodeposi (T) cur=P1 <N N--)//From the first node p, the subsequent nodes are compared with Max one by one if(!LT((cur=Cur -Succ -Data,Max -Data))//If the current element is not less than Max, the Max =Cur//Update max element location record return Max;//Returns the maximum node position}
Select Sort:
Template<TypeName T> //List selection sorting algorithm: Sort n elements starting at position pvoid List<T>:: Selectionsort(Listnodeposi (T) p, int n) {//valid (p) && rank (p) + N <= size /*dsa*/printf"Selectionsort ... \ n"); Listnodeposi (T) head=P -Pred Listnodeposi (T) tail=P for (int i= 0; I<N I++) tail=Tail -Succ//To sort the interval as (head, tail) while(1 <N) {//At least two nodes remaining, within the range to be sortedListnodeposi (T)Max =Selectmax (head -SUCC, N);//Find the biggest (the latter is preferred when ambiguous)InsertBefore (tail, remove (Max));//Move it to the end of the unordered interval (as the new first element of the ordered interval)Tail=Tail -Pred N--; }}
List Merge Sort:
Template<TypeName T> //merge of sequence tables: n elements from P in the current list, merged with M elements from Q in list Lvoid List<T>:: Merge(Listnodeposi (T)&p, int n,List<T>&L, Listnodeposi (T) q, int m) {//Assert:this.valid (p) && rank (p) + N <= size && this.sorted (p, N)//L.valid (q) && rank (q) + M <= l._size && l.sorted (q, m)//Note: In the case of merge sort, it is possible this = = L && rank (p) + n = rank (q)Listnodeposi (T) pp=P -Pred//using a precursor (possibly the header) to return to the front ... while(0 <M//Before Q has moved out of range if((0 <N&&(p -Data <=Q -Data))//If P is still within the range and V (p) <= V (q), the{if(q==(p=P -SUCC)) break; N--; }//replace P with its direct successor (equivalent to listing p in the merged list) Else //If P has exceeded right boundary or V (q) < V (p), then{insertbefore (P, L.Remove ((q=Q -Succ -pred)); M--; }//Transfer Q to P beforeP=Pp -Succ//Determine the (new) starting point of the merge interval}
List::sort sort