private void QuickSort( int[] a, int left, int right ) { if( a == null ) return; int i = left; int j = right; int pivot = a[( left + right ) / 2]; while( i <= j ) { while( a[i] < pivot ) i++; while( a[j] > pivot ) j--; if( i <= j ) { int tmp = a[i]; a[i++] = a[j]; a[j--] = tmp; } } if( j > left ) { QuickSort( a, left, j ); } if( i < right ) { QuickSort( a, i, right ); } }
Ready to future-proof your business? Meet NXT Digital, your all-in-one partner for complete digital transformation. We specialise in building custom digital solutions, and support your applications. Don't get left behind—experience the next level of innovation and let NXT Digital transform your vision.
Monday, November 16, 2009
c# Quick Sort Algorithm Implementation
Subscribe to:
Post Comments (Atom)
4 comments:
i think that should be "
if (j > left) Quicksort(a, left, j);
"
After execution it throws Stack Overflow.
It works when you change if condition
replace
if(j>0)
with
if(j>left)
with if (j > left) it works very well.
can you explain to me how this algorithm?
Post a Comment