算法分析与基础(作业)
#include <iostream>
using namespace std;
void Swap(char* a,char* b)
{
char temp;
temp=*a;
*a=*b;
*b=temp;
}
void selectionsort(char str[])
{
int i=0,j=0;
for(i=0;i<6;i++)
{
for(j=i+1;j<7;j++)
{
if(str[i]>str[j]){
Swap(&str[i],&str[j]);
}
}
}
}
void bubblesort(char str[])
{
int i,j;
for(i=0;i<6;i++)
{
for(j=0;j<6-i;j++)
{
if(str[j]>str[j+1])
{
Swap(&str[j],&str[j+1]);
}
}
}
}
void xianshi(char a[])
{
int i;
for(i=0;i<7;i++)
{
cout << a[i];
cout << ' ';
}
cout << endl;
}
int main()
{
char str[]={'E','X','A','M','P','L','E'};
char abc[]={'E','X','A','M','P','L','E'};
xianshi(str);
selectionsort(str);
xianshi(str);
xianshi(abc);
bubblesort(abc);
xianshi(abc);
return 0;
}