冒泡,选择,堆排序,插入排序,快速排序都可以啊
void maopao(char a[],int n)//冒泡法
{
char c;
for(int i=0;i<n;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1]){c=a[j];a[j]=a[j+1];a[j+1]=c;}
}
cout<<"第 "<<i+1<<" 次外层排序:"<<a<<endl;
}
cout<<"最终结果为:"<<a<<endl<<endl;
}
void xuanze(char a[],int n)//选择法
{
char c;
for( int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
{
if(a[i]>a[j]){c=a[i];a[i]=a[j];a[j]=c;}
}
cout<<"第 "<<i+1<<" 次外层排序:"<<a<<endl;
}
cout<<"最终结果为:"<<a<<endl<<endl;
}
void charu(char a[],int n)//插入法
{
char c;
for(int i=1;i<n;i++)
{
c=a[i];
int j=i-1;
while(j>=0&&c<a[j])
{
a[j+1]=a[j];
j--;
}
a[j+1]=c;
cout<<"第 "<<i<<" 次外层排序:"<<a<<endl;
}
cout<<"最终结果为:"<<a<<endl<<endl;
}
以上为我自已编写的,n为数组的长度
有不对的地方望帮我指正经,呵呵
#define N 10
#include "stdio.h"
void main()
{
int a[],j,j,x;
cout<<"input numbers of the array"<<endl;
for(i=0;i<N;++i) cin>>a[i];
cout<<"please check the input numbers"<<eddl;
for(i=0;i<N;++i)
cout<<a<<endl;
//the double loop as fllows
for(j=1;j<N;j++)
for(i=N-2;i>=0;i--)
if (a[i]>a[i+1])//sort
{x=a[i]; a[i]=a[i+1];a[i+1]=x;}
//now out put
for(i=0;i<N;++j)
cout<<a[i];
cout<<endl;
}
buguo wo juede zuihao haishi yong zhizhen lai gei shuzu paixu ,qingzhijiao.wo ye bu shi hen dong