//bubbel sort
#include <iostream>
using namespace std;
void BubbleSort(int a[],const int length)
{
bool exchange=false;
for(int i=0;i<length;i++)
{
for(int j=length-1;j>i;j--)
{
if(a[j]<a[j-1])
{
exchange=true;
int temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
}
if(exchange==false)
return;
}
}
}
int main(void)
{
int a[]={0,2,4,3,1,8,7,9,6,5};
int length=sizeof(a)/sizeof(int);
BubbleSort(a,length);
for(int k=0;k<length;k++)
cout<<a[k]<<endl;
return 0;
}
//quick sort
#include <iostream>
using namespace std;
void QuickSort(int a[], int s, int t)
{
if(s<t)
{
int i=s,j=t;
int temp=a[s];
while(i!=j)
{
while(j>i && a[j]>temp)
j--;
a[i]=a[j];
while(i<j && a[i]<temp)
i++;
a[j]=a[i];
}
a[i]=temp;
QuickSort(a,s,i-1);
QuickSort(a,i+1,t);
}
}
int main(void)
{
int a[]={0,2,4,3,1,8,7,9,6,5};
int length=sizeof(a)/sizeof(int);
QuickSort(a,0,length-1);
for(int k=0;k<length;k++)
cout<<a[k]<<endl;
return 0;
}
#include <iostream>
using namespace std;
void ShellSort(int *a,int n)
{
int d=n;
while(d>1)
{
d=(d+1)/2;
bool exchange=false;
do
{
for(int i=0;i<n-d;i++)
{
if(a[i+d]<a[i])
{
exchange=true;
int temp=a[i];
a[i]=a[i+d];
a[i+d]=temp;
}
}
exchange=false;
}while(exchange);
}
}
int main(void)
{
int a[]={0,2,4,3,1,8,7,9,6};
int length=sizeof(a)/sizeof(int);
ShellSort(a,length);
for(int k=0;k<length;k++)
cout<<a[k]<<endl;
return 0;
}
因篇幅问题不能全部显示,请点此查看更多更全内容