Practical sortingAlgorithm(The complexity is less than or equal to O (N ^ 2), which is the least efficient but not the simplest. C and C ++ textbooks always like to give a big lecture, it is not conducive to beginners to develop"ProgramEfficiency.
In fact, apart from the complicated implementation of heap sortingCodeMost algorithms are no more complicated than bubble algorithms.
Here are some examples of high-speed sorting algorithms.
Nest sorting, the fastest Sorting Algorithm for sorting byte strings and wide byte strings, the variant of counting sorting (the size of the counting buffer is fixed, with less traversal overhead), and the speed is STD :: more than 20 times of sort, and more importantly, the implementation is extremely simple! The disadvantage is that a buffer with a size at least equal to the value range of the array to be sorted is not suitable for int and other large-range data.
C/C ++ code
Void pigeonholesort (byte * array, int length)
{
Int B [256] = {0 };
Int I, K, j = 0;
For (I = 0; I <length; I ++)
B [array [I] ++;
For (I = 0; I <256; I ++)
For (k = 0; k <B [I]; k ++)
Array [J ++] = I;
}
count sorting by multiple traversal. If the byte string is sorted, the speed is about half of the byte string sorting speed.
C/C ++ Code
void countingsort (byte * array, int length)
{< br> int t;
int I, Z = 0;
byte min, max;
int * count;
min = max = array [0];
for (I = 0; I {< br> If (array [I] min = array [I];
else if (array [I]> MAX)
max = array [I];
}< br> COUNT = (int *) malloc (max-min + 1) * sizeof (INT ));
for (I = 0; I count [I] = 0;
for (I = 0; I count [array [I]-min] ++;
For (t = 0; t <= 255; t ++)
For (I = 0; I <count [T-min]; I ++)
Array [Z ++] = (byte) T;
Free (count );
}
Fast sorting: The most standard Recursive Implementation of quick sorting, with a speed of about half of STD: Sort
C/C ++ code
Void swap (byte * a, byte * B)
{
Byte TMP;
If (! = B)
{
TMP = *;
* A = * B;
* B = TMP;
}
}
int partition (byte * arr, int left, int right)
{< br> int I = left-1, j = right;
byte v = arr [right];
while (1)
{< br> while (ARR [++ I] while (ARR [-- J]> V)
If (j = 1)
break;
if (I> = J)
break;
swap (& arr [I], & arr [J]);
}< br> swap (& arr [I], & arr [right]);
return I;
}
Void quicksort (byte * arr, int left, int right)
{
If (left <right)
{
Int I = partition (ARR, left, right );
Quicksort (ARR, left, I-1 );
Quicksort (ARR, I + 1, right );
}
}
Void quicksort (byte * array, int length)
{
Quicksort (array, 0, length-1 );
}
This is a three-way fast sorting speed equivalent to STD: sort.
C/C ++ code
Void swap (byte * a, byte * B)
{
Byte TMP;
If (! = B)
{
TMP = *;
* A = * B;
* B = TMP;
}
}
Void quicksort (byte * arr, int left, int right)
{
If (left <right)
{
Byte v = arr [right];
Int I = left-1, j = right, P = left-1, q = right, K = 0;
While (1)
{
While (ARR [++ I] <v );
While (ARR [-- J]> V)
If (j = left)
Break;
If (I> = J)
Break;
Swap (& arr [I], & arr [J]);
If (ARR [I] = V)
{
P ++;
Swap (& arr [p], & arr [I]);
}
If (ARR [J] = V)
{
Q --;
Swap (& arr [Q], & arr [J]);
}
}
Swap (& arr [I], & arr [right]);
J = I-1;
I ++;
For (k = left; k <= P; k ++, j --)
Swap (& arr [K], & arr [J]);
For (k = right-1; k> = Q; k --, I ++)
Swap (& arr [K], & arr [I]);
Quicksort (ARR, left, J );
Quicksort (ARR, I, right );
}
}
Void quicksort (byte * array, int length)
{
Quicksort (array, 0, length-1 );
}
The sorting efficiency is 1/3 of STD: sort.
C/C ++ code
Void combsort (byte * arr, int size)
{
Uint Gap = size, swapped = 1, I = 0;
Byte swap = 0;
While (GAP> 1) | swapped)
{
If (GAP> 1)
Gap = gap/1.3;
Swapped = 0;
I = 0;
While (GAP + I) <size)
{
If (ARR [I]-Arr [I + Gap]> 0)
{
Swap = arr [I];
Arr [I] = arr [I + Gap];
Arr [I + Gap] = swap;
Swapped = 1;
}
++ I;
}
}
}
LSD base sorting, which is equivalent to STD: Sort speed, but requires a buffer as large as the input buffer
C/C ++ code
# Define R 256
# Define digit (a, d) (a> 8 * D)
Static byte * aux;
Void radix_sort (byte * arr, int left, int right)
{
If (left <right)
{
Int D = 0;
For (D = 3; D> = 0; d --)
{
Int I = 0, j = 0, Count [R + 1];
For (j = 0; j <r; j ++)
Count [J] = 0;
For (I = left; I <= right; I ++)
Count [digit (ARR [I], D) + 1] ++;
For (j = 1; j <r; j ++)
Count [J] + = count [J-1];
For (I = left; I <= right; I ++)
Aux [count [digit (ARR [I], d)] ++] = arr [I];
For (I = left; I <= right; I ++)
Arr [I] = aux [I-1];
}
}
}
Void radixsort (byte * array, int length)
{
Aux = (byte *) malloc (length );
Radix_sort (array, 0, length-1 );
Free (Aux );
}
The efficiency of Merge Sorting is STD: Sort 1/6. The general implementation is recursion. However, unlike fast sorting, it is extremely easy to merge and change loops.
C/C ++ code
Void Merge (byte * array, int low, int mid, int high)
{
Int I, K;
Byte * temp = (byte *) malloc (high-low + 1 );
Int begin1 = low;
Int end1 = mid;
Int begin2 = Mid + 1;
Int end2 = high;
for (k = 0; begin1 <= end1 & begin2 <= end2; ++ K)
If (array [begin1] temp [k] = array [begin1 ++];
else
temp [k] = array [begin2 ++];
while (begin1 <= end1)
temp [k ++] = array [begin1 ++];
while (begin2 <= end2)
temp [k ++] = array [begin2 ++];
for (I = 0; I <(high-low + 1); I ++)
array [Low + I] = temp [I];
free (temp);
}
Void merge_sort (byte * array, uint first, uint last)
{
Uint mid, I;
For (mid = 1; Mid <= last-first; Mid + = mid)
For (I = first; I <= last-mid; I ++ mid)
Merge (array, I, I + mid-1, min (I + Mid + mid-1, last ));
}
Void mergesort (byte * array, uint length)
{
Merge_sort (array, 0, length-1 );
}