0. General Sort Algorithms
Strategy | Algorithm | In/Out-place | Stability | ||
exchang | IntersectSort | In-place | Steady | ||
exchange | BubbleSort | In-place | |||
exchange | SelectSort | In-place | |||
exchange | ShellSort | In-place | |||
exchange | MergeSort | Out-place | |||
exchange | HeapSort | In-place | Unsteady | ||
exchange | QuickSort | In-place | Unsteady | ||
linear time sort | CountSort | Out-place | Steady | ||
linear time sort | BucketSort | Out-place | Steady | ||
linear time sort | RadixSort | Out-place | Steady |
1. IntersectSort
void IntersectSort(int array[], int length)
{
for (int j = 1; j < length; ++j)
{
int key = array[j];
int i = j - 1;
while (i >= 0 && key < array[i])
{
array[i + 1] = array[i];
--i;
}
array[i + 1] = key;
}
}
2. BubbleSort
void BubbleSort(int array[], int length)
{
for (int j = 0; j < length - 1; ++j)
{
for (int i = 0; i < length - 1 - j; ++i)
{
if (array[i] > array[i + 1])
swap(array[i], array[i + 1]);
}
}
}
3. Select Sort
void SelectSort(int array[], int length)
{
for (int j = 0; j < length; ++j)
{
int minIndex = j;
int min = array[j];
for (int i = j; i < length - 1; ++i)
{
if (min > array[i + 1])
{
min = array[i + 1];
minIndex = i + 1;
}
}
swap(array[j], array[minIndex]);
}
}
4. ShellSort
void ShellSort(int array[], int length)
{
int increment;
for (increment = length / 2; increment > 0; increment /= 2)
{
for (int i = increment; i < length; ++i)
{
int temp = array[i];
int j;
for (j = i; j >= increment; j -= increment)
{
if (temp < array[j - increment])
array[j] = array[j - increment];
else
break;
}
array[j] = temp;
}
}
}
5. QuickSort
void QuickSort(int array[], int left, int right)
{
int length = right - left + 1;
if (length <= 1)
return;
srand((unsigned)time(nullptr));
int kindex = rand() % length + left;
swap(array[left], array[kindex]);
int key = array[left], i = left, j = right;
while (i < j)
{
while (array[j] >= key && i < j)
--j;
if (i < j)
array[i] = array[j];
while (array[i] < key && i < j)
++i;
if (i < j)
array[j] = array[i];
}
array[i] = key;
QuickSort(array, left, i - 1);
QuickSort(array, j + 1, right);
}
6. HeapSort
void Adjust(int array[], int index, int length)
{
int lChild = 2 * index + 1;
int rChild = 2 * index + 2;
int maxId = index;
if (lChild < length && array[lChild] > array[maxId])
maxId = lChild;
if (rChild < length && array[rChild] > array[maxId])
maxId = rChild;
if (maxId != index)
{
swap(array[index], array[maxId]);
Adjust(array, maxId, length);
}
}
void HeapSort(int array[], int length)
{
// build heap
for (int i = length / 2 - 1; i >= 0; --i)
Adjust(array, i, length);
for (int i = length - 1; i >= 0; --i)
{
swap(array[i], array[0]);
Adjust(array, 0, i);
}
}
7. MergeSort
void Merge(int array[], int left, int mid, int right)
{
int length = right - left + 1;
int temp[length];
int p1 = left, p2 = mid + 1;
int i = 0;
while (p1 <= mid && p2 <= right)
temp[i++] = array[p1] > array[p2] ? array[p2++] : array[p1++];
while (p1 <= mid)
temp[i++] = array[p1++];
while (p2 <= right)
temp[i++] = array[p2++];
for (int i = 0; i < length; i++)
array[left + i] = temp[i];
}
void MergeSort(int array[], int left, int right)
{
if (left < right)
{
int mid = (left + right) / 2;
MergeSort(array, left, mid);
MergeSort(array, mid + 1, right);
Merge(array, left, mid, right);
}
}
8. RadixSort
void RadixSort(int array[], int length)
{
int max = array[0];
for (int i = 1; i < length; ++i)
max = array[i] > max ? array[i] : max;
int radixCount = 0;
while (max > 0)
{
max /= 10;
radixCount++;
};
// create bucket
std::vector<std::vector<int>> bucket;
std::vector<int> slot(0, 0);
bucket.assign(10, slot);
int multiplier = 0;
while (multiplier < radixCount)
{
int div = std::pow(10, multiplier);
for (int i = 0; i < length; ++i)
{
int bucketId = (array[i] / div) % 10;
bucket[bucketId].push_back(array[i]);
}
int index = 0;
for (int bucketId = 0; bucketId < 10; ++bucketId)
{
if (!bucket[bucketId].empty())
{
for (int value : bucket[bucketId])
{
array[index++] = value;
}
}
}
//clear bucket
for (auto &slot : bucket)
slot.clear();
++multiplier;
}
}
9. BucketSort
void BucketSort(int array[], int length)
{
int max = array[0];
for (int i = 1; i < length; ++i)
max = array[i] > max ? array[i] : max;
int *bucket = new int[max + 1];
for (int i = 0; i < length; ++i)
bucket[array[i]]++;
int index = 0;
for (int j = 0; j < max + 1; ++j)
for (int k = 1; k <= bucket[j]; ++k)
array[index++] = j;
delete[] bucket;
}