0

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?

4
  • 19
    The fastest sorting algorithm would be no-op-sort: It's preconditions include a sorted array as input. Commented Mar 3, 2010 at 13:06
  • You definitely want bubblesort. No doubt. This is the algorithm for you! Step right up, take 'er for a drive. Want to sign right now before you try it? Commented Mar 3, 2010 at 13:13
  • 5
    The fastest sorting algorithm depends on the input data. What datatype, how sorted is it, do the values fall in a known range, how big is the set of data to sort, and so on. In the most general case, quicksort is probably your best bet, but depending on all these factors, other algorithms may be better. Commented Mar 3, 2010 at 13:31
  • Of course std::sort is usually fast enough for typical use cases, but there are some C++ libraries that claims to be faster than std::sort on some compilers -- search for them and benchmark yourself -- on GitHub/GitLab/BitBucket/SourceForge for example (this question is rather high in DuckDuckGo search result with some keywords...) Commented Jan 1, 2021 at 3:16

11 Answers 11

20

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!

Sign up to request clarification or add additional context in comments.

2 Comments

And if you're sorting large objects you can always sort (smart) pointers to them instead of the actual objects.
This question is very old, but std::sort is O(n log n) worst case since C++11.
6

Use std::sort

Comments

3

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

Comments

3

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.

Comments

3

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.

Comments

0

Use QuickSort function (qsort)

1 Comment

The qsort() function will normally be slower than std::sort in C++. Genericity through void * has more overhead than genericity through templates.
0

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.

Comments

0

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.

Comments

0

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

Comments

0

This code uses the QuickSort algorithm, which has an average time complexity of O(nlogn) and is generally considered to be one of the fastest sorting algorithms. Additionally, the swap() function is used to swap elements in the array, which is typically faster than implementing a swap function manually.

#include <iostream>
#include <algorithm>

using namespace std;

// Function to partition the array
int partition(int arr[], int low, int high) {
    int pivot = arr[high]; // Choose the last element as the pivot
    int i = low - 1;
    for (int j = low; j <= high - 1; j++) {
        if (arr[j] < pivot) {
            i++;
            swap(arr[i], arr[j]);
        }
    }
    swap(arr[i + 1], arr[high]);
    return i + 1;
}

// QuickSort function
void quickSort(int arr[], int low, int high) {
    if (low < high) {
        int pi = partition(arr, low, high); // pi is partitioning index
        quickSort(arr, low, pi - 1); // Sort the elements before partition
        quickSort(arr, pi + 1, high); // Sort the elements after partition
    }
}

// Driver code to test the sorting function
int main() {
    int arr[] = { 64, 25, 12, 22, 11 };
    int n = sizeof(arr) / sizeof(arr[0]);

    quickSort(arr, 0, n - 1);

    cout << "Sorted array: ";
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
    return 0;
}

Comments

-1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.