Frage

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?

War es hilfreich?

Lösung

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

Andere Tipps

You can use the sort function:

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

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;


}

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.

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.

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;
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top