what is wrong with my code for quick sort...??

#include<iostream>
#include<stdio.h>
#define max 50
using namespace std;
void qsort(int *arr,int,int);
int partition(int *arr,int,int);
int main()
{
int n,i;
cout<<"Enter size of the array:\n";
cin>>n;
int a[max];
for(i=0;i<n;i++)
cin>>a[i];
qsort(a,0,n-1);
for(int k=0;k<n;k++)
cout<<a[k]<<endl;
return 0;
}
void qsort(int *arr,int left,int right)
{
if(right>left)
{
int p;
p=partition(arr,left,right);
qsort(arr,left,p-1);
qsort(arr,p+1,right);
}
}
int partition(int *arr,int left,int right)
{
int i,p,q,t;
p=left+1;
q=right;
while(q>=p)
{
while(arr[p]<i)
p++;
while(arr[q]>i)
q--;
if(q>p)
{
t=arr[p];
arr[p]=arr[q];
arr[q]=t;
}
}
t=arr[left];
arr[left]=arr[q];
arr[q]=t;
return q;
}
[code] "Please use code tags" [/code]
You never assigned a value to 'i' in partition()
i see you defined a max of 50 for int a[max] in int main(), but i dont believe that will stop anyone from entering a number higher than 50 for the size of the array.... also im pretty sure you can't call a function while within said function...


1
2
3
4
5
6
7
8
9
10
void qsort(int *arr,int left,int right)
{
if(right>left)
{
int p;
p=partition(arr,left,right);
qsort(arr,left,p-1);     //not sure this flow would work
qsort(arr,p+1,right);
}
}
Topic archived. No new replies allowed.