本篇博客是为了熟悉冒泡选择插入希尔归并快速基数排序这几种排序算法而写的,基本上是看了书上代码,然后自己手敲理解一遍。
#include <bits/stdc++.h>
using namespace std;
int lis1[9]={24,46,36,42,68,84,22,48,66};
int lis2[9]={24,46,36,42,68,84,22,48,66};
int n=9;
void turnlisbace()
{
for (int i=0;i<n;i++)
lis1[i]=lis2[i];
}
void output(int lis[],int n)
{
for (int i=0;i<n;i++)
cout<<lis[i]<<" ";
cout<<endl;
}
void InsertSort(int a[],int n) //插入排序
{
int temp,j;
for (int i=1;i<n;i++)
{
temp=a[i];
for (j=i-1;j>=0;j--)
{
if (a[j]>temp)
a[j+1]=a[j];
else
break;
}//此处跳出循环时,a[j]<temp,所以temp插入到j的后一位(j+1)。
a[j+1]=temp;
}
cout<<"The result of insertsort is here:"<<endl;
output(a,n);
cout<<endl;
}
void InsertSort_twodiv(int a[],int n) //二分插入排序
{
int temp,j,low,high,mid;
for (int i=1;i<n;i++)
{
temp=a[i];
low=0;high=i-1;
while (low<=high) //通过二分的方法找到已有序的部分里插入的位置
{
mid=(high+low)/2;
if (a[mid]>temp)
high=mid-1;
else
low=mid+1;
}
for (j=i-1;j>=high+1;j--)
a[j+1]=a[j];
a[j+1]=temp;
}
cout<<"The result of insersort_twodiv is here:"<<endl;
output(a,n);
cout<<endl;
}
void SelectSort(int a[],int n) //选择排序
{
for (int i=0;i<n-1;i++) //进行n-1趟排序,因为n-1的数确定好了位置,第n个数也确定好了位置
{
int mintemp=i,j;
for (j=i;j<n;j++)
{
if (a[j]<a[mintemp])
mintemp=j;
}
swap(a[i],a[mintemp]);
}
cout<<"The result of selectsort is here:"<<endl;
output(a,n);
cout<<endl;
}
void ShellSort(int a[],int n) //希尔排序
{
int temp,j;
for (int step=n/2;step>=1;step/=2)
{
for (int i=step;i<n;i++)
{
temp=a[i];
for (j=i-step;j>=0 && a[j]>temp;j-=step)
a[j+step]=a[j];
a[j+step]=temp;
}
}
cout<<"The result of shellsort is here:"<<endl;
output(a,n);
cout<<endl;
}
void BubbleSort(int a[],int n) //冒泡排序
{
bool flag=true;
for (int i=0;i<n;i++)
{
for (int j=n-1;j>=i;j--)
{
if (a[j]<a[j-1])
{
swap(a[j],a[j-1]);
flag=false;
}
}
if (flag==true)
break;
}
cout<<"The result of bubblesort is here:"<<endl;
output(a,n);
cout<<endl;
}
//快速排序,快速排序需要两个函数完成
//取标准轴函数,同时在这里完成大于标准轴的数放在右边,小于标准轴的放在左边的操作
int Partition(int a[],int low,int high)
{
int standardint =a[low];
while (low<high)
{
while (a[high]>=standardint && low<high) //左边数满足时候,high--,寻找到不满足的赋值到右边
high--; //(右边下标处为刚刚不满足的值(或为标准值),所以不会发生丢失)
a[low]=a[high];
while (a[low]<=standardint && low<high) //右边数满足时,low++,寻找到不满足的赋值到左边
low++; //(左边下标处为刚刚不满足的值,所以不会发生丢失)
a[high]=a[low];
}
a[low]=standardint; //将标准值赋值到中间位置,此时,大于的值在右,小于值在左
return low;
}
void QuickSort(int a[],int low,int high) //使用递归的手法,依次向下快排
{
if (low<high)
{
int Pivot=Partition(a,low,high);
QuickSort(a,low,Pivot-1);
QuickSort(a,Pivot+1,high);
}
}
//归并排序,同样需要两个函数完成
void merge(int a[],int low,int mid,int high) //将low——mid,mid+1——high的的两个数组归并到一起
{
int b[high-low+1];
int i,j,k;
for (i=low,j=mid+1,k=0;i<=mid && j<=high;k++)
{
if (a[i]<a[j])
b[k]=a[i++];
else
b[k]=a[j++];
}
while (i<=mid)
b[k++]=a[i++];
while (j<=high)
b[k++]=a[j++];
for (i=low,k=0;i<=high;i++)
a[i]=b[k++];
}
void MergeSort(int a[],int low,int high) //同样使用递归函数
{
if (low<high)
{
int mid=(low+high)/2;
MergeSort(a,low,mid);
MergeSort(a,mid+1,high);
merge(a,low,mid,high);
}
}
//基数排序,参考网上的,目前还没仔细理解代码
int max(int date[],int n)
{
int max=0;
for(int i=0;i<n;i++){
int count=1,tem=date[i];
while(tem/10!=0){
tem=tem/10;
count++;
}
if(count>max)
max=count;
}
return max;
}
void BaseSort(int date[],int n)
{
int maxx=max(date,n);
int num=1;
for(int i=0;i<maxx;i++)
{
int count[10];
int temp[10][‘n‘];
for(int f=0;f<10;f++)
{
count[f]=0;
}
for(int g=0;g<10;g++)
{
for(int z=0;z<n;z++){
temp[g][z]=0;
}
}
for(int j=0;j<n;j++)
{
int fg=date[j]/num;
int k=fg%10;
count[k]++;
int te=count[k]-1;
temp[k][te]=date[j];
}
int b=0;
for(int h=0;h<10;h++){
if(count[h]>0)
{
for(int v=0;v<count[h];v++)
{
date[b]=temp[h][v];
b++;
}
}
}
num=num*10;
}
cout<<"The result of basesort is here:"<<endl;
output(date,n);
cout<<endl;
}
int main()
{
InsertSort(lis1,n);
turnlisbace();
InsertSort_twodiv(lis1,n);
turnlisbace();
SelectSort(lis1,n);
turnlisbace();
ShellSort(lis1,n);
turnlisbace();
BubbleSort(lis1,n);
turnlisbace();
QuickSort(lis1,0,n-1);
cout<<"The result of quicksort is here:"<<endl;
output(lis1,n);
cout<<endl;
turnlisbace();
MergeSort(lis1,0,n-1);
cout<<"The result of mergesort is here:"<<endl;
output(lis1,n);
cout<<endl;
turnlisbace();
BaseSort(lis1,n);
return 0;
}