What codes should i use if i want it in alphabetical order | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What codes should i use if i want it in alphabetical order

25th Nov 2016, 7:00 AM
Nielea Valerie
Nielea Valerie - avatar
15 Answers
+ 3
You mean arranged characters of a word in alphabetical order, right?
25th Nov 2016, 7:17 AM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 3
I have use simple bubble sort technique. But If you want I can create a ever faster program with a good time complexity.
25th Nov 2016, 7:34 AM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 3
You welcome
29th Nov 2016, 6:18 AM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 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; }
25th Nov 2016, 7:33 AM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 2
Hey Nielea, is that you want. right?
25th Nov 2016, 7:59 AM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 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😀
25th Nov 2016, 8:02 AM
Megatron
Megatron - avatar
+ 2
In what? can you explain it
25th Nov 2016, 8:22 AM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 2
Sort them using first character of their name or something else
25th Nov 2016, 8:28 AM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 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; }
26th Nov 2016, 1:25 PM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 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.
25th Nov 2016, 8:16 AM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 1
hey aditya!! thankyou so much sorry for very late replyyyyy
29th Nov 2016, 5:00 AM
Nielea Valerie
Nielea Valerie - avatar
0
the words are given and i should transform it in alphabetical order
25th Nov 2016, 7:14 AM
Nielea Valerie
Nielea Valerie - avatar
0
yessss
25th Nov 2016, 7:17 AM
Nielea Valerie
Nielea Valerie - avatar
0
anything that could sort the given words to order well if that could help then could you help me?
25th Nov 2016, 8:20 AM
Nielea Valerie
Nielea Valerie - avatar
0
my prof told us to show 20 names with their corresponding grades then after that sort it in Alphabetical order
25th Nov 2016, 8:24 AM
Nielea Valerie
Nielea Valerie - avatar