C ++ common sort algorithm

zhaozj2021-02-16  59

// Select Sort Method SelectionSort (int Arr [], int N) template void selectsort (t arr [], int n) {int smallindex; // Table minimum content of the subscript INT Pass, J; / / Used to scan the subscript T Temp; // Used to exchange the temporary variable of the table element

// PASS range is 0 ~ n-2 for (Pass = 0; Pass

// If you find a smaller element, assign the location to SmallIndex if (Arr [J]

// If SmallIndex and Pass are not in the same position //, the minimum term in the sub-table is exchanged with Arr [Pass] {TEMP = Arr [Pass]; Arr [pass] = arr [smallindex] Arr [shortindex] = TEMP;}}}

/ ************************************************** *********************** Double-end selection algorithm: is a variant of the sequencing algorithm above, can locate the minimum and maximum elements in each subtray and put them Since the beginning and end of the child table. *************************************************** ******************************** /// Double-end selection Sort algorithm function deselsort () Template void desilsort (t arr [], int N) {int smallindex, largeIndex; // 表 表 下 下 下 下 = 0, Rightpass = N-1, I, J; // Used from Table The subscript T Temp of the sub-table on the left and right side; // Temporal variables for exchange elements should "LEFTPASS <= Rightpass) {// From the left and right edges SMALLINDEX = LEFTPASS; LARGEINDEX = RightPass; // j And i traversing the entire sub-table arr [leftpass] for (i = leftpass 1; i

// If SmallIndex and LeftPass are not in the same position //, the minimum item in the sub-table is exchanged with Arr [Pass] {TEMP = Arr [LeftPass]; Arr [LeftPass] = Arr [SmallIndex] Arr [shortindex] = TEMP;

For (j = rightpass-1; j> leftpass; j--) if (arr [j]> arr [largeIndex]) LARGEINDEX = J;

IF (LargeIndex! = rightpass) {temp = arr [rightpass]; arr [rightpass] = arr [largeIndex]; arr [largeIndex] = Temp;} // from two contracts LeftPass ; RightPass--;}

// Self-editing routing algorithm function bubblesort () Implementation Template int bubblesort (t arr [], int n) {bool exchanged = false; // Does exchange INT i, j; // The subscript T TEMP; / / Temporary variable for exchange elements

/ / Start traversal process, the following standard j constitutes a sub-table, a total of N-1 sub-table for (j = n-1; j> = 0; J - J-j), never shrink from N-1 ~ 0, Take the composition sub-table 0 to N-1, 0 ~ N-2, 0 ~ N-3..0 ~ 1 {Exchanged = false; for (i = 0; I Arr [i 1]) {temp = arr [i]; arr [i] = arr [i 1]; Arr [i 1] = Temp; Exchanged = true; }}} If (!!) Return nj-1; // If there is no exchange in a traversal, it is said that it has been //, interrupt traversal process} return n-1-j;}

// 泡 法 排 一;>>;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; // / Temporary variable for exchange elements

/ / Start traversal process, the following standard j constitutes a sub-table, a total of N-1 sub-table for (Pass = 0; Pass arr [i 1]) {TEMP = Arr [I]; Arr [i] = arr [i 1]; Arr [i 1] = Temp;}}} Return Pass;}}}

转载请注明原文地址:https://www.9cbs.com/read-22322.html

New Post(0)