Template Class ChainNode {
Friend Chain ; Template Friend Ostream & Operator << (Ostream & OS, Const Chain & C); T Data; CHAINNODE * LINK;
}
Template Class Chain {
Friend chainiterator ; private: chainNode * first; Bool Bubble (CHAINNODE * Current); // Removable function, start bubbling to Currentpubilc: void insert () from the last log of the list / Insert Algorithm Sorts the chain list, you must not create a new node and delete the old node void bubblesort (); // Sprinkler sorting void selectionsort (); // Select sorting void ranksort (); // count Sort
}
Template Void Chain :: INSERTIONSORT () // Insert algorithm is sorted aside the linked list, does not create new nodes and delete old nodes {
IF (first)
For (CHAINNODE * current = first; current-> link;) {// current-> link is the data to be inserted
For (CHAINNODE * P = first; p-> data link-> data; p = p-> link); // p Pointing the first larger than or equal to the data to be inserted into the data in the table IF (p == current-> link) {// There is no data current = current-> link; continue; // continues the next data insert} if (p! = current)} if (p! / The data to be inserted is moved to the firstmost number of CHAINNODE * n = current-> link-> link; current-> link-> link = p-> link; p-> link = current -> link; current-> link = n;} else current = current-> link; // If it is already behind the number than him, update current-> link T x = p-> link-> Data; // Exchange To insert Elements and His Fronts P-> LINK-> DATA = P-> Data; P-> Data = P-> Data; P-> Data = X;
}
} // Question 1: Insert Sort For the row of sequential linked lists still need to check N (n - 1), can I terminate insertion insertion?
Template Bool Chain :: Bubble (CHAINNODE * CURRENT) // Removable function, start bubbling to Current {bool sorted = true; // If the chain is row Good sequence (not exchanged), return True if (current && current-> link && current-> link-> link) sorted = bubble (current-> link); if (current-> data> current-> link-> DATA) {t temp = current-> data; current-> data = current-> link-> data; current-> link-> data = temp; sorted = false;} return sorted;} template void chain :: bubblesort () // Sprinkler = false; for (chainnode * start = first; start && start-> link &&! Sorted; start = start-> link) sorted = bubble (START);} Question 2: Can the recursive function can be sorted by the same number of times?
Template void chain :: selectionsort () // Select sort {bool sorted = false; for (chainNode * start = first; start && start-> link &&! sorted; start = start- > link) {sorted = true; for (CHAINNODE * current = start-> link; current; current = current-> link) {if (current-> data data) {// exchange t temp = current-> data; current-> data = start-> data; start-> data = Temp;} if (sorted && current-> link && current-> data> current-> link-> data) // If you are on the list There is a big item than the latter item, indicating that there is no sorted = false;}}} problem 3: Now I have to exchange it more than the Start big node, can be exchanged in each search process once?
Template Void Chain :: Ranksort () // Count {INT * R = New Int Int [Length ()]; // Storage Rank INT i = 0; for (CHAINNODE * P = First; p = p-> link) // Initialization Rank R [i ] = 0; i = 0; // Start position int J; // Current position for (CHAINNODE * start = first; Start && start-> link; start = start-> link) {// calculation ranking J = i; for (CHAINNODE * current = start-> link; current; current = current-> link) {J ; if (start-> data data) R [j] ; else r [i] ;} i ;} i = 0; for (chainnode * start = first; start && start-> Link; start = start-> link, i ) while (r [i]! = i) {INT T = R [i]; chainNode * current = start; for (int J = i; j link; t temp = current-> data; current-> data = start-> data; start-> data = temp; r [i] = r [t]; r [t] = t DELETE [] R;} Question 4: Is it the principal of my algorithm? Original, welcome to not correct. There are four questions troublesome masters. If you have a better sorting method, please send me a point of leaf_jo@msn.com Thank you!