Home » C++ programs

C++ program to Sort Names in an Alphabetical Order

In this article, we are going to learn how to sort a list of names (lowercase) in alphabetical order (Dictionary order) using C++ program?
Submitted by Radib Kar, on December 12, 2018

Problem statement: Write a C++ program to sort N names in alphabetical order.

Example & explanation:

Let user inputs 5 names as following:

    rahul
    virat
    vijay
    bumrah
    rahane

Alphabetical sorting means to sort names according to alphabet order starting from the rightmost character. (Rightmost character->'r' for 'rahul').

That means, in the 'rahul' precedes 'vijay' in alphabetical order since rightmost character of 'rahul' is 'r' where as rightmost character of 'vijay' is 'v' & 'r' precedes 'v' in alphabetical order.

The next concern is what about 'rahul' & 'rahane' since both of them has same rightmost character. In such cases compare corresponding elements from right to left direction.

Thus 'rahane' precedes 'rahul' since 'a' precedes 'u' in alphabetical order. (Both have 'rah' part common).

One more thing is 'abdul' will precedes 'abdulla' as both have 'abdul' as common part & 'abdul' has nothing left where 'abdulla' has.

So using the above facts the names will appear after sorting:

    bumrah
    rahane
    rahul
    vijay
    virat

Algorithm:

1. Constructing list of names

Declare a vector of strings & take each string &insert to the vector.

    vector<string>names;
    for i=0:n-1
        input each name;
        insert name into the vector
    End for loop

2. Sorting in alphabetical order

We can sort the vector using our own comparator function to sort the strings in alphabetical order.

Our comparator function is defined as:

    Function[bool] mycomp (string a, string b)
        return a<b;
        //returns 1 if string a is alphabetically less than string b, 
        //which is quite similar to strcmp operation 
        //else returns 0
    End Function

Now to sort the vector of string we use this comparator function while using STL sort function.

    sort(names.begin(), names.end(), mycomp);
    //this sorts the names in alphabetical order

C++ implementation for Sorting Names in an Alphabetical Order

#include <bits/stdc++.h>
using namespace std;

//function to print the array
void print(vector<string> names){
	printf("printing ........\n");
	for(int i=0;i<names.size();i++)
		cout<<names[i]<<endl;
	printf("\n");
}

bool mycomp(string a, string b){
	//returns 1 if string a is alphabetically 
	//less than string b
	//quite similar to strcmp operation
	return a<b;
}

vector<string> alphabaticallySort(vector<string> a){
	int n=a.size();
	//mycomp function is the defined function which 
	//sorts the strings in alphabatical order
	sort(a.begin(),a.end(),mycomp);
	return a;
}

int main()
{   
	int n;
	printf("enter number of names to be added: ");
	scanf("%d",&n);

	//creating a vector of strings
	//vector to store strings(names)
	vector<string> names;
	string name;
	printf("enter names: \n");
	//taking input
	for(int i=0;i<n;i++){
		cin>>name;
		//insert names into the vector
		names.push_back(name); 
	}

	printf("\nbefore sorting\n");
	print(names);

	//function to sort names alphabetically
	names=alphabaticallySort(names);

	printf("after alphabetical sorting\n");
	print(names);

	return 0;
}

Output

enter number of names to be added: 5
enter names:
rahul
virat
vijay
bumrah
rahane

before sorting
printing ........
rahul
virat
vijay
bumrah
rahane

after alphabetical sorting
printing ........
bumrah
rahane
rahul
vijay
virat





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.