#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
template<class Type>
void Swap(Type& a, Type& b) //函数模板
{
Type tmp = a;
a = b;
b = tmp;
}
void BubbleSort(int* ar, int len) //冒泡排序
{
assert(ar != nullptr);
for (int i = 0; i < len - 1; ++i) //正循环
{
bool tag = true; //设计标记,减少时间复杂度,提高效率
for (int j = 0; j < len - 1 - i; ++j)
{
if (ar[j] > ar[j + 1])
{
tag = false;
Swap(ar[j], ar[j + 1]);
}
}
if (tag) break;
tag = true;
for (int j = len - 2 - i; j > i; --j) //逆循环,j不需要等于i,等于i的在正循环比较中已经比较过了
{
if (ar[j] < ar[j - 1])
{
tag = false;
Swap(ar[j], ar[j - 1]);
}
}
if (tag)break;
}
}
void SelectSort(int* ar, int len) //简单选择排序
{
assert(ar != nullptr);
int i, j, k;
for (i = 0; i < len - 1; ++i)
{
k = i; //k用于存放最小(大)值下标,以便退出内循环后和前面值进行交换;
for (j = i + 1; j < len; ++j)
{
if (ar[j] < ar[k])
{
k = j;
}
}
Swap(ar[k], ar[i]);
}
}
void InsertSort(int* ar, int len) //直接插入排序
{
assert(ar != nullptr);
for (int i = 1; i < len; ++i)
{
if (ar[i] < ar[i - 1])
{
int j = i - 1;
int tmp = ar[i];
do
{
ar[j + 1] = ar[j];
--j;
} while (j >= 0 && ar[j] > tmp);
ar[j + 1] = tmp;
}
}
}
int Partation(int* ar, int left, int right)
{
int mid = ar[left];
while (left < right)
{
while (left < right && ar[right] >= mid)
{
right--;
}
if (left < right)
{
ar[left] = ar[right];
}
while (left < right && ar[left] < mid)
{
left++;
}
if (left < right)
{
ar[right] = ar[left];
}
}
ar[left] = mid;
return left;
}
void QuickPass(int* ar, int left, int right)
{
if (left < right)
{
int pos = Partation(ar, left, right);
QuickPass(ar, 0, pos - 1);
QuickPass(ar, pos + 1, right);
}
}
void QuickSort(int* ar, int len) //快速排序
{
assert(ar != nullptr);
QuickPass(ar, 0, len - 1);
}
void Shell(int* ar, int len, int gap)
{
for (int i = gap; i < len; ++i)
{
if (ar[i - gap] > ar[i])
{
int j = i - gap;
int tmp = ar[i];
do
{
ar[j + gap] = ar[j];
j-=gap;
} while (j >= 0 && ar[j] > tmp);
ar[j + gap] = tmp;
}
}
}
void ShellSort(int* ar, int len) //希尔排序,多调用几次直接插入排序,但最后的gap一定为1;
{
assert(ar != nullptr);
int gap[] = { 5,3,1 };
int lengap = sizeof(gap) / sizeof(gap[0]);
for (int i = 0; i < lengap; ++i)
{
Shell(ar, len, gap[i]);
}
}
void Merge(int* ar, int len, int gap)
{
int low1 = 0; //设置四个指针
int high1 = low1 + gap - 1;
int low2 = high1 + 1;
int high2 = low2 + gap - 1 < len ? low2 + gap - 1 : len - 1;
int* br = (int*)malloc(sizeof(int) * len); //开辟内存用于存放俩组比较后的值;
if (br == nullptr) exit(1);
int i = 0;
while (low2 < len) //说明左右右手都拿到了数据
{
while (low1 <= high1 && low2 <= high2) //每俩个组判断哪个手先空
{
if (ar[low1] <= ar[low2])
{
br[i++] = ar[low1++];
}
else
{
br[i++] = ar[low2++];
}
}
while (low1 <= high1)
{
br[i++] = ar[low1++];
}
while (low2 <= high2)
{
br[i++] = ar[low2++];
}
low1 = high2 + 1;
high1 = low1 + gap - 1;
low2 = high1 + 1;
high2 = low2 + gap - 1 < len ? low2 + gap - 1 : len - 1;
}
while (low1 < len) //最后结果只能是右手抓空,然后把左手的值再依次放入br中
{
br[i++] = ar[low1++];
}
for (int i = 0; i < len; ++i) //最终把br的值一个一个赋给ar
{
ar[i] = br[i];
}
free(br);
br = nullptr;
}
void MergeSort(int* ar, int len) //归并排序
{
assert(ar != nullptr);
for (int i = 1; i < len; i *= 2)
{
Merge(ar, len, i);
}
}
void HeapAdjust(int* ar, int start, int end)
{
int tmp = ar[start];
for (int i = 2 * start + 1; i <= end; i = start * 2 + 1) //i的值就是根据父节点推出子节点
{
if (i < end && ar[i] < ar[i + 1]) //i<len意味着存在右孩子
{
++i;
}
if (ar[i] > tmp)
{
ar[start] = ar[i];
start = i;
}
else
{
break;
}
}
ar[start] = tmp;
}
void HeapSort(int* ar, int len) //堆排序
{
assert(ar != nullptr);
for (int i = (len - 1 - 1) / 2; i >= 0; --i) // 从最后一个根节点开始排列大顶堆
{
HeapAdjust(ar, i, len - 1);
}
for (int i = 0; i < len - 1; ++i)
{
Swap(ar[0], ar[len - 1 - i]); //然后让根节点和最后一个叶子节点交换值
HeapAdjust(ar, 0, (len - 1 - i) - 1); //然后继续排列大顶堆
}
}
int Get_Figure(int* ar, int len)
{
int max = 0;
for (int i = 0; i < len; ++i)
{
if (ar[i] > max)
{
max = ar[i];
}
}
int count = 0;
while (max != 0)
{
++count;
max /= 10;
}
return count;
}
int Get_Num(int n, int fin)
{
for (int i = 0; i < fin; ++i)
{
n /= 10;
}
return n % 10;
}
void Radix(int* ar, int len, int fin)
{
int bucket[10][20] = { 0 }; // 设置桶的大小,10个桶,每个容量为20;
int num[10] = { 0 }; //每个桶存放数据的数量
for (int i = 0; i < len; ++i)
{
int index = Get_Num(ar[i], fin); //得出此值该放入哪个桶
bucket[index][num[index]] = ar[i]; //放入对应的桶中
num[index]++; //此桶中数量加1;
}
int k = 0;
for (int i = 0; i < 10; ++i) //此时i小时10,因为一共10个桶,容易错写成i<len;
{
for (int j = 0; j < num[i]; ++j)
{
ar[k++] = bucket[i][j];
}
}
}
void RadixSort(int* ar, int len) //基数排序
{
assert(ar != nullptr);
int count = Get_Figure(ar, len); //循环次数依据最大值的位数
for (int i = 0; i < count; ++i)
{
Radix(ar, len, i);
}
}
void Print(int* ar, int len)
{
assert(ar != nullptr);
for (int i = 0; i < len; ++i)
{
printf("%-4d", ar[i]);
}
printf("\n");
}
int main()
{
int ar[] = { 13,21,3,79,4,98,35,58,60,99,30,46,66,1 };
int len = sizeof(ar) / sizeof(ar[0]);
//BubbleSort(ar, len);
//SelectSort(ar, len);
//InsertSort(ar, len);
//QuickSort(ar, len);
//ShellSort(ar, len);
//MergeSort(ar, len);
//HeapSort(ar, len);
RadixSort(ar, len);
Print(ar, len);
return 0;
}
八大排序你值得拥有
最新推荐文章于 2024-11-07 23:24:07 发布
本文详细介绍了多种排序算法的C++实现,包括冒泡排序、选择排序、插入排序、快速排序、希尔排序、归并排序和堆排序,并提供了基数排序的完整实现。通过这些排序算法,探讨了不同排序方法的时间复杂度和效率优化策略。
摘要由CSDN通过智能技术生成