JayKay
JayKay

Reputation: 183

How to alphabetically sort strings?

I have been trying to use this c++ program to sort 5 names alphabetically:

#include <iostream>
#include <cstring>
#include <conio.h>
using namespace std;

int main()
{
char names[5][100];
int x,y,z;

char exchange[100];

cout << "Enter five names...\n";

for(x=1;x<=5;x++)
{
    cout << x << ". ";
    cin >> names[x-1];
}
getch();

for(x=0;x<=5-2;x++)
{
    for(y=0;y<=5-2;y++)
    {
        for(z=0;z<=99;z++)
        {
            if(int(names[y][z])>int(names[y+1][z]))
            {   
                strcpy(exchange,names[y]);
                strcpy(names[y],names[y+1]);
                strcpy(names[y+1],exchange);
                break;
            }
        }   
    }
}   

for(x=0;x<=5-1;x++)
    cout << names[x];

return 0;
}

If I enter Earl, Don, Chris, Bill, and Andy respectively, I get this:

AndyEarlDonChrisBill

Could someone please tell me whats wrong with my program?

Upvotes: 0

Views: 147562

Answers (6)

user7968404
user7968404

Reputation: 101

You can use the sort function:

#include <algorithm>
#include <vector>
using namespace std;

...

vector<string> s;
sort(s.begin(),s.end());

Upvotes: 10

jingo francis
jingo francis

Reputation: 13

Putting this here in case someone needs a different solution.

/* sorting example */
#include <iostream>
using namespace std;

bool isSwap( string str1, string str2, int i)
{
    if(str1[i] > str2[i])
        return true;
    if(str1[i] == str2[i])
        return isSwap(str1,str2,i+1); 
    return false;
}

int main()
{   
    string str[7] = {"you","your","must","mike", "jack", "jesus","god"};
    int strlen = 7;
    string temp;
    int i = 0;
    int j = 0;
    bool changed = false;
    while(i < strlen-1)
    {
        changed = false;
        j = i+1;
        while(j < strlen)
        {
            if(isSwap(str[i],str[j],0))
            {
                temp = str[i];
                str[i] = str[j];
                str[j] = temp;
                changed = true;
            }
            j++;
        }
        if(changed)
            i = 0;
        else
            i++;       
    }
    for(i = 0; i < strlen; i++)
        cout << str[i] << endl;
    return 0;
}

Upvotes: 0

shawon
shawon

Reputation: 1022

You are using too much unnecessary loops. Try this simple and efficient one. You need to just swap when a string is alphabetically latter than other string.

Input
5
Ashadullah
Shawon
Shakib
Aaaakash
Ideone

Output
Aaaakash
Ashadullah
Ideone
Shakib
Shawon


#include <bits/stdc++.h>
using namespace std;
int main()
{
    string s[200],x[200],ct,dt;
    int i,j,n;
    cin>>n;
    for(i=0;i<n;i++)
    {
        cin>>s[i];
    }

    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {

            if(s[i]>s[j])
            {

                ct=s[i];
                s[i]=s[j];
                s[j]=ct;

            }

        }

    }
    cout<<"Sorted Name in Dictionary Order"<<endl;
    for(i=0;i<n;i++)
    {
        cout<<s[i]<<endl;
    }
    return 0;


}

Upvotes: 1

AngelCastillo
AngelCastillo

Reputation: 2435

You could use std::set or std::multiset (if you will allow repeated items) of strings, and it will keep the items sorted automatically (you could even change the sorting criteria if you want).

#include <iostream>
#include <set>
#include <algorithm>

void print(const std::string& item)
{
    std::cout << item << std::endl;
}

int main()
{
    std::set<std::string> sortedItems;

    for(int i = 1; i <= 5; ++i)
    {
        std::string name;
        std::cout << i << ". ";
        std::cin >> name;

        sortedItems.insert(name);
    }

    std::for_each(sortedItems.begin(), sortedItems.end(), &print);
    return 0;
}

input:

  1. Gerardo
  2. Carlos
  3. Kamilo
  4. Angel
  5. Bosco

output:

Angel
Bosco
Carlos
Gerardo
Kamilo

Upvotes: 10

Testing
Testing

Reputation: 76

The code does not take care when the names are already in order. Add the following

else if(int(names[y][z])<int(names[y+1][z]))
            break;  

To the if statement.

Upvotes: 0

caskey
caskey

Reputation: 12695

Your code implements a single-pass of bubble sort. Essentially missing the 'repeat until no changes are made to the array' loop around the outside.

Upvotes: 0

Related Questions