Sorting Names Alphabetically!! Help!!!

Mar 8, 2012 at 3:55pm
Hi!
I'm writing a program in C++ where the user is asked to enter a list of ten names and cities of residence in the format <firstname> <Surname> <city>.

I then have to sort the list by city and then in each city group alphabetically by name, using Bubblesort.

I'm able to extract each of the three components from the strings, but now I'm really lost on how to implement the Bubblesort correctly. All of the notes I have seen are for sorting numbers, not letters. Can anyone help me or offer suggestions as to how to sort the names?

This is what I have so far:

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

int main()
{
char name[10][80];
char buffer1 [80];
char buffer2 [80];
char buffer3 [80];
int i,j,k,q,word_size,start_pos,end_pos,name_size,city_start,city_end,city_length;


cout<<"Enter 10 names and cities of residence: "<<"\n";
gets( name[0]);
gets( name[1]);
gets( name[2]);
gets( name[3]);
gets( name[4]);
gets( name[5]);
gets( name[6]);
gets( name[7]);
gets( name[8]);
gets( name[9]);

cout<<"***Initialise check***"<<"\n";


cout<<"\n";
cout<<"\n";
cout<< name[0]<< "\n";
cout<< name[1]<< "\n";
cout<< name[2]<< "\n";
cout<< name[3]<< "\n";
cout<< name[4]<< "\n";
cout<< name[5]<< "\n";
cout<< name[6]<< "\n";
cout<< name[7]<< "\n";
cout<< name[8]<< "\n";
cout<< name[9]<< "\n"<<"\n";

cout<< "***Check Complete***"<<"\n";

for (i=0; i<=9; i++)
{ j=0;

while (isspace (name[i][j]) )

{j=j+1;

}


k=j;
while(!isspace(name[i][k])) k=k+1; //advances through the first name
name_size = k-j;

for (q=0; q<=name_size; q++)
{
// this generates the name
buffer1[q] = name[i][j];
j++;

}

buffer1[j]='\0';//this is the null char to terminate first name name
cout<<"Name "<<i+1<<": "<<buffer1<<"\n";

if (j<80){
while(isspace(name[i][k])) k=k+1; // advances through the next block of spaces
start_pos=k;
while(!isspace(name[i][k])) k=k+1;// advances through the surname
end_pos=k;

word_size = end_pos - start_pos;
}

// check word size of first char


for (q=0; q<=word_size; q++)
{
// this generates the surname
buffer2[q] = name[i][start_pos];
start_pos++;

}
//this is the null char to terminate surname name
buffer2[start_pos]='\0';
cout << "Surname "<<i+1<<": "<<buffer2 << "\n";


while(isspace(name[i][k])) k++;

city_start=k;
while(!isspace(name[i][k])) k++;

city_end=k;
city_length=city_end-city_start;


for (q=0; q<=city_length; q++)
{
// this generates the city
buffer3[q] = name[i][city_start];
city_start++;

}

//this is the null char to terminate surname name
buffer3[city_start]='\0';
cout<< "City " << i+1 <<": "<< buffer3 <<"\n";
cout<< "\n" << "\n";

}

return 0;
}
Mar 8, 2012 at 7:21pm
Hi

there are always more algorithm( solution) for a problem to be solved.
You may compare the asci code of each names, e.g. first compare the asci code of the first character, then second and so on, and put the name in corresponding order.

a very simple example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
	unsigned int n = 5;
	string s1[5] = { "roberto", "alex", "dia", "dido", "nicole" };

	for (unsigned int i = 0; i < n; i++) {
		cout << s1[i] << endl;
	}

	for (unsigned int i = 0; i < n; i++) {
		for (unsigned int j = 0; j < n; j++) {
			if (s1[i].at(0) < s1[j].at(0)) {
				string tmp = s1[i];
				s1[i] = s1[j];
				s1[j] = tmp;
			}
		}

	}
	cout<<endl;

	for (unsigned int i = 0; i < n; i++) {
		cout << s1[i] << endl;
	}



hope it helps
Last edited on Mar 8, 2012 at 7:44pm
Mar 8, 2012 at 7:40pm
there is a much simpler and easier way to do that wich actualy works every time in all cases: the string's included comparison operators!!! so simply:
1
2
3
4
5
6
for (unsigned int i = 0; i < n - 1; i++) {

		if (s1[i] > s1[ i + 1]) {
			swap (s1[i], s1[i+1]);
		}
}

PS: swap is in the <algorithm> header.
Mar 8, 2012 at 8:24pm
alternatively, you could also use strcmp on any two char array strings. It will return 0 if both are the same or [-1] or [1] depending on which is the bigger or smaller according to the ascii values of the characters within these strings.
Topic archived. No new replies allowed.