QuickSort an Array of Integers

Description: QuickSort implementation for sorting an array of integers.
Tested Platform: .NET Compiler, Windows 7
Language: C/C++
int partition(int data[], int left, int right) {
	int pivot = data[right - 1];
	int i = left - 1;
	int temp;

	for (int j = left; j < right; j++) {
		if (data[j] <= pivot) {
			i += 1;
			temp = data[i];
			data[i] = data[j];
			data[j] = temp;
		}
	}
	return i;
}

int* quicksort(int data[], int left, int right) {
	if (left < right) {
		int i = partition(data, left, right);
		quicksort(data,left, i);
		quicksort(data, i+1, right);
	}
	return data;
}

Submitted: October 23, 2012

Return to the snippets listing