c++ - stack depth for quicksort -
i using tail recursive version of quicksort here code
#include <iostream> using namespace std; int partition(int a[],int l,int r){ int pivot=a[l]; std::swap(a[pivot],a[r]); int store=l; (int i=l;i<r;i++){ if (a[i]<=pivot){ std::swap(a[i],a[store]); store=store+1; std::swap(a[store],a[r]); } } return store; } void tail_quick(int a[],int p,int r){ while(p<r){ int q=partition(a,p,r); tail_quick(a,p,q-1); p=q+1; } } int main(){ int a[]={21,10,13,8,56,9,34,11,22,44}; int n=sizeof(a)/sizeof(int); tail_quick(a,0,n-1); (int i=0;i<n;i++) cout<<a[i]<<" "; return 0; }
but output not correct not sorted , randome numbers in output please help
you using uninitialized variable in call partition
below. sure shouldn't else?
void tail_quick(int a[],int p,int r){ while(p<r){ int q=partition(a,p,q); // <--- uninitialized variable "q" tail_quick(a,p,q-1); p=q+1; } }
Comments
Post a Comment