真爱无限的知识驿站

学习积累技术经验,提升自身能力

linux下练习 c++ 几种排序算法

code:

#include <iostream>
#include<algorithm>
#include<ctime>
using namespace std;
void sort(int* a,int n)//普通冒泡排序
{
bool changed;
do
{
changed=false;
for(int i=1;i<n;i++)
{
if(a[i]<a[i-1])
{
swap(a[i],a[i-1]);
changed=true;
}
}
--n;
}
while(changed);
}
void sort1(int* a,int n)//插入排序
{
int j;
for(int i=1;i<n;i++)
{
int t=a[i];
for(j=i;j>0&&t<a[j-1];j--)
a[j]=a[j-1];
a[j]=t;
}
}
void sort2(int* a,int n)//高效冒泡排序
{
int j;
for(int i=0;i<n-1;i++)
{
int t=i;
for(j=i+1;j<n;j++)
if(a[j]<a[t]) t=j;
if(i!=t) swap(a[t],a[i]);
}
}
void sort3(int* a,int n)//快速(分组)排序
{
if(n<=1) return;
else if(n==2)
{
if(a[0]<=a[1]) return;
else swap(a[0],a[1]);
}
else
{
swap(a[n/2],a[0]);
int jie =a[0];
int* L=a+1;
int* R=a+n-1;
while(L<R)
{
while(L<R && *L<jie) ++L;
while(a<R && *R>=jie) --R;
if(L<R) swap(*L,*R);
}
if(*R <jie) swap(*R,a[0]);
sort(a,R-a);
sort(R+1,n-1-(R-a));
}
}
int main()
{
const int N=10240;
int a[N];
for(int i=0;i<N;i++)
{
a[i]=N-i;
}
clock_t t1=clock();
sort(a,N);
clock_t t2=clock();
cout<<"用时:"<<double(t2-t1)/CLOCKS_PER_SEC<<endl;
for(int i=21;i<31;i++)
{
cout<<a[i]<<' ';
}
cout<<endl;
//int b[5]={45,42,67,88,11};
for(int i=0;i<N;i++)//重新赋值
{
a[i]=N-i;
}
t1=clock();
sort1(a,N);
t2=clock();
cout<<"用时:"<<double(t2-t1)/CLOCKS_PER_SEC<<endl;
for(int i=21;i<31;i++)
{
cout<<a[i]<<' ';
}
cout<<endl;
for(int i=0;i<N;i++)//重新赋值
{
a[i]=N-i;
}
t1=clock();
sort2(a,N);
t2=clock();
cout<<"用时:"<<double(t2-t1)/CLOCKS_PER_SEC<<endl;
for(int i=21;i<31;i++)
{
cout<<a[i]<<' ';
}
cout<<endl;
for(int i=0;i<N;i++)//重新赋值
{
a[i]=N-i;
}
t1=clock();
sort3(a,N);
t2=clock();
cout<<"用时:"<<double(t2-t1)/CLOCKS_PER_SEC<<endl;
for(int i=21;i<31;i++)
{
cout<<a[i]<<' ';
}
cout<<endl;
}


linux下练习 c++ 二分查找

code:

#include<iostream>
using namespace std;
class person
{
string name;
int age;
public:
person(const char* n,int a):name(n),age(a){}
friend bool operator <(const person& a,const person& b)//运算符重载-比较年龄大小
{
return a.age<b.age;
}
friend bool operator >(const person& a,const person& b)//运算符重载-比较年龄大小
{
return a.age>b.age;
}
friend bool operator ==(const person& a,const person& b)//运算符重载-等于号
{
return a.age==b.age;
}
friend ostream& operator<<(ostream& o,const person& a)//运算符重载-输出
{
o<<a.name<<":"<<a.age<<endl;
}
};
person* bsearch(person* a,int n,const int age)//二分查找
{
if(n<=0) return NULL;
int mid=n/2;
person p("",age);
if(a[mid]==p) return a+mid;
if(p<a[mid]) return bsearch(a,mid,age);
else return bsearch(a+mid+1,n-mid-1,age);
}
person* bsearch2(person* a,int n,const int age)//二分查找
{
int b=0,e=n-1;
person t("",age);
while(b<=e)
{
int mid=(b+e)/2;
if(a[mid]==t) return a+mid;
if(t<a[mid]) e=mid-1;
else b=mid+1;
}
return NULL;
}
int main()
{
person a[5]={person("a1",34),
person("a2",25),
person("a3",16),
person("a4",77),
person("a5",40)};
for(int i=0;i<5;i++)//排序
{
for(int j=i+1;j<5;j++)
if(a[j]<a[i]) swap(a[j],a[i]);
}
for(int i=0;i<5;i++)
cout<<a[i];
int fage;
cout<<"请输入要查找的年龄:";
cin>>fage;
person* p=bsearch2(a,5,fage);//查找
if(p!=NULL) cout<<*p;
else cout<<"未找到!
";
}


<< 1 >>

Powered By Z-BlogPHP 1.7.3

Copyright 2024-2027 pukuimin Rights Reserved.
粤ICP备17100155号