473,785 Members | 3,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Quicksort for list of string

can any one tell me if I give the followiing string in input:
ABC
abc
BBC

then how I can get
ABC
abc
BBC
or

abc
ABC
BBC
as my output usiing quicksort.

Apr 19 '07
12 4390
On Apr 19, 9:48 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
<aparnakakkar2. ..@gmail.comwro te in message

news:11******** **************@ n76g2000hsh.goo glegroups.com.. .
On Apr 19, 8:20 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
<aparnakakkar2. ..@gmail.comwro te in message
>news:11******* **************@ p77g2000hsh.goo glegroups.com.. .
can any one tell me if I give the followiing string in input:
ABC
abc
BBC
then how I can get
ABC
abc
BBC
or
abc
ABC
BBC
as my output usiing quicksort.
Well, this doesn't use quicksort but std::sort which is O( N log N ).
Output is:
Before sort:
ABC
abc
BBC
ABCD
aB
After sort:
ABC
ABCD
BBC
aB
abc
After Case Insensitive sort:
aB
ABC
abc
ABCD
BBC
There may be a better/faster algorithm for the case insensitve
comparing
of
std::strings. I just did this one rather quickly.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cctype>
bool CILessthan( const std::string Elem1, const std::string Elem2 )
{
for ( std::size_t i = 0; i < Elem1.length(); ++i )
{
if ( std::tolower( Elem1[i] ) < std::tolower( Elem2[i] ) )
return true;
else if ( std::tolower( Elem1[i] ) std::tolower( Elem2[i] ) )
return false;
}
// Equal up to this point, but one may be longer
if ( Elem1.length() < Elem2.length() )
return true;
return false;
}
void ShowData( const std::vector<std ::string>& Data )
{
for ( std::vector<std ::string>::cons t_iterator it = Data.begin();
it
!=
Data.end(); ++it )
std::cout << (*it) << "\n";
}
int main()
{
std::vector<std ::stringData;
Data.push_back( "ABC" );
Data.push_back( "abc" );
Data.push_back( "BBC" );
Data.push_back( "ABCD" );
Data.push_back( "aB" );
std::cout << "Before sort:\n";
ShowData( Data );
std::sort( Data.begin(), Data.end() );
std::cout << "\nAfter sort:\n";
ShowData( Data );
std::sort( Data.begin(), Data.end(), CILessthan );
std::cout << "\nAfter Case Insensitive sort:\n";
ShowData( Data );
std::string wait;
std::getline( std::cin, wait );
}- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -
please tell me how is it possible with (std::list<std: :stringdata)
as an argument

Just replace
std::sort( Data.begin(), Data.end(); CILessthan );
with
Data.sort( CILessthan );

As long as it's a list and not a vector.- Hide quoted text -
Its not working I want Data to be of type " std::list<std:: string>
Data;"

--------

Please don't send questions to my e-mail, post them here.

In this program if you replace every occurance of "vector" with "list",
change
std::sort( Data.begin(), Data.end(); CILessthan );
with
Data.sort( CILessthan );

and compile it should run fine.
Apr 20 '07 #11
On Apr 20, 8:37 am, "Jim Langston" <tazmas...@rock etmail.comwrote :
On Apr 19, 9:48 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :


<aparnakakkar2. ..@gmail.comwro te in message
news:11******** **************@ n76g2000hsh.goo glegroups.com.. .
On Apr 19, 8:20 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
><aparnakakkar2 ...@gmail.comwr ote in message
>>news:11****** *************** @p77g2000hsh.go oglegroups.com. ..
can any one tell me if I give the followiing string in input:
ABC
abc
BBC
then how I can get
ABC
abc
BBC
or
abc
ABC
BBC
as my output usiing quicksort.
>Well, this doesn't use quicksort but std::sort which is O( N log N ).
>Output is:
>Before sort:
>ABC
>abc
>BBC
>ABCD
>aB
>After sort:
>ABC
>ABCD
>BBC
>aB
>abc
>After Case Insensitive sort:
>aB
>ABC
>abc
>ABCD
>BBC
>There may be a better/faster algorithm for the case insensitve
>comparing
>of
>std::strings . I just did this one rather quickly.
>#include <iostream>
>#include <string>
>#include <vector>
>#include <algorithm>
>#include <cctype>
>bool CILessthan( const std::string Elem1, const std::string Elem2 )
>{
> for ( std::size_t i = 0; i < Elem1.length(); ++i )
> {
> if ( std::tolower( Elem1[i] ) < std::tolower( Elem2[i] ) )
> return true;
> else if ( std::tolower( Elem1[i] ) std::tolower( Elem2[i] ) )
> return false;
> }
> // Equal up to this point, but one may be longer
> if ( Elem1.length() < Elem2.length() )
> return true;
> return false;
>}
>void ShowData( const std::vector<std ::string>& Data )
>{
> for ( std::vector<std ::string>::cons t_iterator it = Data.begin();
>it
>!=
>Data.end(); ++it )
> std::cout << (*it) << "\n";
>}
>int main()
>{
> std::vector<std ::stringData;
> Data.push_back( "ABC" );
> Data.push_back( "abc" );
> Data.push_back( "BBC" );
> Data.push_back( "ABCD" );
> Data.push_back( "aB" );
> std::cout << "Before sort:\n";
> ShowData( Data );
> std::sort( Data.begin(), Data.end() );
> std::cout << "\nAfter sort:\n";
> ShowData( Data );
> std::sort( Data.begin(), Data.end(), CILessthan );
> std::cout << "\nAfter Case Insensitive sort:\n";
> ShowData( Data );
> std::string wait;
> std::getline( std::cin, wait );
>}- Hide quoted text -
>- Show quoted text -- Hide quoted text -
>- Show quoted text -
please tell me how is it possible with (std::list<std: :stringdata)
as an argument
Just replace
std::sort( Data.begin(), Data.end(); CILessthan );
with
Data.sort( CILessthan );
As long as it's a list and not a vector.- Hide quoted text -

Its not working I want Data to be of type " std::list<std:: string>
Data;"

--------

Please don't send questions to my e-mail, post them here.

In this program if you replace every occurance of "vector" with "list",
change
std::sort( Data.begin(), Data.end(); CILessthan );
with
Data.sort( CILessthan );

and compile it should run fine.- Hide quoted text -

- Show quoted text -
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cctype>

#include <list>
#include <iostream>
using namespace std ;
typedef list<stringVR_S TRINGList;
bool CILessthan( std::list<std:: string>::iterat or Elem1,
std::list<std:: string>::iterat or Elem2 )
{
//std::list<std:: string::iterato r i;
int i;
int arrayLength = Elem1->length();
for ( i = 0; i < arrayLength; ++i )
{
if ( strcmp(Elem1->c_str(),Elem 2->c_str())<=0 )
return true;
else if ( strcmp(Elem1->c_str(), Elem2->c_str())>0)
return false;
}
// Equal up to this point, but one may be longer
if ( Elem1->length() < Elem2->length())
return true;
return false;

}

void ShowData( const std::list<std:: string>& Data )
{
for ( std::list<std:: string>::const_ iterator it = Data.begin();
it != Data.end(); ++it )
std::cout << (*it) << "\n";
}

int main()
{
std::list<std:: stringData;
Data.push_back( "ABC" );
Data.push_back( "abc" );
Data.push_back( "BBC" );
std::cout << "Before sort:\n";
ShowData( Data );
// std::sort( Data.begin(), Data.end() );
std::cout << "\nAfter sort:\n";
ShowData( Data );

// std::sort( Data.begin(), Data.end(), CILessthan );
Data.sort(CILes sthan);
std::cout << "\nAfter Case Insensitive sort:\n";
ShowData( Data );
std::string wait;
std::getline( std::cin, wait );
}
I have done changes above for list but still getting error

Apr 20 '07 #12
On Apr 20, 8:37 am, "Jim Langston" <tazmas...@rock etmail.comwrote :
On Apr 19, 9:48 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :


<aparnakakkar2. ..@gmail.comwro te in message
news:11******** **************@ n76g2000hsh.goo glegroups.com.. .
On Apr 19, 8:20 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
><aparnakakkar2 ...@gmail.comwr ote in message
>>news:11****** *************** @p77g2000hsh.go oglegroups.com. ..
can any one tell me if I give the followiing string in input:
ABC
abc
BBC
then how I can get
ABC
abc
BBC
or
abc
ABC
BBC
as my output usiing quicksort.
>Well, this doesn't use quicksort but std::sort which is O( N log N ).
>Output is:
>Before sort:
>ABC
>abc
>BBC
>ABCD
>aB
>After sort:
>ABC
>ABCD
>BBC
>aB
>abc
>After Case Insensitive sort:
>aB
>ABC
>abc
>ABCD
>BBC
>There may be a better/faster algorithm for the case insensitve
>comparing
>of
>std::strings . I just did this one rather quickly.
>#include <iostream>
>#include <string>
>#include <vector>
>#include <algorithm>
>#include <cctype>
>bool CILessthan( const std::string Elem1, const std::string Elem2 )
>{
> for ( std::size_t i = 0; i < Elem1.length(); ++i )
> {
> if ( std::tolower( Elem1[i] ) < std::tolower( Elem2[i] ) )
> return true;
> else if ( std::tolower( Elem1[i] ) std::tolower( Elem2[i] ) )
> return false;
> }
> // Equal up to this point, but one may be longer
> if ( Elem1.length() < Elem2.length() )
> return true;
> return false;
>}
>void ShowData( const std::vector<std ::string>& Data )
>{
> for ( std::vector<std ::string>::cons t_iterator it = Data.begin();
>it
>!=
>Data.end(); ++it )
> std::cout << (*it) << "\n";
>}
>int main()
>{
> std::vector<std ::stringData;
> Data.push_back( "ABC" );
> Data.push_back( "abc" );
> Data.push_back( "BBC" );
> Data.push_back( "ABCD" );
> Data.push_back( "aB" );
> std::cout << "Before sort:\n";
> ShowData( Data );
> std::sort( Data.begin(), Data.end() );
> std::cout << "\nAfter sort:\n";
> ShowData( Data );
> std::sort( Data.begin(), Data.end(), CILessthan );
> std::cout << "\nAfter Case Insensitive sort:\n";
> ShowData( Data );
> std::string wait;
> std::getline( std::cin, wait );
>}- Hide quoted text -
>- Show quoted text -- Hide quoted text -
>- Show quoted text -
please tell me how is it possible with (std::list<std: :stringdata)
as an argument
Just replace
std::sort( Data.begin(), Data.end(); CILessthan );
with
Data.sort( CILessthan );
As long as it's a list and not a vector.- Hide quoted text -

Its not working I want Data to be of type " std::list<std:: string>
Data;"

--------

Please don't send questions to my e-mail, post them here.

In this program if you replace every occurance of "vector" with "list",
change
std::sort( Data.begin(), Data.end(); CILessthan );
with
Data.sort( CILessthan );

and compile it should run fine.- Hide quoted text -

- Show quoted text -
Thanks,its working fine.

Apr 20 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5233
by: why | last post by:
Hi, just been curious. i have learn that quicksorting algorithm is more widely used then heapsort, despite having the same performance indication of O(n log n) anyone knows why? newbie
2
2945
by: nkharan | last post by:
Hey, Here is the pseudocode of quicksort. It can be observed that there are as many as (n-1) unresolved stack calls in the worst case. Now, how do i reduce this unresolved stack calls? what modifications should I make? procedure QuickSort(L, low, high) recursive Input: L (a list of size n), low, high (integers) Output: L is sorted if high > low then
6
944
by: Baltazar007 | last post by:
Does anyone know how to make quicksort for single linked list without using array? I know that mergesort is maybe better but I need quicksort!! thnx
8
640
by: aparnakakkar2003 | last post by:
hello can any one tell me how i can create program to sort string list(standard template library) using quicksort.
8
6036
by: aparnakakkar2003 | last post by:
hello can any one tell me how i can create program to sort string list(standard template library) using quicksort.
3
3385
by: jollyfolly | last post by:
Could you please help me find the error. I myself have (i might be wrong) boiled it down to the for loop because it somehow magically converts a list into an int and tries to iterate over that. But I don't know how to fix it, doesn't even make sense why it would do it. Please help me out. Thanks in advance! Here is the error it gives me: TypeError: 'int' object is not iterable Here is my code: import random
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10319
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10087
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9947
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7496
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.