Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quick Sort vs Selection Sort (Java vs C++)

I created two projects. One in C++ and one in Java. I did time trials for a QuickSort and SelectionSort for both. Oddly enough I found some very strange behavior.

Here were the results for an array of size 10,000:

SelectionSort Java: 80 ms

SelectionSort C++: 2914 ms

QuickSort Java: 1 ms

QuickSort C++: ~45 seconds

Now call me crazy but I've always been taught that QuickSort is the fastest. This proves to be true in Java yet in C++ it completely gets shut down.

So my question is, does C++ handle QuickSort differently?

I tried to keep the functions the same between languages and they are exactly the same with the exception of using a vector in C++ vs an int array. I'd prefer to use a vector anyway because the actual project I want to use the sort for in C++ requires a vector.

I'm sure it's a dumb mistake or something I'm making but please provide some insight as to why this is happening.

EDIT:

I believe I see what the problem is. Thanks everyone for the extremely fast responses. I'll be modifying my code to work as intended. I knew it was a simple mistake. Also, although the question asked is quite embarrassing, the responses are educational.

like image 838
Timlankey Avatar asked May 05 '11 17:05

Timlankey


People also ask

Is quick sort and selection sort same?

selection sort is slightly better than quicksort for huge data structures ! Where did you get this from? The algorithm takes quadratic time so it's obviously much worse than quicksort. Actually, how are you going to fit 10GB in RAM, you can't use any algorithm on your array if it's not in RAM.

What is the difference between Bubble Sort and selection sort in C?

In bubble sort, two adjacent elements are compared. If the adjacent elements are not at the correct position, swapping would be performed. In selection sort, the minimum element is selected from the array and swap with an element which is at the beginning of the unsorted sub array.

What is selection sort in C?

Selection sort is another algorithm that is used for sorting. This sorting algorithm, iterates through the array and finds the smallest number in the array and swaps it with the first element if it is smaller than the first element.

Which sorting technique is best?

Quicksort. Quicksort is one of the most efficient sorting algorithms, and this makes of it one of the most used as well. The first thing to do is to select a pivot number, this number will separate the data, on its left are the numbers smaller than it and the greater numbers on the right.


2 Answers

Your quicksort function returns your entire vector by value on every recursive call, even though the function modifies it in place. Probably returning all those temporaries and then throwing them away hurts performance.

Just change the function to void and remove the ending return and see how it behaves.

EDIT: If you're more used to Java where almost everything is garbage collected references, note that in C++ a return by value (as you have on the return type) typically makes a copy of whatever is being returned. And as @Johannes Schaub - litb points out the compiler isn't even able to optimize the return away because it's not returning an automatic (local) variable.

EDIT2: If you aren't doing this as an exercise however you should use either std::sort or std::stable_sort (the latter if you know your data will already be almost sorted, or you need to preserve the order of duplicates). For example std::sort(A.begin(), A.end());

like image 114
Mark B Avatar answered Sep 17 '22 13:09

Mark B


You are returning a complete vector on every recursive call. This takes a lot of time (99,99% of the time spent copying).

By the way, you can use the STL sort function in C++, it's guaranteed to be a quicksort (though this will mess up your profiling because you're not doing a true comparison).

EDIT:

Apparently std::sort is not guaranteed to be quicksort, but it is guaranteed to be O(n*log(n)). Source

like image 40
orlp Avatar answered Sep 19 '22 13:09

orlp