tags:

views:

249

answers:

10

Hello... In C++ I need to sort the arrays that I had written with a fast speed as possible, My Question is What is the Best and the Fastest sorting function to use? Or Just Make One with MY self?

A: 

Use QuickSort function (qsort)

Alelz
The `qsort()` function will normally be slower than `std::sort` in C++. Genericity through `void *` has more overhead than genericity through templates.
David Thornley
+2  A: 

I think if you have sufficient time, go through some sorting algorithms, that will help you a lot to solve your problem

lakhlaniprashant.blogspot.com
+6  A: 

Use std::sort

Didier Trosset
+19  A: 

Use the std::sort function which typically defaults to quicksort (but deals with the ugly edge cases of e.g. a fully sorted array taking O(n^2) time).

Then measure the speed: if that's not good enough, describe details (e.g. how large are your arrays, what data do they contain, will there be a lot of equivalent elements and is stability important), and get further advice. Don't, for the love of Knuth, implement your own sorting function unless you have some extremely unique requirements!

Pontus Gagge
And if you're sorting large objects you can always sort (smart) pointers to them instead of the actual objects.
Mark B
A: 

Yes,Quicksort is the best, but if you have a new good algorithm and it is good for your current situation, Just write your own.

Engo
+2  A: 

Use Radix- or Bucket sort if your keys are small in size (integers for example) and your sort typical more than 500 items.

Otherwise use introsort: http://en.wikipedia.org/wiki/Introsort You may want to try std::sort as well. Usually it is just an implementation of introsort.

Nils Pipenbrinck
+1  A: 

Whatever you do, don't try to make your own sorting algorithm unless you really know what you're doing, and you have some very unusual requirements.

applechewer
A: 

While qsort alone could very well be the safer option, if you are really desperate for speed you have to give a little bit more about the specific case.

In general, sort methods are considered "better" when they scale well for large dataset and work gracefully with general cases without having pathologically bad results for corner cases (like sorting an almost completely sorted array, or an array which is in reverse order).

So while you look at sorting methods in general (plenty of literature) keep in mind if your specific applications has to deal with plenty of small (say 10-12 items) arrays. Or very large array. Or if memory is limited. Or if the compare function is extremely expensive.

p.marino
A: 

I wouldn't write my own sort unless your requirements are extremely specific. Use the ones that are part of the standard library, they are well tested and unlikely to trip you up when it comes to boundary conditions.

That said, if you have an array and you need the contents sorted in a specific order every time (as opposed to being able to resort it by various arbitrary criteria) I would think that you are using the wrong container and might want to look into one that automatically sorts the elements on insert, for example a std::set or std::multiset.

Timo Geusch
A: 

sorting algorithms basically depend on the requirement of the programmer .the size of the array (to be sorted ) is highly matters in this case . so for instance you can check out the link and know about the efficiency of the various sorting algorithms

http://www.durangobill.com/SortAlgorithms/Sort_Algorithms.html

sandy101