I want to write a generic quicksort over STL containers. We have to
parallelize this algorithm afterwards. This is what I've got for now:
template<typena me containerIterat or>
void quicksort(conta inerIterator begin, containerIterat or end){
Â*Â*Â*Â*Â*Â*Â*Â *ifÂ*(endÂ*>Â*b egin){
Â*Â*Â*Â*Â*Â*Â*Â *Â*Â*Â*Â*Â*Â*Â* Â*containerIter atorÂ*pivot(beg in);
Â*Â*Â*Â*Â*Â*Â*Â *Â*Â*Â*Â*Â*Â*Â* Â*containerIter atorÂ*newPivot( partition(begin ,Â*end,Â*pivot) );
Â*Â*Â*Â*Â*Â*Â*Â *Â*Â*Â*Â*Â*Â*Â* Â*quicksort(beg in,Â*--newPivot);
Â*Â*Â*Â*Â*Â*Â*Â *Â*Â*Â*Â*Â*Â*Â* Â*quicksort(++n ewPivot,Â*end);
Â*Â*Â*Â*Â*Â*Â*Â *}
}
template<typena me containerIterat or>
containerIterat or partition(conta inerIterator begin, containerIterat or end,
containerIterat or pivot){
Â*Â*Â*Â*Â*Â*Â*Â *//whatÂ*IÂ*wantÂ* hereÂ*isÂ*toÂ*s toreÂ*theÂ*valu eÂ*ofÂ*theÂ*piv ot,
Â*Â*Â*Â*Â*Â*Â*Â *//soÂ*somethingÂ* like:Â*
Â*Â*Â*Â*Â*Â*Â*Â *pivotValue(*pi vot);
}
Problem is I don't know the type of this value at this stage.
Should I include it in the templateparamet ers? 3 2407
"Lieven" <li************ *@hotmail.com> wrote in message
news:41******** **************@ news.skynet.be. .. pivotValue(*piv ot); }
Problem is I don't know the type of this value at this stage. Should I include it in the templateparamet ers?
No need. If T is an iterator type, then std::iterator_t raits<T>::value _type
is the type that is obtained by dereferencing T.
"Andrew Koenig" <ar*@acm.org> wrote in message
news:Vi******** *************@b gtnsc04-news.ops.worldn et.att.net... "Lieven" <li************ *@hotmail.com> wrote in message news:41******** **************@ news.skynet.be. ..
pivotValue(*piv ot); }
Problem is I don't know the type of this value at this stage. Should I include it in the templateparamet ers?
No need. If T is an iterator type, then std::iterator_t raits<T>::value _type is the type that is obtained by dereferencing T.
And 'iterator type' includes pointers.
john
Lieven wrote: I want to write a generic quicksort over STL containers. We have to parallelize this algorithm afterwards. This is what I've got for now:
template<typena me containerIterat or> void quicksort(conta inerIterator begin, containerIterat or end){
ifÂ*(endÂ*>Â*be gin){
containerIterat orÂ*pivot(begin ); containerIterat orÂ*newPivot(pa rtition(begin, *end,Â*pivot));
/*I realize I made a mistake here. When I do --newPivot, this is a
destructive operation. The iterator now points to the element in front of
the pivotelement. So the ++newPivot afterwards just points to the
pivotelement again.*/
quicksort(begin ,Â*--newPivot); quicksort(++new Pivot,Â*end); } }
template<typena me containerIterat or> containerIterat or partition(conta inerIterator begin, containerIterat or end, containerIterat or pivot){
//whatÂ*IÂ*wantÂ* hereÂ*isÂ*toÂ*s toreÂ*theÂ*valu eÂ*ofÂ*theÂ*piv ot, //soÂ*somethingÂ* like: pivotValue(*piv ot); }
Problem is I don't know the type of this value at this stage. Should I include it in the templateparamet ers?
What is the best way to solve this? something like using --newPivot++ in the
first expression? Or is there another way? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
by: Lieven |
last post by:
I want to make a quicksort algorithm that can take both a vector and a list.
This is the code I've got. But I also want to give an argument that will be
used for comparing the elements. I am used to programming in Lisp/Scheme,
where I would just pass a lambda. Is a function object something like that
in C++? And in that case, how would I pass the function/operator < (lesser
then).
//quicksort
template<typename BidirectionalIterator>...
|
by: HERC777 |
last post by:
just paste the below into notepad, call it iqsort.html double click it
and your IE browser will run it. (neat! a universal programming
language we can all run!)
it falls out from the sort procedure. I got javascript quicksort
running, it's quick but it gets stack overflow after about 5000
elements.
Am I passing the array as a parameter alright? I think javascript
default is to pass pointers, that can be global.
|
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
|
by: camotito |
last post by:
Hi, I want to sort an array of pointers, each position of this array
point to a position in an integer array.
There is a strange behavior when the 'pivot' is '0'. I am using Visual
C++, and when executing the OS tell me "quicksort.exe has encountered
a problem and needs to close". When there is no zeros in the integer
array, this doesn't happen.
The pivot is always the last element. Try to change the last element in
the array for a non...
| |
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
|
by: simo |
last post by:
Hello to all... I am trying to write an algorithm parallel in order to
realize the quicksort. They are to the first crews with C and MPI. The
algorithm that I am trying to realize is PARALLEL QUICKSORT with
REGULAR SAMPLING. The idea on the planning is right. the code that I
have written syntactically does not only have problems that it does
not want any to know to work... Are two days that I analyze it but I do
not succeed to find the...
|
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.
|
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.
|
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,...
|
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...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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...
|
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...
|
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();...
|
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...
|
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
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |