C ++ common sorting algorithmsSelect blog from yuguanglou
// Select the sorting method selectionsort (INT arr [], int N)
Template <typename T>
Void selectionsort (T arr [], int N)
{
Int smallindex; // subscript of the smallest element in the table
Int pass, J; // used to scan subtable subscript
T temp; // temporary variable used to exchange table elements
// The range of Pass is 0 ~ N-2
For (pass = 0; pass <n-1; pass ++)
{
// Starts scanning the 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 <n; j ++)
// If a smaller element is found, the position is assigned to smallindex.
If (ARR [J] <arr [smallindex])
Smallindex = J;
// If smallindex and pass are not in the same position
// Swap the minimum entry in the sub-table with the ARR [pass]
If (smallindex! = Pass)
{
Temp = arr [pass];
Arr [pass] = arr [smallindex];
Arr [smallindex] = temp;
}
}
}
/*************************************** *********************************
Double-end selection Sorting Algorithm: This is a variant of the Sorting Algorithm selected above. You can locate the minimum and maximum elements in each subtable.
And place them at the beginning and end of the sub-table respectively.
**************************************** ********************************/
// Implement the deselsort () function of the double-end selection Sorting Algorithm
Template <typename T>
Void deselsort (T arr [], int N)
{
Int smallindex, largeindex; // subscript of the minimum and maximum elements in the table
Int leftpass = 0, rightpass = n-1, I, j; // used to scan subtable subscript on the left and right of the table
T temp; // temporary variable used for element exchange
While (leftpass <= rightpass)
{
// Scan the child table from the left and right
Smallindex = leftpass;
Largeindex = rightpass;
// J and I traverse the entire sub-Table arr [leftpass] ~ Arr [rightpass]
For (I = leftpass + 1; I <rightpass; I ++)
// If a smaller element is found, the position is assigned to smallindex.
If (ARR [I] <arr [smallindex])
Smallindex = I;
// If smallindex and leftpass are not in the same position
// Swap the minimum entry in the sub-table with the 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;
If (largeindex! = Rightpass)
{
Temp = arr [rightpass];
Arr [rightpass] = arr [largeindex];
Arr [largeindex] = temp;
}
// Shrink the sub-table from the two ends
Leftpass ++;
Rightpass --;
}
}
// Implement the bubblesort () function of the Self-compiled bubble sort algorithm
Template <typename T>
Int bubblesort (T arr [], int N)
{
Bool exchanged = false; // whether an exchange occurs
Int I, j; // subscripts used to traverse sub-tables
T temp; // temporary variable used for element exchange
// Start the traversal process. The following Mark J forms a sub-table, with a total of N-1 sub-tables
For (j = n-1; j> = 0; j --) // J shrinks n-1 ~ From the back to the front ~ 0 to form a sub-Table 0 ~ N-1, 0 ~ N-2, 0 ~ N-3 .. 0 ~ 1
{
Exchanged = false;
For (I = 0; I <j; I ++) // traverses the subtable range from 0 ~ J
{
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 traversal, it indicates that
// The traversal process is interrupted after sorting.
}
Return n-1-j;
}
// Implementation of bubblesortex (), a general algorithm function for sorting by Bubble Method
Template <typename T>
Int bubblesortex (T arr [], int N)
{
Int I, pass; // used to traverse subtable subscript
T temp; // temporary variable used for element exchange
// Start the traversal process. The following Mark J forms a sub-table, with a total of N-1 sub-tables
For (pass = 0; pass <n; pass ++) // The value of pass increases from 0 ~ N-1 to form the subtable 0 ~ N-1, 0 ~ N-2, 0 ~ N-3 .. 0 ~ 1
{
For (I = 0; I <n-pass; I ++) // traverses the subtable range from 0 ~ N-pass
{
If (ARR [I]> arr [I + 1])
{
Temp = arr [I];
Arr [I] = arr [I + 1];
Arr [I + 1] = temp;
}
}
}
Return pass;
}