0
What codes should i use if i want it in alphabetical order
15 Respostas
+ 3
You mean arranged characters of a word in alphabetical order, right?
+ 3
I have use simple bubble sort technique. But If you want I can create a ever faster program with a good time complexity.
+ 3
You welcome
+ 2
#include <iostream>
using namespace std;
int main() {
string s;
int i,z;
char g;
cout << "Please enter a word \n";
cin >> s;
cout << "You have entered " << s << endl;
z=0;
while(z!=s.size()-1)
{
i=0;
while(i!=s.size()-1)
{
if(s[i]>s[i+1])
{
g=s[i];
s[i]=s[i+1];
s[i+1]=g;
/*
You can use
s[i+1]=s[i]+s[i+1]-(s[i]=s[i+1]);
as Megatron said.
*/
}
i++;
}
z++;
}
cout << "Your alphabetical ordered word is " << s << endl;
return 0;
}
+ 2
Hey Nielea, is that you want. right?
+ 2
yes Aditya you can surely easily speed up this program by just doing one thing look at my code for swapping variable.
Like it also if it's really easy greatđ
+ 2
In what? can you explain it
+ 2
Sort them using first character of their name or something else
+ 2
Here is what you want, just replace n with 20 and remove cout and cin in
#include <iostream>
using namespace std;
int main() {
int i,j,n;
string c[1][2];
//Delete From here
cout << "Please enter number of student\n";
cin >> n;
//To here and set value of n=20;
string s[n][2];
cout << "Please enter name and grade of student (Please give a space between them)\n";
for(i=0;i<n;i++)
{
cout << "\nstudent number " << i+1 << endl;
cout << "\nName: ";
cin >> s[i][0];
cout << "\tGrade: ";
cin >> s[i][1];
}
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(s[j][0]>s[j+1][0])
{
c[0][0] = s[j][0];
s[j][0] = s[j+1][0];
s[j+1][0] = c[0][0];
c[0][1] = s[j][1];
s[j][1] = s[j+1][1];
s[j+1][1] = c[0][1];
// s[j+1][0]=s[j][0]+s[j+1][0]-(s[j][0]=s[j+1][0]);
// s[j+1][1]=s[j][1]+s[j+1][1]-(s[j][1]=s[j+1][1]);
}
}
}
cout << "\nYour student list in alphabetic order\n";
for(i=0;i<n;i++)
{
cout << "\nStudent number " << i+1 << endl;
cout << "Name: " << s[i][0] << "\tGrade: " << s[i][1] << endl;
}
return 0;
}
+ 1
Well, Megatron thanks for giving me that information. Yes your logic is superb. But here I am taking about technique such as quick sort, merge sort. Which have time complexity equal to O(nlogn). Anyway, thanks again. I update my answer.
+ 1
hey aditya!! thankyou so much sorry for very late replyyyyy
0
the words are given and i should transform it in alphabetical order
0
yessss
0
anything that could sort the given words to order well if that could help then could you help me?
0
my prof told us to show 20 names with their corresponding grades then after that sort it in Alphabetical order