Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include<iostream>
using namespace::std;
void quicksort(int p,int q,int a[]);
int partition(int a[],int m,int p);
void interchange(int a[],int i,int j);
int main()
{
	int arr[10],n;
	cout<<"Enter the no of elements ";
	cin>>n;
	cout<<"Enter elements in array : ";
	for(int i=0;i<n;i++)>
	{
		cin>>arr[i];
	}
	quicksort(0,n-1,arr);
	for(int i=0;i<n;i++)>
	{
		cout<<arr[i];
	}
	return 0;

}
void quicksort(int p,int q,int a[])
{
	int j;
	if(p<q)>
	{
		j= partition(a,p,q+1);
		quicksort(p,j-1,a);
		quicksort(j+1,q,a);
	}
}
int partition(int a[],int m,int p)
{
	int V=a[m],i=m,j=p;
	while(i<j)>
	{
		while(a[i]<=V&&i<p)>
		{
			i=i+1;
		}
		while(a[i]>V)
		{
			j=j-1;
		}
		if(i<j)>
		{
			interchange(a,i,j);
		}
	}
	int temp=a[m];
	a[m]=a[j];
	a[j]=temp;
	return i;
}
void interchange(int a[],int i,int j)
{
	int temp;
	temp=a[i];
	a[i]=a[j];
	a[j]=temp;
}


What I have tried:

i have tried to replace n by any no but then also its not working
Posted
Updated 1-Mar-16 23:35pm
v2
Comments
Patrice T 2-Mar-16 14:05pm    
I think you should make another question with your updated code and actual problem
An example of an input and actual output can help understand what is your problem.
Philippe Mori 3-Mar-16 21:48pm    
Since arr is of size 10, then obviously you cannot expect your code to work with any value...

The following loop:


C++
while(a[i]>V)
{
    j=j-1;
}

can cause an infinite loop since you check the value using i instead of using j...

 
Share this answer
 
v2
Quote:
i have tried to replace n by any no but then also its not working

I think it is time for you to stop guessing what your code is doing. It is time to see your code executing and ensuring that it does what you expect.

The debugger is your friend. It will show you what your code is really doing.
Follow the execution step by step, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

Potential problem: you don't check that the number of values don't exeed the size of the array.

In this code, a[j] is excluded from sorting:
C++
j= partition(a,p,q+1);
quicksort(p,j-1,a);
quicksort(j+1,q,a);

My mistake, the problem is not this. looks like you are messing with indexes, the debugger is probably the best way to see what append.
 
Share this answer
 
v3
Comments
nihal saxena 2-Mar-16 6:28am    
ok thanks i got it i was returning 'i' instead of 'j'
nihal saxena 2-Mar-16 7:34am    
but then also its not working can me please tell me where i have to edit my code
Patrice T 2-Mar-16 9:17am    
My mistake, the problem is not there

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900