C++的问题:用冒泡排序法对10个字符串按照从小到大排序

来源:百度知道 编辑:UC知道 时间:2024/05/23 13:22:41
#include <iostream>
#include <string>
using namespace std;
void sort(char *pstr[]);
int main()
{
int i;
char *pstr[10],str[10][20];
for(i=0; i<10; i++)
pstr[i]=str[i];
for(i=0; i<10; i++)
cin>>pstr[i];
sort(pstr);
for(i=0; i<10; i++)
cout<<pstr[i];
return 0;
}
void sort(char *pstr[10])
{ int i,j;
char p;
for(i=0; i<10; i++)
{
for(j=i+1; j<10; j++)
{ if(strcmp(*(pstr+i),*(pstr+j))>0)
{ p=*(pstr+i);
*(pstr+i)=*(pstr+j);
*(pstr+j)=p;
}
}
}
}
哪位大虾帮忙改一下,谢谢了

字符串排序用的是字典法。

对于const char *str的排序,可以用strcmp来做啊。

冒泡么,最简单的了。楼主自己看着办。

题是不是错了啊 10个字符串怎么来排序啊

应该是10个数
#include <stdio.h>
void main()
{
int a[10];
int i,j,t;
printf("input 10 numbers :\n");
for (i=0;i<10;i++)
scanf("%d",&a[i]);
printf("\n");
for (j=0;j<9;j++)
for (i=0;i<9-j;i++)
if (a[i]<a[i+1])
{t=a[i];
a[i]=a[i+1];
a[i+1]=t;
}
printf("the sorted numbers:\n");
for (i=0;i<10;i++)
printf("%d",a[i]);
printf("\n");
}

这个问题没有错..回因为字符在排序的时候会按ASCII码来进行由小到大的排序...你看看是不是这样..反正我以前与到过这样的题目的..