QuickSort

Basic algorithm for quick sort

public void Qsort(int [] input) {//in place quick sort
		recursiveQsort(input, 0, input.length-1);
	}
	
	private void recursiveQsort(int [] input, int start, int end) {
	    if (start >= end) {
			return;
		}
		int pivot = input[end];
		int mid = start; // keep track of the next slot for smaller elements
		for (int i = start; i < end; i++){
		    if (input[i] <= end) 
				swap(input, i, mid++);
		}
		swap(input, mid, end);
		recursiveQsort(input, start, mid-1);
		recursiveQsort(input, mid+1, end);
	}
	
	private void swap(int [] input, int start, int end) {
		if (start == end)
			return;
		int temp = input[start];
		input[start] = input[end];
		input[end] = temp;
	}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s