冒泡
#include<iostream>
using namespace std;
const list_size=10;
const listincreament=10;
typedef int datatype;
typedef struct
{
datatype* a; // 为了可以扩张存储单元,设置成这个指针变量,指向地址的。
int length;
int listsize;
int incrementsize;
}sqlist;
void InitList(sqlist &l,int maxsize=list_size,int increasize=listincreament)
{
l.a=new datatype[maxsize];
l.length =0;
l.listsize=maxsize;
l.incrementsize =increasize;
}
void increament(sqlist&l )
{
datatype *a;
a=new datatype[l.listsize+l.incrementsize];
for(int i=0;i<l.listsize;i++)
{
a[i]=l.a[i];
}
l.a=a;
delete []l.a;
l.listsize+=l.incrementsize;
}
void listinsert(sqlist & l,int i,datatype e)
{
//if(l.length>=l.listsize)
// increament(l);
for(int j=l.length-1;j>=i;j--)
{
l.a[j+1]=l.a[j] ; // *(l.a+j+1)=*(l.a+j);
}
*(l.a+i)=e;
l.length ++;
}
void travel(sqlist l)
{
for(int i=0;i<l.length ;i++)
{
cout<<*(l.a+i)<<endl;
}
}
void BubbleSort(sqlist &l)
{
int i,w,j; j=0;
i=l.length-1;
while(i>0)
{
int lastexchangeindex=-1;
for(j=0;j<i;j++)
{
if(l.a[j+1]<l.a[j])
{
w=l.a[j+1];
l.a[j+1]=l.a[j];
l.a[j]=w;
lastexchangeindex=j;
}
}
i=lastexchangeindex;
}
}
int main()
{
sqlist la;int m;
InitList(la); datatype s;
cin>>m;
for(int i=0;i<m;i++)
{
cin>>s;
listinsert(la,i,s);
}
BubbleSort(la);
travel(la);cout<<endl;
return 0;
}
每一趟 比较后,将此趟最大的数 下沉到 此趟参与比较个数的序数最大的位置
当 某一趟 没有 发生交换 排序结束
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
插入
#include<iostream>
using namespace std;
const list_size=10;
const listincreament=10;
typedef int datatype;
typedef struct
{
datatype* a; // 为了可以扩张存储单元,设置成这个指针变量,指向地址的。
int length;
int listsize;
int incrementsize;
}sqlist;
void InitList(sqlist &l,int maxsize=list_size,int increasize=listincreament)
{
l.a=new datatype[maxsize];
l.length =0;
l.listsize=maxsize;
l.incrementsize =increasize;
}
void increament(sqlist&l )
{
datatype *a;
a=new datatype[l.listsize+l.incrementsize];
for(int i=0;i<l.listsize;i++)
{
a[i]=l.a[i];
}
l.a=a;
delete []l.a;
l.listsize+=l.incrementsize;
}
void listinsert(sqlist & l,int i,datatype e)
{
//if(l.length>=l.listsize)
// increament(l);
for(int j=l.length-1;j>=i;j--)
{
l.a[j+1]=l.a[j] ; // *(l.a+j+1)=*(l.a+j);
}
*(l.a+i)=e;
l.length ++;
}
void listinsert1(sqlist & l,int j,int i,datatype e)// 插入第j个位置,待插入的元素原来的序号为i;
{
//if(l.length>=l.listsize)
// increament(l);
for(int p=i-1;p>=j;p--)
{
l.a[p+1]=l.a[p] ;
}
*(l.a+j)=e;
}
void travel(sqlist l)
{
for(int i=0;i<l.length ;i++)
{
cout<<*(l.a+i)<<endl;
}
}
void InsertSort(sqlist &l)
{
int t;
for(int i=1;i<l.length;i++)
{
t=l.a[i];
for(int j=0;j<=i;j++)
{
if(t<l.a[j])
{
listinsert1(l,j,i,t) ;
break;}
}
}
}
int main()
{
sqlist la;int m;
InitList(la); datatype s;
cin>>m;
for(int i=0;i<m;i++)
{
cin>>s;
listinsert(la,i,s);
}
InsertSort(la);
travel(la);cout<<endl;
return 0;
}
// 注意插入的函数
------------------------------------------------
快速排序
#include<iostream>
using namespace std;
const list_size=10;
const listincreament=10;
typedef int datatype;
typedef struct
{
datatype* a; // 为了可以扩张存储单元,设置成这个指针变量,指向地址的。
int length;
int listsize;
int incrementsize;
}sqlist;
void InitList(sqlist &l,int maxsize=list_size,int increasize=listincreament)
{
l.a=new datatype[maxsize];
l.length =0;
l.listsize=maxsize;
l.incrementsize =increasize;
}
void increament(sqlist&l )
{
datatype *a;
a=new datatype[l.listsize+l.incrementsize];
for(int i=0;i<l.listsize;i++)
{
a[i]=l.a[i];
}
l.a=a;
delete []l.a;
l.listsize+=l.incrementsize;
}
void listinsert(sqlist & l,int i,datatype e)
{
//if(l.length>=l.listsize)
// increament(l);
for(int j=l.length-1;j>=i;j--)
{
l.a[j+1]=l.a[j] ; // *(l.a+j+1)=*(l.a+j);
}
*(l.a+i)=e;
l.length ++;
}
void travel(sqlist l)
{
for(int i=0;i<l.length ;i++)
{
cout<<*(l.a+i)<<endl;
}
}
int Partition(sqlist&l,int low,int high) // 一次划分函数
{
datatype t =l.a[low];
datatype pivotkey=l.a[low];
while(low<high)
{
while(low<high && l.a[high]>=pivotkey)
--high;
if(low<high)
l.a[low++]=l.a[high];
while(low<high && l.a[low]<=pivotkey)
++low;
if(low<high)
l.a[high--]=l.a[low];
}
l.a[low]=t;
return low;
}
void QSort(sqlist &l,int s,int t) // 递归的 划分
{
if(s<t)
{
int pivotloc =Partition(l,s,t);
QSort(l,s,pivotloc);
QSort(l,pivotloc+1,t);
}
}
int main()
{
sqlist la;int m;
InitList(la); datatype s;
cin>>m;
for(int i=0;i<m;i++)
{
cin>>s;
listinsert(la,i,s);
}
QSort(la,0,la.length-1);
travel(la);cout<<endl;
return 0;
}