这里用C++实现了前七大排序算法,还有三种线性时间排序以后加。
代码说明:
1.每种算法都封装成void algorithm(int array[], int len)函数
2.数据输入采用STL容器vector以实现任意个数数据的输入
3.只支持int型数据排序,输入其他类型(敲入字符)即可结束数据输入
4.主函数main()加入了可选参数,可指定用哪种算法,默认算法宏定义为快速排序
5.整个程序可通过g++编译,简单数据测试也未发现问题
6.吐槽:C++处理数据真是费劲。内存管理方面,成也由之,败也由之啊
#include<iostream>
#include<vector>
#include<cstring>
#define sortAlgorithm quickSort //设置默认算法
using namespace std;
//*****************************交换排序******************************************
void bubbleSort(int a[],int len){
int temp;
for(int i = 0;i < len-1;i++){
for(int j = 0;j < len-i-1;j++){
if(a[j]>a[j+1]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}
//------------------------------------------------------------
void heapAdjust(int a[],int rootIndex,int len){ //自上而下调整为大顶堆
int maxIndex = rootIndex;
int leftIndex = 2 * rootIndex + 1;//左孩子
int rightIndex = 2 * rootIndex + 2;//右孩子
if(leftIndex < len && a[leftIndex] > a[maxIndex])
maxIndex = leftIndex;
if(rightIndex < len && a[rightIndex] > a[maxIndex])
maxIndex = rightIndex;
if(maxIndex != rootIndex){
swap(a[rootIndex],a[maxIndex]);
heapAdjust(a,maxIndex,len);//对以孩子为根节点的堆递归调整
}
}
void heapBuild(int a[],int len){//自下而上建堆
for(int i = len-1;i>=0;i--)heapAdjust(a,i,len);
}
void heapSort(int a[],int len){
heapBuild(a,len);//先建一次大顶堆,找一个最大值
for(int i = len-1;i > 0;i--){
swap(a[0],a[i]);//将最大值放到数组最后
len--;
heapAdjust(a,0,len);//再找一次最大值放到堆顶
}
}
//*****************************选择排序*******************************************
void selectionSort(int a[],int len){
int temp,minIndex;
for(int i = 0;i < len-1;i++){
minIndex = i;
for(int j = i+1;j < len;j++)
if(a[j] < a[minIndex]) minIndex = j;
temp = a[i];
a[i] = a[minIndex];
a[minIndex] = temp;
}
}
//-----------------------------------------------------------------
void ksort(int start,int end,int a[]){
if(end < start + 2) return;
int e = end,s = start;
while (start < end){ //以第一个数a[s]为标准将数列分为前后两部分
while (++start < e && a[start] <= a[s]);
while (--end > s && a[end] >= a[s]);
if (start < end) swap(a[start],a[end]);
}
swap (a[end],a[s]);//为前半段换一个标准数
ksort(s,end,a);ksort(start,e,a);//递归
}
void quickSort(int a[],int len){ //为了统一函数参数,多写一个
ksort(0,len,a); //C++不支持函数嵌套定义!
}
//*******************************插入排序***************************************
void insertionSort(int a[],int len){
for(int i = 0;i < len-1;i++){
int doneIndex = i;
int undo = a[i+1];
while(doneIndex >=0 && undo < a[doneIndex]){
a[doneIndex+1] = a[doneIndex];//后移给undo腾位置
doneIndex--;
}
a[doneIndex+1] = undo;//插入
}
}
//-------------------------------------------------------------------------
void shellSort(int a[],int len){
for(int dis = len/2;dis > 0;dis/=2){ //按dis距离分组插入,3与5距离是2
for(int i = dis;i < len;i++){
int doneIndex = i - dis;
int undo = a[i];
while(doneIndex >= 0 && a[doneIndex] > undo){
a[doneIndex+dis] = a[doneIndex];
doneIndex -= dis;
}
a[doneIndex+dis] = undo;
}
}
}
//*******************************归并排序*****************************************
void merge(int a[],int left,int mid,int right){
int temp[right-left];
int t = 0;//暂存数组起始
int i = left;//前半部分起始
int j = mid + 1;//后半部分起始
while(i <= mid && j <= right){
if(a[i] <= a[j]){
temp[t] = a[i];
i+=1;t+=1;
}
else{
temp[t] = a[j];
j+=1;t+=1;
}
}
while(i <= mid){
temp[t] = a[i];
i+=1;t+=1;
}
while(j <= right){
temp[t] = a[j];
j+=1;t+=1;
}
for(int i = left;i <= right;i++)//重新写入a[]
a[i] = temp[i-left];
}
void mSort(int a[],int left,int right){
if(left >= right)return;
int mid = (left + right)/2;
mSort(a,left,mid);
mSort(a,mid+1,right);
merge(a,left,mid,right);
}
void mergeSort(int a[],int len){
mSort(a,0,len-1);
}
//********************************************************************************
int main(int argc,char* algo[]){
//--------------------------------------------输入
cout<<"Please input the numbers:\n";
vector<int> input;
//为了实现任意数量输入,使用STL容器vector
int x;
while(cin>>x)
input.push_back(x);
//---------------------------------------------排序
int len = input.size();
int arr[len];
for(int i = 0;i < len;i++)
arr[i] = input[i];
if(argc == 1)//选择排序算法
sortAlgorithm(arr,len);
else if(!strcmp(algo[1],"bubble"))
bubbleSort(arr,len);
else if(!strcmp(algo[1],"selection"))
selectionSort(arr,len);
else if(!strcmp(algo[1],"insertion"))
insertionSort(arr,len);
else if(!strcmp(algo[1],"shell"))
shellSort(arr,len);
else if(!strcmp(algo[1],"quick"))
quickSort(arr,len);
else if(!strcmp(algo[1],"heap"))
heapSort(arr,len);
else if(!strcmp(algo[1],"merge"))
mergeSort(arr,len);
else{
cout<<"No such algorithm! You can use the following one:\n\t1.bubble\n\t2.selection\n\t3.insertion\n\t4.shell\n\t5.quick\n\t6.heap\n\t7.merge "<<endl;
return 0;
}
//---------------------------------------------输出
for(int i = 0;i < len;i++)
cout<<arr[i]<<' ';
cout<<endl;
return 0;
}
2019.3