Fun with Java Quick Sort

Quick Sort is an improved version of bubble sort, and it is the best kind of internal sorting, it will appear in a lot of questions in the face, but also as a sort method must master programmer.

The basic idea

Quick sort using a divide and conquer strategy to put a sequence is divided into two sub-sequences, the basic steps are:

  • Taken as the reference number of a start sequence number;
  • Zoning process: this will be a large number of the total number of put it right, it is less than or equal to the number of full-put it on the left;
  • Recursively sub-sequence of the left and right step 2, each section until only a few.

Quick sort proposed by CAR Hoare in 1962. The basic idea is: a trip by ordering the data to be sorted into two independent parts, the part where all the data than the other part of all data to be small, then this method of data for the two parts separately fast sorting, sorting the entire process can be recursively, in order to achieve the whole data into an ordered sequence.

Here we use diagrams to explain the sorting algorithm

Here Insert Picture DescriptionIn an array of example, taking reference number is the first number range.

0 1 2 3 4 5 6 7 8 9
72 6 57 88 60 42 83 73 48 85

Initially, i = 0; j = 9; temp = a [i] = 72

Having saved numbers a [0] is added to temp, can be understood as a pit dug in the array a [0], the data can be filled into the other to this.

From j start looking forward a number equal to or smaller than the temp temp's. When j = 8, meet the conditions, a [8] into a pit dug refill a [0] in.

a [0] = a [8]; i ++; such a pit a [0] was to get up, but the formation of a new pit a [8], how to do this? Simple, find a digital to fill a [8] the pit. The back from i to find a start temp number greater than one, when i = 3, meet the conditions, a [3] into a pit dug refill a [8] = a [3]; j-;

Array becomes:

0 1 2 3 4 5 6 7 8 9
48 6 57 88 60 42 83 73 88 85

i = 3; j = 7; temp = 72

Repeat the above steps, after the start looking forward, and then front looking back.

From start looking forward j, when j = 5, in line with conditions, a [5] to fill a pit dug, a [3] = a [5]; i ++;

From i started looking back, when i = 5, because the i == j exit.

In this case, i = j = 5, and a [5] is just the last pit dug, thus temp fill a [5].

Array becomes:

0 1 2 3 4 5 6 7 8 9
48 6 57 42 60 72 83 73 88 85

As can be seen a [5] it is less than the foregoing figures, a [5] is greater than the number after it. Thus above steps are repeated for a [0 ... 4] and a [6 ... 9] This two sub-interval it.

Log digging fill summarized

1. i = L; j = R; the reference number first excavated pit form a [i].

2. j-- looking from back to front is smaller than its number, after a pit dug to find a [i] in this number before filling.

3. i ++ from front to back looking than its large number, after finding this number also dug a pit before to fill a [j] in.

4. Repeat steps 2 and 3 two further, until i == j, the reference number filled in a [i] in.



package quickSort;
 
public class QuickSort {
	private static int count;
	/**
	 * 测试
	 * @param args
	 */
	public static void main(String[] args) {
		int[] num = {3,45,78,64,52,11,64,55,99,11,18};
		System.out.println(arrayToString(num,"未排序"));
		QuickSort(num,0,num.length-1);
		System.out.println(arrayToString(num,"排序"));
		System.out.println("数组个数:"+num.length);
		System.out.println("循环次数:"+count);
		
	}
	/**
	 * 快速排序
	 * @param num	排序的数组
	 * @param left	数组的前针
	 * @param right 数组后针
	 */
	private static void QuickSort(int[] num, int left, int right) {
		//如果left等于right,即数组只有一个元素,直接返回
		if(left>=right) {
			return;
		}
		//设置最左边的元素为基准值
		int key=num[left];
		//数组中比key小的放在左边,比key大的放在右边,key值下标为i
		int i=left;
		int j=right;
		while(i<j){
			//j向左移,直到遇到比key小的值
			while(num[j]>=key && i<j){
				j--;
			}
			//i向右移,直到遇到比key大的值
			while(num[i]<=key && i<j){
				i++;
			}
			//i和j指向的元素交换
			if(i<j){
				int temp=num[i];
				num[i]=num[j];
				num[j]=temp;
			}
		}
		num[left]=num[i];//将标记位和最后一个比他小的数换位置
		num[i]=key;//key在他正确的位置上
		count++;
		QuickSort(num,left,i-1);
		QuickSort(num,i+1,right);
	}
	/**
	 * 将一个int类型数组转化为字符串
	 * @param arr
	 * @param flag
	 * @return
	 */
	private static String arrayToString(int[] arr,String flag) {
		String str = "数组为("+flag+"):";
		for(int a : arr) {
			str += a + "\t";
		}
		return str;
	}
 
}

Hereinafter blog transferred from the code, and contains examples Analysis
Original: https://blog.csdn.net/qq_36186690/article/details/82470451

Guess you like

Origin blog.csdn.net/qunqunstyle99/article/details/95311714