472,354 Members | 1,357 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

STL Sort

Is the STL sort() algorithm implemented genericly over all the
containers or once for each container? And in which file is it implemented?
Jul 22 '05 #1
5 7038
Li even De Keyzer wrote:
Is the STL sort() algorithm implemented genericly over all the
containers or once for each container? And in which file is it implemented?


It's implemented generically for the containers with random access
iterators (C++ arrays fall under that category). std::list has its
own sort (since its iterators aren't random-access).

It's declared in <algorithm>.

V
Jul 22 '05 #2
Lieven De Keyzer wrote:
Is the STL sort() algorithm implemented genericly over all the
containers or once for each container? And in which file is it implemented?


It's defined in <algorithm>. The file in which the actual implementation
lives is library specific, but it will be included by algorithm, and is
very frequently called stl_algo.h, in a directory called "bits" (you of
course can't assume that).

It is implemented generically over two random-access iterators. The
definition of a random access iterator can be looked up in the standard,
but in essence it generalises the idea of a pointer, so you can assume
you can do things like * to derefence, ++ and -- to go back and forth,
and add and subtract constants to go back and forth in larger steps,
assuming of course you don't leave the range [start,end], and you only
use * on either start or items between start and end (not end of course).

Any container whose iterators are random-access can therefore have
exactly the same sort algorithm applied to them.

Chris
Jul 22 '05 #3
Lieven De Keyzer wrote:

Is the STL sort() algorithm implemented genericly over all the
containers or once for each container? And in which file is it implemented?


AFAIK it is a generic sort algorithm with the exception of std::list. std::list
has its own sort implemented.

#include <algorithm>

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #4
chris wrote:
Lieven De Keyzer wrote:
Is the STL sort() algorithm implemented genericly over all the
containers or once for each container? And in which file is it
implemented?


It's defined in <algorithm>. The file in which the actual implementation
lives is library specific, but it will be included by algorithm, and is
very frequently called stl_algo.h, in a directory called "bits" (you of
course can't assume that).

It is implemented generically over two random-access iterators. The
definition of a random access iterator can be looked up in the standard,
but in essence it generalises the idea of a pointer, so you can assume
you can do things like * to derefence, ++ and -- to go back and forth,
and add and subtract constants to go back and forth in larger steps,
assuming of course you don't leave the range [start,end], and you only
use * on either start or items between start and end (not end of course).

Any container whose iterators are random-access can therefore have
exactly the same sort algorithm applied to them.

Chris


The thing is, we have to parallelize an sort algorithm and meet the speedup
against the normal algorithm. I chose Quicksort. One of the requirements
for this, is that it is generic over STL containers. So I wrote my own
Quicksort template, working with random-access iterators but also with
bi-directional, for the list. What I want to know is: is there a way I can
also sort the associative containers with adjustments to my algorithm. Here
is the code:

//quicksort
template<typename ContainerIterator>
void quicksort(ContainerIterator begin, ContainerIterator end){
if (begin != end){

ContainerIterator newPivot(partition(begin, end));
quicksort(begin, newPivot);
quicksort(++newPivot, end);
}
}
//partition function
template<typename ContainerIterator>
ContainerIterator partition(ContainerIterator begin, ContainerIterator end){

typedef typename std::iterator_traits<ContainerIterator>::value_typ e
ValueType;
ValueType endValue(*((--end)++));
ContainerIterator front((--begin)++);
for(ContainerIterator it(begin); (it != (--end)++); it++)
if (*it <= endValue){

++front;
std::iter_swap(front, it);
}
std::iter_swap(++front,(--end)++);
return front;
}

Jul 22 '05 #5

The thing is, we have to parallelize an sort algorithm and meet the speedup against the normal algorithm. I chose Quicksort. One of the requirements
for this, is that it is generic over STL containers. So I wrote my own
Quicksort template, working with random-access iterators but also with
bi-directional, for the list. What I want to know is: is there a way I can
also sort the associative containers with adjustments to my algorithm. Here is the code:


Associative containers are already sorted, you cannot change the order.

john
Jul 22 '05 #6

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

Similar topics

2
by: Thomas Philips | last post by:
I recently had the need to sort a large number of lists of lists, and wondered if an improvement to the Decorate-Sort-Undecorate idiom is in the works. Ideally, I would like to sort the list of...
1
by: Kamilche | last post by:
I've written a generic sort routine that will sort dictionaries, lists, or tuples, either by a specified key or by value. Comments welcome! import types def sort(container, key = None,...
4
by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
40
by: Elijah Bailey | last post by:
I want to sort a set of records using STL's sort() function, but dont see an easy way to do it. I have a char *data; which has size mn bytes where m is size of the record and n is the...
7
by: Stuart | last post by:
The stl::sort() that comes with Dev Studio 6 is broken (it hits the degenerate case in a common situation). I have a replacement. I would like to globally do "using namespace std; except use my...
7
by: DC Gringo | last post by:
I have a datagrid that won't sort. The event handler is firing and return label text, just not the sort. Here's my Sub Page_Load and Sub DataGrid1_SortCommand: -------------------- Private...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
10
by: Woody Ling | last post by:
In 32 bits DB2 environment, is it meaningful to set sheapthres larger than 256MB for the following case.. 1. Intra-parallel is ON 2. Intra-parallel is OFF
5
by: neehakale | last post by:
I know that heap sort,quick sort and merg sort are the faster sorting algoritms than the bubble sort,selection sort,shell sort and selection sort. I got an idea,in which situation we can use...
5
by: neocortex | last post by:
Hello! I am a newbie in Python. Recently, I get stuck with the problem of sorting by two criteria. In brief, I have a two-dimensional list (for a table or a matrix). Now, I need to sort by two...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...

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.