Algorithm - Quick Sort

package com.arithmetic.sort;
/**
 * Quick sort method:
 * 1 Select the axis value, as far as possible to make Left and Right equal
 * Strategy: choose leftmost; choose randomly; choose average
 *
 * @author Administrator
 *
 */
public class QuickSort {
	public static void main(String[] args) {
		int [] arrays = new int[]{8,2,7,3,6,4,9,1,5,10};
		quickSort(arrays, 0, arrays.length-1);
		for(int i = 0; i<arrays.length;i++){
			System.out.print(arrays[i]+" ");
		}
		System.out.println();
	}
	/**
	 * This function is used to generate the axis value or the position of the reference value
	 * @param begin
	 * @param end
	 * @return
	 */
	public static int getPivot(int begin, int end){
		return (begin + end) >> 1;
	}
	/**
	 * Each time the division
	 * @param arrays
	 * @param begin
	 * @param end
	 * @return
	 */
	private static int partition(int[] arrays, int begin, int end){
		// get the base value position
		int pivot = getPivot(begin, end);
		// get the base value
		int pivotValue = arrays[pivot];
		/*Assign the last value to the pivot position, which means that the axis value is used as a temporary variable for comparison with the subsequence, and the last
		 *Leave it empty, we will make the first data greater than the axis value
		 */
		arrays[pivot] = arrays[end];
		//As long as begin and end are not equal, we consider this sorting is not over
		while(begin != end){
			//If begin <end and array[begin] < axis value, we think this number is smaller than the axis value, and we don't care about it, we continue to compare the next one
			while(begin < end && arrays[begin] < pivotValue){
				begin++;
			}
			//If begin <end but array[begin] > axis value, we put the current value in the last position
			//And the end position moves a pointer forward
			if(begin < end && arrays[begin] > pivotValue){
				arrays[end] = arrays[begin];
				end--;
			}
			//At this point, we start to compare the ratio from the right side, and put it at the position where the value is smaller than the axis value. If it is larger than the axis value, we don't care.
			while(begin < end && arrays[end] >= pivotValue ){
				end --;
			}
			//If begin < end, but array[end] < axis value, we put the current value in the position where begin is empty
			if(begin < end && arrays[end] < pivotValue){
				arrays[begin] = arrays[end];
				begin++;
			}
		}
		//If begin and end meet, that is, begin == end, then the two subscripts point to the same element, that is, the axis value
		arrays[begin] = pivotValue;
		return begin;
	}
	/**
	 * start quick sort
	 * @param arrays
	 * @param low
	 * @param high
	 */
	public static void quickSort(int[] arrays, int low, int high){
		if((high - low) < 1){//If there is only one number or empty, return directly
			return;
		}
		if((high - low) == 1){//If there are only 2 numbers, and the number in front is greater than the number in the back, we directly exchange no need for quick sorting
			if(arrays[low] > arrays[high]){
				swap(arrays, low, high);
			}
		}
		int pivotPosition = partition(arrays, low, high);
		quickSort(arrays, low, pivotPosition);//(recursive) the previous subsequence
		quickSort(arrays, pivotPosition+1, high);//(recursive) back subsequence
	}
	/**
	 * perform value exchange
	 * @param arrays
	 * @param index1
	 * @param index2
	 */
	public static void swap(int[] arrays,int index1, int  index2 ){
		int tmp = arrays[index1];
		arrays[index1] = arrays[index2];
		arrays[index2] = tmp;
	}
}

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327062502&siteId=291194637