Hi, i've tried to get my quicksort to work, but i think because i have been looking at it for so long i cant see the problem with it so if someone can spot my mistake that would be great!

int parttition(int myArray[],int first, int last);
void quicksort(int myArray[],int first, int last);


//last=size-1;




void quick( int size)
{
	int	*myArray;

	myArray = (int *) malloc (size * sizeof(int));
	myArray= new int [size];

int x;
	srand((unsigned)time(0)); 
	for (x=0;x<size;x++)
	{
		int random_integer = rand()/25; 
		printf("%d\n",myArray[x]=random_integer);
	}
	printf("\n\n\n\n");
	int first,last;
	first=0;
	last=size-1;

	quicksort( myArray, first,  last);
	for (x=0;x<size;x++)
	{
		printf("%d\n",myArray[x]);
	}
}



int parttition(int myArray[],int first, int last)
{
	int pivot,i,s;
	pivot=myArray[last];
	i=first;
	s=last-1;
	int temp1=0,temp2=0,temp3=0;
	
	while( i<=last)
	{
		
		if (( myArray[i]>pivot))
		{
			temp1= myArray[i];
		}
		i++;
	}
	while(s>=0)
	{
		if ( myArray[s]<pivot)
		{
			temp2= myArray[s];
		}

		s--;
	}
		if ( s<i+1)
		{
			myArray[s]=temp1;
			myArray[i-1]=temp2;
			


		}
		else
		{
			return first;
		}
		temp3=pivot;
		pivot=temp1;
		myArray[i-1]=myArray[last];
	}


void quicksort(int myArray[],int first, int last)
{ 
	int	pivot=0;
	if (first<last)
	{
		pivot=parttition(myArray,first, last);
			quicksort(myArray,first, pivot-1);
			quicksort(myArray,pivot+1, last);

	}
}

Recommended Answers

All 2 Replies

1) delete line 14 -- it is causing a huge memory leak.

2) line 22: Don't try to get fancy -- split that into two lines so make it easier to understand. Don't write programs in an attempt to impress anyone because you will probably fail.

3) a few seconds of googling would have given you the C course code for the algorithm.

I've done all that but yet it is not working!
I did try and google it but I could not find any code that seemed to work which is why I am struggling stil, if i could have a couple of pointers that would be great

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.