Recursion and Quick Sort

This is a discussion on Recursion and Quick Sort within the C Programming forums, part of the General Programming Boards category; Hi! I've been studying quick sort. I stumbled across this piece of code:
Code:
void quicksort(int arr[], int left, int ...

Was using the debugging feature in DevC++. Found some amusing behaviour which led me to the following conclusions..Am I right in assuming that during recursive calls to quicksort, the values of left and right passed by value rather than reference?
If not, I am seriously bugged! because I dont seem to get Quick Sort step by step at all although I get the general concept! Thanks!

:)

Clearly, the left and right parameters are of type int, i.e., the corresponding arguments are passed by value.

If they were of pointer type instead, then the corresponding arguments would still be passed by value, but we can conceptually regard what the pointers point to as being passed by reference.

Ah you hit the nail on the head! Sometimes you just think so deep that you overlook the simpler things! That's why I saw a few anomalies while watching values of left,right,i,j,to name a few! Thanks! Btw, how good is this code? I mean, are there any better code examples for quick sort that this one?

I have read all kinds of misleading posts regarding fast sorters. They were misleading in the sense that they optimized a particular algorithm, to perform exceptionally well on a particular PC system, (and there are many such optimizations you can make), or their IDEA of a fast sorting algorithm, was untested, and just wasn't that fast.

Those algorithms and optimizations for specific data, or a specific type of computer system, don't interest me. I want to use it on any PC and, I need it to be clear enough that it can be optimized for the specific type of data that will be sorted, if necessary.

To use it, you need to also have an Insertion sort function, and some of those are funky, as well. The one's that don't "shuffle" are hardly different from Bubble sort, imo. Unfortunately, the Bible of C ("The C Programming Language by K&R), has an example of a funky Insertion sort, so it's become fairly commonplace.

In the original quicksort and also in how they teach it, the pivot value is swapped to the end of the array before partitioning and then swapped into the place between the high and low items afterwards. However a few of us have found that it is faster to not do that, but to instead make a copy of the pivot whilst also leaving the pivot in the array. This further simplifies the two inner while loops as well, giving even more speed. The code you posted has this modification. Just be aware that when you look at algorithm descriptions elsewhere then they may not have this modification. When it comes to any kind of exam or test, you're probably best to know about the classic way they do it.

The thing I dislike with the code posted here is that is that it fails if the number of items to sort is zero. A caller should not have to check if the number of items is zero before calling the sort function. However if this function were called from a stub that looked like this... then it is okay:

The other thing to be aware of is that with a specially crafted array with enough items that has the values in the exact worst ordering could cause this code to create a stack overflow. This would happen if say the smallest item were in the middle of each sub-array every time, which can be crafted by someone working backwards through the algorithm. The best way to protect against that is to only recurse on the partition that has fewer items and iterate on the one that has more. Picking the splitter randomly is another way, but that still isn't a guarantee that you wont get stack overflow.

Feel free to take a look at the quicksort or other sorting algorithms on my website if you like. Any feedback is welcome also.

Which didn't move the partition. (and is VERY "fragile" in my C compiler. It was much more robust in my BASIC interpreter.) You had to adjust the size of the ST array, sometimes, however.

When I moved over to C and started reading up more on Quicksort, I couldn't understand what they were doing, moving the partition around. Later on, I learned that was just the way the early versions were done, so nearly everyone has been doing it that way, ever since.

That book wasn't much (very small), but it had some great info in it, and helped get some badly needed programs running in an office that was still using MANUAL typewriters, (one computer), for heaven's sake!