C ++ common sort algorithm

xiaoxiao2021-03-06  71

// Select Sort Method Selectionsort (int Arr [], int N)

Template

Void Selectionsort (T Arr [], INT N)

{

INT smallindex; // The minimum element of the table

INT Pass, J; // Used to scan the subscript

T Temp; // Temporary variables used to exchange table elements

// PASS range is 0 ~ N-2

For (Pass = 0; Pass

{

// Start scan sub-table from the subscript pass

SmallIndex = Pass;

// j traverses the entire sub-table Arr [Pass 1] to Arr [N-1]

For (j = pass 1; j

// If you find a smaller element, assign this location to SmallIndex

IF (Arr [J]

SmallIndex = J;

// If SmallIndex and Pass are not in the same location

// The minimum term in the sub-table is exchanged with Arr [Pass]

IF (SmallIndex! = Pass)

{

Temp = arr [pass];

Arr [pass] = arr [smallindex];

Arr [smallindex] = TEMP;

}

}

}

/ ************************************************** **********************

Dual-end selection algorithm: is a variant of the sequencing algorithm above, and can locate the smallest and maximum elements per subtaby.

And put them on the beginning and end of the child table.

*********************************************************** ********************* /

// Double-end selection ordering algorithm DESELSORT () implementation

Template

Void Deselsort (T Arr [], INT N)

{

INT SmallIndex, LargeIndex; // The smallest and maximum elements in the table

INT LEFTPASS = 0, Rightpass = N-1, I, J; // Used to subscript from the table left and right side

T Temp; // Temporary variable for exchange elements

While (LeftPass <= rightpass)

{

/ / From the left and right side to start scanning sub-table

SmallIndex = LeftPass;

LargeIndex = rightpass;

// j and i traversing the entire sub-table arr [leftpass] ~ arr [rightpass]

For (i = leftpass 1; i

// If you find a smaller element, assign this location to SmallIndex

IF (Arr [i]

SmallIndex = i;

// If SmallIndex and LeftPass are not in the same location

// The minimum term in the sub-table is exchanged with Arr [Pass]

IF (SmallIndex! = LEFTPASS)

{

Temp = arr [leftpass];

Arr [leftpass] = arr [smallindex];

Arr [smallindex] = TEMP;

}

For (j = rightpass-1; j> leftpass; j -)

IF (Arr [J]> Arr [LargeIndex])

LargeIndex = J;

LargeIndex! = Rightpass

{

Temp = arr [rightpass];

Arr [rightpass] = arr [largeIndex];

Arr [LargeIndex] = TEMP;

}

/ / From two-headed shrink sheet

Leftpass ;

Rightpass -;

}

}

// Automatic Self-propelled Sort Algorithm Function Bubblesort ()

Template

Int bubblesort (t arr [], int N)

{

BOOL Exchanged = false; // Have you exchange

INT I, J; // Used to traverse the subscription

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 shrinks N-1 ~ 0 from the back, to constitute sub-tables 0 to N-1, 0 ~ N-2, 0 ~ N -3..0 ~ 1

{

EXCHANGED = FALSE;

For (i = 0; i

{

IF (Arr [i]> Arr [i 1])

{

Temp = arr [i];

Arr [i] = arr [i 1];

Arr [i 1] = TEMP;

Exchanged = True;

}

}

If (! Exchanged) return n-j-1; // If there is no exchange in a travers, it is already

// Sort, interrupt traversal process

}

Return n-1-j;

}

// Exercise method Sorting general algorithm function bubblesortex () implementation

Template

Int bubblesortex (t arr [], int N)

{

INT I, pass; // Used to traverse the subscription

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 (Pass = 0; Pass

{

For (i = 0; i

{

IF (Arr [i]> Arr [i 1])

{

Temp = arr [i];

Arr [i] = arr [i 1];

Arr [i 1] = TEMP;

}

}

}

Return Pass;

}

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

New Post(0)