本文包括了六大排序算法
插入排序、选择排序(插入类)
冒泡排序、快速排序(交换类)
选择排序、堆排序(选择类
函数分析:
rand():
使用c++标准库cstdlib中的rand(),用%100控制数据关键字的范围,生成随机数。
传参:
传值调用,不改变主函数数组中元素得到次序,每一次排序都是对相同的数据操作。
排序函数:
插入排序的特点是一趟排序下来位置是相对的,相对于相邻元素的大或者小,移动次数和比较次数差不多,因为相当于是把index项拿走留下一个空,减少移动次数
选择排序的特点是最后放置的位置是绝对的,冒泡排序一趟拿出最大的一个元素放在末尾,快速排序一趟选出分割位置的元素
性能分析:
总的来说,快速排序和堆排序是最优的。
当数组基本有序,(比如就一个乱序)插入排序是最优的,其次是冒泡排序。
当数组是有序的情况下,快速排序是最慢的,时间复杂度是n^2。
直接插入排序:正序是最好情况,完全逆序是最差情况,因为是相邻位置交换,所以是稳定的
冒泡排序:正序是最好情况,完全逆序是最差情况,因为是相邻位置交换,所以是稳定的
快速排序:最好情况是无序,正序或逆序都是最坏情况,因为是远距离交换,所以是不稳定的,由于是递归,空间复杂度也是不一定的。
选择排序:时间复杂度不受初始情况影响,是唯一一种不稳定的简单排序算法
堆排序:时间复杂度不受初始条件影响,同样是不稳定的,相较于快速排序,空间复杂度更小
#include <iostream>
#include <cstdlib>
#include <ctime>
#define MAXSIZE 100
using namespace std;
typedef int KeyType;
typedef struct{
KeyType key;
int otherInfo;
}RedType;//data element
typedef struct{
RedType r[MAXSIZE+1];
int length;
}SqList;//data list
int Create(SqList &L){
L.length=MAXSIZE;
//注意,声明数组的时候空间就已经分配好了,因为r虽然是一个指针,但同时也是一个变量名
for(int i=1;i<=MAXSIZE;i++){
L.r[i].key=rand()%100;
}
return 1;
}
int QcomCount=0;//times of comparing
int QshiftCount=0;//times of shifting
int HcomCount=0;//times of comparing
int HshiftCount=0;//times of shifting
//设置哨兵的排序方式:直接插入排序,希尔排序
void InsertSort(SqList L){
int comCount=0;//times of comparing
int shiftCount=0;//times of shifting
cout<<"This is the results of STRAIGHT INSERTION SORT:"<<endl;
for(int i=2;i<=L.length;i++){
if(L.r[i].key<L.r[i-1].key){//make judgment on whether to make blank
comCount++;
//set r[0] lest overflow,because the loop of while is bound to stop when j equls 0,the set can reduce comparation of"j>0"
L.r[0]=L.r[i];//the position of i is blank
shiftCount++;
int j;
for(j=i-1; L.r[0].key<L.r[j].key; j--){
comCount++;
L.r[j+1]=L.r[j];
shiftCount++;
}
L.r[j+1]=L.r[0];
}
}
//comCount+=2*L.length-2;//add the omitted times of comparations
for(int i=1;i<=L.length;i++){
cout<<L.r[i].key<<" ";
}
cout<<"\n"<<"Comparation time:"<<comCount<<endl;
cout<<"Shifting time:"<<shiftCount<<endl;
}
void ShellSort(SqList L){
int comCount=0;//times of comparing
int shiftCount=0;//times of shifting
int increment;
cout<<"This is the results of SHELL INSERTION SORT:"<<endl;
for(increment=L.length/2;increment>=1;increment/=2){
for(int i=increment+1;i<=L.length;i++){
if(L.r[i].key<L.r[i-increment].key){
comCount++;
L.r[0]=L.r[i];
shiftCount++;
int j;
for(j=i-increment;L.r[0].key<L.r[j].key&&j>=1;j-=increment){
comCount++;
L.r[j+increment]=L.r[j];
shiftCount++;
}
L.r[j+increment]=L.r[0];
shiftCount++;
}
}
}
for(int i=1;i<=L.length;i++){
cout<<L.r[i].key<<" ";
}
cout<<"\n"<<"Comparation time:"<<comCount<<endl;
cout<<"Shifting time:"<<shiftCount<<endl;
}
void BubbleSort(SqList L){
int comCount=0;//times of comparing
int shiftCount=0;//times of shifting
cout<<"This is the results of BUBBLE EXCHANGE SORT:"<<endl;
for(int i=1;i<=L.length-1;i++){
bool flag=true;//flag signing correct sort
for(int j=1;j<L.length-i;j++){
if(L.r[j].key>L.r[j+1].key){
comCount++;
shiftCount+=3;
flag=false;//implying incorrect sort
RedType t=L.r[j];
L.r[j]=L.r[j+1];
L.r[j+1]=t;
}
}
if(flag)
break;
}
for(int i=1;i<=L.length;i++){
cout<<L.r[i].key<<" ";
}
cout<<"\n"<<"Comparation time:"<<comCount<<endl;
cout<<"Shifting time:"<<shiftCount<<endl;
}
void QSort(SqList &L,int low, int high){
if(low>=high)
return;
int i=low,j=high;
RedType t=L.r[low];
while(i<j){
while(i<j&&L.r[j].key>=t.key){
QcomCount++;
j--;
}
if(i<j)
L.r[i]=L.r[j];
QshiftCount++;
while(i<j&&L.r[i].key<=t.key){
QcomCount++;
i++;
}
if(i<j)
L.r[j]=L.r[i];
QshiftCount++;
}
L.r[i]=t;
QSort(L,low,i-1);
QSort(L,i+1,high);
}
void SelectSort(SqList L){
int comCount=0;//times of comparing
int shiftCount=0;//times of shifting
cout<<"This is the results of SELECT SORT:"<<endl;
for(int i=1;i<=L.length;i++){
int k=i;
for(int j=i;j<=L.length;j++){
comCount++;
if(L.r[j].key<L.r[k].key){
k=j;
}
}
RedType t;
t=L.r[i];
L.r[i]=L.r[k];
L.r[k]=t;
shiftCount+=3;
}
for(int i=1;i<=L.length;i++){
cout<<L.r[i].key<<" ";
}
cout<<"\n"<<"Comparation time:"<<comCount<<endl;
cout<<"Shifting time:"<<shiftCount<<endl;
}
void Down(SqList &L,int index,int n){
RedType t=L.r[index];
while(2*index<=n){
int child=2*index;
if(child+1<=n&&L.r[child].key<L.r[child+1].key)child++;
if(L.r[child].key>t.key){
HcomCount++;
L.r[index]=L.r[child];
index=child;
HshiftCount++;
}
else
break;
}
L.r[index]=t;
}
void HeapSort(SqList L){
cout<<"This is the results of HEAP SORT SELECT SORT:"<<endl;
for(int i=L.length/2;i>=1;i--)
Down(L,i,L.length);
for(int i=L.length;i>=1;i--){
RedType t;
t=L.r[i];
L.r[i]=L.r[1];
L.r[1]=t;
HshiftCount+=3;
Down(L,1,i-1);
}
for(int i=1;i<=L.length;i++){
cout<<L.r[i].key<<" ";
}
cout<<"\n"<<"Comparation time:"<<HcomCount<<endl;
cout<<"Shifting time:"<<HshiftCount<<endl;
}
int main(){
//cout<<"Please input the count of array";
SqList L;
Create(L);
InsertSort(L);
ShellSort(L);
BubbleSort(L);
HeapSort(L);
SelectSort(L);
cout<<"This is the results of QUICK SORT EXCHANGE SORT:"<<endl;
QSort(L,1,MAXSIZE);
for(int i=1;i<=L.length;i++){
cout<<L.r[i].key<<" ";
}
cout<<"\n"<<"Comparation time:"<<QcomCount<<endl;
cout<<"Shifting time:"<<QshiftCount<<endl;
}