### Quicksort question

I was hoping someone could explain how the `quick()` function and the `partition()` function works. I found this source code online. I have used pieces of it in my own code but I do not understand what it does, just that it does what I ask.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849`` ``````#include using namespace std; void exch(int a[],int i,int j){ int s=a[i]; a[i]=a[j]; a[j]=s; } int partition(int a[],int l,int h); void quick(int a[],int l,int h){ if (h<=l) return ; int j=partition(a,l,h); quick(a,l,j-1); quick(a,j+1,h); } int partition(int a[],int l,int h){ int i=l-1; int j=h; int v=a[h]; while(true){ while( a[++i]v) if (j==i) break; if (i>=j) break; exch(a,i,j); } exch(a,i,h); return i; } int main(){ int a[]={12,43,13,5,8,10,11,9,20,17}; int n=sizeof(a)/sizeof(int); quick(a,0,n-1); for (int i=0;i

Also, line 41 confused me as well. Thanks in advance
Last edited on
Check out this link. It has code, too. And with better variable names.
http://www.algolist.net/Algorithms/Sorting/Quicksort

EDIT: line 41 gets the number of elements in the array a and stores that number in n.
Last edited on