选择排序程序

来源:互联网 发布:怎样禁止用户安装软件 编辑:程序博客网 时间:2024/06/10 02:33

 #include<iostream.h>
#include<string.h>
#include<stdlib.h>

template <typename T>
void swap(T  &a,T  &b)
{
 a=(a)^(b);
 b=(a)^(b);
 a=(a)^(b);
}

template <typename T>
void sort(T a[],int n)
{
 int i,j;
 int k;
// T temp;
 for(i=0;i<n-1;i++)
 {
  k=i;
  for(j=i+1;j<n;j++)
  {
   if(a[j]<a[k])
    k=j;
  }
    //temp=a[k];
    //a[k]=a[i];
    //a[i]=temp;
  swap(a[k],a[i]);
 }
}


template <typename T>
void show(T a[],int n)                             ////输出排序后的数组
{
 int k=n;
 int i;
 for(i=0;i<k;i++)
 {
  cout<<dec<<a[i]<<' ';
 }
}


 
void main()
{
 int i=2;
 int j=5;
 cout<<i++ + ++j<<endl;
 cout<<"i="<<i<<' '<<"j="<<j<<endl;
 i>0?i++:j++;
 
 cout<<"i="<<i<<' '<<"j="<<j<<endl;
 i=i+j;
 j=i-j;
 i=i-j;
 cout<<"i="<<i<<endl;
 cout<<"j="<<j<<endl;
 i=i^j;
 j=i^j;
 i=i^j;
 cout<<"i="<<i<<endl;
 cout<<"j="<<j<<endl;
 int  temp;
 temp=i;
 i=j;
 j=temp;

 cout<<"i="<<i<<endl;
 cout<<"j="<<j<<endl;

///验证atoi()函数

 char str[10];
 cout<<"please input a numeric string:";
 cin>>str;
 cout<<str<<endl;
 cout<<atoi(str)<<endl;
 cout<<hex<<(int )str<<endl;
 cout<<(int )str<<endl;
 //cout<<()str<<endl;

//选择排序及输出

 int a[10]={12,45,65,25,48,74,1,32,};
 sort(a,8);
 show(a,8);

int d[10]={12.3,45.4,78.1,14.3,42.1,72.1,35.1,28.0};

sort(d,8);

show(d,8);


}

原创粉丝点击