虽然写这个博客主要目的是为了给我自己做一个思路记忆录,但是如果你恰好点了进来,那么先对你说一声欢迎。我并不是什么大触,只是一个菜菜的学生,如果您发现了什么错误或者您对于某些地方有更好的意见,非常欢迎您的斧正!
8.1排序算法的下界
这一节讲了“决策树模型”,为什么要讲这个呢,就是为了告诉你两个定理:①在最坏情况下,任何比较排序算法都需要做Ω(nlgn)次比较。
②堆排序和归并排序都是渐近最优的比较排序算法
看不懂这个图也没什么关系吧。反正我不是理解很清晰。
8.2计数排序
计数排序的基本思想是:对每一个输入元素x,确定小于x的元素个数,利用这一信息,就可以直接把x放在它输出数组中的位置上了。例如,如果有17个数小于x,则x就应该在第18个位置上。感觉这个基本思路非常容易理解,我们就直接放代码了:
接下来是代码部分(建议复制后自己运行一下):
计数排序.h
#pragma once
typedef int dataType;
/*打印数组*/
void PrintCountArray(dataType arr[],int length);
/*初始化数组*/
void InitCountArray(dataType arr[], int length);
/*计数排序*/
void CountSort(dataType arr[], int length);
/*计数排序测试函数*/
void TestCountSort();
计数排序.cpp
#include "计数排序.h"
#include <stdlib.h>
#include <time.h>
#include <iostream>
using namespace std;
/*打印数组*/
void PrintCountArray(dataType arr[], int length)
{
for (int i = 0; i < length; i++)
{
cout << arr[i] << " ";
if ((i + 1) % 15 == 0)
cout << endl;
}
cout << endl; cout << endl;
}
/*初始化数组*/
void InitCountArray(dataType arr[], int length)
{
srand((unsigned)time(NULL));
for (int i = 0; i < length; i++)
{
arr[i] = rand() % 100 + 1;/*arr[i]在1-100之间*/
}
}
/*计数排序*/
void CountSort(dataType arr[], int length)
{
dataType *temp=new dataType[length];/*构造一个临时数组*/
int i,j;
for (i = 0; i < length; i++)/*这个临时数组每个数赋值为0*/
temp[i] = 0;
for (i = 0; i < length; i++)
{
int count = 0;
for (j = 0; j < length; j++)/*扫描待排序数组*/
{
if (arr[j] < arr[i])/*统计比arr[i]小的数*/
count++;
}
while (temp[count] != 0)/*如果排的位置已经有了,就往后移(防止两个或多个数相同带来错误)*/
count++;
temp[count] = arr[i];/*把数放到对应位置*/
}
for (i = 0; i < length; i++)/*把排好序的数组赋值给原数组*/
{
arr[i] = temp[i];
}
delete[] temp;/*销毁这个临时数组*/
}
/*计数排序测试函数*/
void TestCountSort()
{
int length;
cout << "输入你期望的数组长度:";
cin >> length;
dataType *arr = new dataType[length];
InitCountArray(arr, length);
cout << "输出待排序的数组:" << endl;
PrintCountArray(arr, length);
CountSort(arr, length);
cout << "输出排序后的数组:" << endl;
PrintCountArray(arr, length);
delete[] arr;
cout << endl;
cout << endl;
cout << "测试一下有多个数相同的情况:" << endl;
cout << "这是我们自定义的数组:"<<endl;
dataType arr2[15] = {6,6,6,8,8,8,8,20,1,3,5,6,7,15,15};
PrintCountArray(arr2, 15);
CountSort(arr2, 15);
cout << "输出排序后的数组:" << endl;
PrintCountArray(arr2, 15);
}
主函数
#include "计数排序.h"
#include <stdio.h>
int main()
{
TestCountSort();
getchar();
getchar();
return 0;
}
运行结果
8.3基数排序
基数排序(radix sort)属于“分配式排序”(distribution sort),又称“桶子法”(bucket sort)或bin sort,顾名思义,它是透过键值的部份资讯,将要排序的元素分配至某些“桶”中,藉以达到排序的作用,基数排序法是属于稳定性的排序,其时间复杂度为O (nlog(r)m),其中r为所采取的基数,而m为堆数,在某些时候,基数排序法的效率高于其它的稳定性排序法。这个图解还是很清晰的,这是skywang12345 的博客(https://www.cnblogs.com/skywang12345/p/3603669.html)里的,我截图来的,感觉非常不错。
我们来看一下需要怎样的一些函数,或者说运行过程
①GetMaxNum获取数组中最大的数
②GetLoopTimes获取数字的位数,这样我们就知道要进行几次放数字的操作
③RadixSort进行基数排序,程序主体部分。(以个位排序为例)
1、建立一批桶buckets [ 10 ] [10]
2、求出arr[i]的个位数,比如8
3、把它放到8号桶的第一位上buckets[8][0](如果第一位有了,就放第二位)
4、将桶中的数倒回到arr数组中
④ControlRadixSort程序运行控制,即控制进行个位还是十位或者是百位的排序
接下来是代码部分(建议复制后自己运行一下):
基数排序.h
#pragma once
/*打印数组*/
void PrintRadixArray(int arr[], int length);
/*初始化数组*/
void InitRadixArray(int arr[], int length);
/*获取数组中最大的数*/
int GetMaxNum(int arr[], int length);
/*获取数字的位数*/
int GetLoopTimes(int num);
/*进行基数排序*/
void RadixSort(int arr[], int length,int loop);
/*程序运行控制*/
void ControlRadixSort(int arr[], int length);
/*基数排序测试函数*/
void TestRadixSort();
基数排序.cpp
#include "基数排序.h"
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <iostream>
using namespace std;
/*打印数组*/
void PrintRadixArray(int arr[], int length)
{
for (int i = 0; i < length; i++)
{
cout << arr[i] << " ";
if ((i + 1) % 15 == 0)
cout << endl;
}
cout << endl; cout << endl;
}
/*初始化数组*/
void InitRadixArray(int arr[], int length)
{
srand((unsigned)time(NULL));
for (int i = 0; i < length; i++)
{
arr[i] = rand() % 1000 + 1;/*arr[i]在1-1000之间*/
}
}
/*获取数组中最大的数*/
int GetMaxNum(int arr[], int length)
{
int max = 0;
for (int i = 0; i < length; i++)
{
if (max < arr[i])
max = arr[i];
}
return max;
}
/*获取数字的位数*/
int GetLoopTimes(int num)
{
int count = 1;
num /= 10;
while (num!= 0)
{
count++;
num /= 10;
}
return count;
}
/*进行基数排序*/
void RadixSort(int arr[], int length, int loop)
{
/*1、我们建立一批桶子*/
int buckets[10][10] = {0};/*全部赋值为0*/
/*如何求每一位上的数:
如256,它的个位=(256/1)%10=6
它的十位=(256/10)%10=5
它的百位=(256/100)%10=2
temp就是其中的1、10、100 */
int temp = (int)pow(10, loop - 1);
int i, j, k;
k = 0;
for (i = 0; i < length; i++)
{
int arrIndex = (arr[i] / temp) % 10;/*2、求出它的个位或十位或百位上的数*/
for (j = 0; j < 10; j++)
{
if (buckets[arrIndex][j] == 0)/*如果这个位置是空的*/
{
buckets[arrIndex][j] = arr[i];//3、把它放到arrIndex号桶的正确位置*/
break;
}
}
}
/*4、将桶中的数倒回到arr数组中*/
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10; j++)
{
if (buckets[i][j] != 0)
{
arr[k] = buckets[i][j];
buckets[i][j] = 0;
k++;
}
}
}
}
/*程序运行控制*/
void ControlRadixSort(int arr[], int length)
{
int max = GetMaxNum(arr, length);
int loop = GetLoopTimes(max);
/*对每一位进行桶分配*/
for (int i = 1; i <= loop; i++)
{
RadixSort(arr, length, i);
}
}
/*基数排序测试函数*/
void TestRadixSort()
{
int length;
cout << "输入你期望的数组长度:";
cin >> length;
int *arr = new int[length];
InitRadixArray(arr, length);
cout << "输出待排序的数组:" << endl;
PrintRadixArray(arr, length);
ControlRadixSort(arr, length);
cout << "输出排序后的数组:" << endl;
PrintRadixArray(arr, length);
delete[] arr;
}
主函数
#include "基数排序.h"
#include <stdio.h>
int main()
{
TestRadixSort();
getchar();
getchar();
return 0;
}
运行结果
8.4桶排序
桶排序假设输入数据服从均匀分布,平均情况下它的时间代价为O(n)。老实说,基数排序与桶排序十分相似,要说它们的不同点,大概就是,基数排序得对每一位进行一次排序,而桶排序仅仅针对最大的那一位,剩下的就是桶里面自己排序了。这个自己排序怎么排,我发现其实书中调用的是sort函数,emm,就是一个现成的函数,在algorithm头文件里。但是我不知道怎么去调用它,就很迷,所以我就放弃了这个,而且看网上一些关于桶排序的文章,也感到云里雾里,我就先跳过了,要是以后我有醍醐灌顶的时候,我再更这个地方吧。
参考网站以及博客:
百度百科https://baike.baidu.com/item/%E8%AE%A1%E6%95%B0%E6%8E%92%E5%BA%8F/8518144?fr=aladdin
算法导论第八章线性时间排序(CloudDeveloper的博客)
https://www.cnblogs.com/bakari/p/4845016.html
skywang12345 的博客
https://www.cnblogs.com/skywang12345/p/3603669.html