I wrote this code below and it isn't working giving me a stack overflow problem.Can someone please help me?

#include<iostream>
using namespace std;


int partition(int* A,int l,int r)
{
	 int v=A[r];
	 int i=l;
	 int j=r;
	 int temp;
	 while(i<j)
	 {
		 while(A[i]<v)
		 {
			 i++;
		 }
		 while((i<j)&&(A[j]>=v))
		 {
			 j--;
		 }
		 if (i<j)
		 {
			 temp=A[i];
			 A[i]=A[j];
			 A[j]=temp;
		 }
		 else
		 {
			 temp=A[i];
			 A[i]=A[r];
			 A[r]=temp;
		 }
	 }
	 return i;
}

void quicksort(int* A,int l, int r)
{
	if(r>1)
	{
		index=partition(A,l,r);
		quicksort(A,l,index-1);
		quicksort(A,index+1,r);
	}
}

int main()
{
	int myArray[10]={3,4,2,1,8,7,5,9,6,0};
	for(int i =0; i<10; i++)
	{
		cout << myArray[i] << " ";
	}
	cout << endl;
	quicksort(myArray,0,9);
	for(int i=0; i<10;i++)
	{
		cout << myArray[i] << " ";
	}
	system("pause");
    return 0;
}

I don't know WTH but I rewrote the same thing again and now its working.

#include<iostream>
using namespace std;

int partition(int* A, int l, int r)
{
	int i=l;
	int j=r;
	int v=A[r];
	int temp;
	while(i<j)
	{
		while(A[i]<v)
		{
			i++;
		}
		while((i<j)&&(A[j]>=v))
		{
			j--;
		}
		if(i<j)
		{
			temp=A[i];
			A[i]=A[j];
			A[j]=temp;
		}
		else
		{
			temp=A[i];
			A[i]=A[r];
			A[r]=temp;
		}
	}
	return i;
}

void quicksort(int* A, int l, int r)
{
	int i;
	if(r>l)
	{
		i=partition(A,l,r);
		quicksort(A,l,i-1);
		quicksort(A,i+1,r);
	}
}




int main()
{
	int Arr[12]={5,1,19,25,17,21,5,19,20,9,15,14};
	int *pt= &Arr[0];
	for(int z=0; z<12;z++)
	{
		cout << Arr[z] << " ";
	}
	cout << endl;
	quicksort(Arr,0,11);
	for(int z=0; z<12;z++)
	{
		cout << Arr[z] << " ";
	}
	system("pause");
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.