hi, I have a problem with implementation of a quicksort algorithm. I have pseudo-code which helping me understand this, but I still can't make this program to work. Does anybody can help me figur out what is wrong in my code?


My code :

#include <iostream>
#include <ctime>

void setA (int A[], int size) 
	for (int i=0; i< size; i++)
		A[i] = rand()%10 + 1;
		std::cout << i + 1 << " index value = " << A[i] << std::endl;

void printA (int A[], int size)
	for (int i=0; i<size; i++)
		std::cout << "now the value of index " << i + 1 << " = " << A[i] << std::endl;

int partition (int A[], int down, int top)
	int w = A[down];
	int i = down;
	int j = top + 1;
	} while (A[j] <= w);

	} while (A[i] >= w);

	if (i < j)
		int temp = A[i];
		A[i] = A[j];
		A[j] = temp;

		int temp = A[j];
		A[j] = A[down];
		A[down] = temp;
		return j;

void quicksort (int A[], int down, int top)
	if (down < top)
		int j = partition (A, down, top);
		quicksort (A, down, j-1);
		quicksort (A, j+1, top);

int main()
	srand((unsigned int)time(NULL));
	int size; 
	std::cout << "Enter array size : " << std::endl;
	std::cin >> size;
	int A[10000];
	int down = 0; 
	int top = size;
	setA (A, size);
	std::cout << "--------------------------------------------" << std::endl;
	partition(A, down, top);
	printA (A, size);
	return 0;

I'm totaly beginner so if my code is ugly or non-standard than correct me pls.

Edited by gfx: n/a

8 Years
Discussion Span
Last Post by griswolf

Just as a minor suggestion: Get used to zero-based indexing. Your cout << ... i+1 ... makes me itch.

For debugging purposes, it is good to use a fixed set of data so you can easily see what changes when you modify your code. I suggest this:
{5,1,9,2,3,4,8,0,7,6} which is small enough to see easily, large enough to require a little bit of recursion, and not a power of 2 in length.

Also: Please be very specific about the problem. "I can't make this program work" is too broad. Something like "It doesn't compile and this is the error" or "it sorts all but one item and here is the original and nearly sorted data", or the like

Votes + Comments
Solid advice
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.