A Simple Implementation of Quick Sorting

Keywords: less

The basic idea of quick sorting is divide and conquer. By selecting the principal component, the array is divided into two segments. In turn, the smaller number of principal elements is placed on the left side of the principal element, and the number of principal elements is placed on the right side of the principal element. When the principal component comparison ends, the location of the principal element is its final position. The following figure is then called recursively.

When is the best time to sort quickly?

Every time just in the middle - > T (N) = O (N logN)

How to select the principal component is very important.

Here we take the median of the first, middle and last digits as the principal component.
It should be noted that when we take the principal component, we change the position of the principal component and the last element of the array so as to facilitate our next comparison.

subset division

As shown above, we put the principal at the end of the array and set two pointers (i=0, j=length-1):
i refers to the element from the starting position in order to compare with the principal component, when the hour pointer moves backward than the principal component, otherwise stop, etc.
The elements referred to in j are compared with the principal component in turn from the starting position, and when they are larger than the principal component, the pointer moves forward, or stops, etc.
When both pointers stop, exchange the elements they refer to.
Note: When the element is equal to the principal component, it should stop (considering its efficiency).

When does the pointer stop?
When i - j is greater than or equal to 0, the principal element is replaced with the element indicated by the i pointer (which is exactly the final position of the principal element).

The method is then called recursively.

The code is implemented as follows:

public class Quicksort {
	public int pivot(int[] array, int left, int right) {
		int mid = (left + right) / 2;
		if (array[left] > array[mid]) {
			swap(array, left, mid);
		}
		if (array[left] > array[right]) {
			swap(array, left, right);
		}
		if (array[mid] > array[right]) {
			swap(array, mid, right);
		}
		swap(array, mid, right);//Put the principal on the rightmost side
		int tamp = array[right];
		return tamp;
	}

	public void quickSort(int a[]) {
		int start = 0;
		int end = a.length - 1;
		quicksort(a, start, end);
	}

	public void swap(int array[], int i, int j) {
		int a = array[i];
		int b = array[j];
		array[i] = b;
		array[j] = a;
	}

	public void quicksort(int[] array, int left, int right) {
		if (left >= right) {
			return;
		}
		int temp = pivot(array, left, right);

		int i = left;
		int j = right - 1;

		while (i < j) {
			while (array[i] < temp && i < j) {//Stop and swap when the element is larger than the principal component
				i++;
			}
			while (array[j] > temp && i < j) {
				j--;
			}
			if (i < j) {
				swap(array, i, j);
				i++;
				j--;
			} else
				break;
		}
		swap(array, i, right);//Put the principal in the right place

		quicksort(array, left, i - 1);//recursion
		quicksort(array, i + 1, right);
	}

	public static void main(String[] args) {
		// Method stub for TODO automatic generation
		int[] array = { 12, 20, 5, 16,12,12, 15, 1, 30, 45, 23, 9 };

		Quicksort test = new Quicksort();
		test.quickSort(array);
		for (int i : array) {
			System.out.print(i + " ");
		}
	}
}

Quick Sorting Problem

When small-scale data (e.g., less than 100 N) is sorted, it may be better to use simple sorting (e.g., insert sorting, etc.).

Posted by saeed42 on Sat, 05 Oct 2019 12:06:11 -0700