473,385 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

What is the name of sorting algorithm used in list (STL) class?

Dear All,

What is the name of sorting algorithm used in list (STL) class? and how it
work?
Thanks!!!

Best Regards,
Wahoo
~ Let us linux ~
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 22 '05 #1
5 1820

"Wahoo" <wa***@yahoo.com> wrote in message news:40******@lungfunggdn.org...
Dear All,

What is the name of sorting algorithm used in list (STL) class? and how it
work?


It is sort.
E.g.
#include <list>
#include <iostream>
using namespace std;

int main(){
list<int> l;
list<int>::const_iterator itr;
l.push_back(5);
l.push_back(4);
l.push_back(17);
l.push_back(3);
l.sort();
for (itr = l.begin(); itr != l.end(); ++itr)
cout << *itr; //print 34517
}

-Sharad
Jul 22 '05 #2

"Sharad Kala" <no*****************@yahoo.com> schrieb im Newsbeitrag
news:2h************@uni-berlin.de...

"Wahoo" <wa***@yahoo.com> wrote in message

news:40******@lungfunggdn.org...
Dear All,

What is the name of sorting algorithm used in list (STL) class? and how it work?


It is sort.


Er, I think the question was: What does list<T>.sort() do?
I'm not sure if the method is defined in std C++, but only the result
is. To see what your implementation does open the file "list" from
your include directory and search for list().
--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
Jul 22 '05 #3
Wahoo wrote:
Dear All,

What is the name of sorting algorithm used in list (STL) class? and how it
work?
Thanks!!!

Best Regards,
Wahoo
~ Let us linux ~
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----


std::lists have a sort method, as demonstrated in another reply by Sharad.

In general you can sort any STL container with a random access iterator
using one of the algorithms listed below, declared in <algorithm>.

template<class RandomAccessIterator>
void sort(RandomAccessIterator first, RandomAccessIterator last);

template<class RandomAccessIterator, class Compare>
void sort(RandomAccessIterator first, RandomAccessIterator last,
Compare comp);

template<class RandomAccessIterator>
void stable_sort(RandomAccessIterator first, RandomAccessIterator last);

template<class RandomAccessIterator, class Compare>
void stable_sort(RandomAccessIterator first, RandomAccessIterator last,
Compare comp);

template<class RandomAccessIterator>
void partial_sort(RandomAccessIterator first,
RandomAccessIterator middle,
RandomAccessIterator last);

template<class RandomAccessIterator, class Compare>
void partial_sort(RandomAccessIterator first,
RandomAccessIterator middle,
RandomAccessIterator last,
Compare comp);

template<class InputIterator, class RandomAccessIterator>
RandomAccessIterator
partial_sort_copy(InputIterator first, InputIterator last,
RandomAccessIterator result_first,
RandomAccessIterator result_last);

template<class InputIterator, class RandomAccessIterator,
class Compare>
RandomAccessIterator
partial_sort_copy(InputIterator first, InputIterator last,
RandomAccessIterator result_first,
RandomAccessIterator result_last,
Compare comp);

As an example, consider the following:

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>

using namespace std ;

int main()
{
vector<unsigned> v ;
unsigned i ;

for (i = 32; i > 0; i--)
v.push_back(i) ;

sort<vector<unsigned>::iterator>(v.begin(), v.end()) ;

copy(v.begin(), v.end(), ostream_iterator<unsigned>(cout, " ")) ;
cout << endl ;

return 0 ;
}

Alan
Jul 22 '05 #4
Alan Johnson wrote:
Wahoo wrote:
Dear All,

What is the name of sorting algorithm used in list (STL) class?


sort<vector<unsigned>::iterator>(v.begin(), v.end()) ;


Actually, and I realized this just after posting, the compiler should be
able to infer the template type from the parameters (is this allowed by
the standard?), so this line could be changed to:

sort(v.begin(), v.end()) ;
Alan
Jul 22 '05 #5
On 19 May 2004 13:27:24 +0800, "Wahoo" <wa***@yahoo.com> wrote:
Dear All,

What is the name of sorting algorithm used in list (STL) class? and how it
work?


I think it's generally implemented as a mergesort, or some variant of
it. It is a divide and conquer algorithm, where to recursively sort
each half of the sequence and then merge the two sorted halves
together - it's a good algorithm for linked lists, since merge is
quite efficient for linked lists.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #6

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

Similar topics

9
by: Hunter Hou | last post by:
Folks, I am just curious why standard library doesn't provide a "merge" operation without sorting, because sometimes I don't require sorting, just merge. I looked at list.merge() and merge()...
11
by: velthuijsen | last post by:
I tried taking a list and pass it through std::sort like the following: sort(Unsorted.begin(), Unsorted.end()); I got an error back stating that the list iterator doesn't have a binary...
2
by: yee young han | last post by:
I need a fast data structure and algorithm like below condition. (1) this data structure contain only 10,000 data entry. (2) data structure's one entry is like below typedef struct _DataEntry_...
1
by: Prasad Karunakaran | last post by:
I am using the C# DirectoryEntry class to retrieve the Properties of an user object in the Active Directory. I need to get the First Name and Last Name as properties. I know it is not supported...
17
by: Allerdyce.John | last post by:
Hi, I am trying to compare the amount of work between using STL algorithm VS a plain Java loop. Let's say the class Rect has 2 attributes: area, and areaPerCent. In Java, I just write a...
4
by: _Raven | last post by:
Okay, I am playing with submitting forms with Ajax. I am trying to adapt this script to my forms: http://www.captain.at/howto-ajax-form-post-get.php I have included my code at the bottom of this...
3
by: Nelis Franken | last post by:
Good day. I'm having trouble telling STL to use a member function to sort items within the relevant class. The isLess() function needs to have access to the private members of the Foo class to...
6
by: Yin Zhu | last post by:
Hello all, I haved used STL in writing some arithmetic programs for a while now. There are some very convenient to use, such as map(use it as a mapping array), set(use it as a dictionary for...
4
by: slapsh0t11 | last post by:
Hello! I need help with a program that I believe I am nearly done with. However, there seems to be a few details that preclude me from success. Here is my assignment: Here is my class file...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.