LeetCode 912. Sort array

Posted by ted_chou12 on Fri, 03 Apr 2020 15:07:07 +0200

My LeetCode: https://leetcode-cn.com/u/ituring/

My LeetCode source code [GitHub]: https://github.com/izhoujie/Algorithmcii

LeetCode 912. Sort array

subject

Give you an array of integers, nums, in ascending order.

Example 1:

Input: nums = [5,2,3,1]
Output: [1,2,3,5]

Example 2:

Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]

Tips:

  • 1 <= nums.length <= 50000
  • -50000 <= nums[i] <= 50000

Source: LeetCode
Links: https://leetcode-cn.com/problems/sort-an-array
Copyright belongs to the network. For commercial reprint, please contact the official authorization. For non-commercial reprint, please indicate the source.

Solving problems

There is nothing to say about this problem. The arrangement will be finished soon;
Extension: the sort (dual pivot quicksort) source code in Arrays is worth looking at. Sort is a multi factor sorting method that integrates quick sorting, merging, direct selection, insertion sorting, etc,
The designer of source code should calculate several data volume boundaries based on a large number of data, so as to decide to use the optimal sorting algorithm;
Some parts of the sort source code:

/**
 * This class implements the Dual-Pivot Quicksort algorithm by
 * Vladimir Yaroslavskiy, Jon Bentley, and Josh Bloch. The algorithm
 * offers O(n log(n)) performance on many data sets that cause other
 * quicksorts to degrade to quadratic performance, and is typically
 * faster than traditional (one-pivot) Quicksort implementations.
 * The sorting algorithm is a two axis fast sorting, which is implemented by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch,
 * In a large number of data sets, the worst sorting performance of other fast sorting algorithms is O(n ²), but the algorithm will always provide stable O(nlogn) sorting performance, and its efficiency is better than the traditional single axis fast sorting
 *
 * All exposed methods are package-private, designed to be invoked
 * from public methods (in class Arrays) after performing any
 * necessary array bounds checks and expanding parameters into the
 * required forms.
 * All methods are package private so that they can be called by Arrays after necessary boundary checking, parameter extension and formatting
 *
 * @author Vladimir Yaroslavskiy
 * @author Jon Bentley
 * @author Josh Bloch
 *
 * @version 2011.02.11 m765.827.12i:5\7pm
 * @since 1.7
 */
final class DualPivotQuicksort {
...
}

Several boundary quantities:

/**
 * The maximum number of runs in merge sort.
 * Maximum number of merge sub columns: the maximum number of sequences to be merged
 */
private static final int MAX_RUN_COUNT = 67;
/**
 * The maximum length of run in merge sort.
 * Merge sub column maximum length: the maximum length of the sequence to be merged
 */
private static final int MAX_RUN_LENGTH = 33;
/**
 * If the length of an array to be sorted is less than this
 * constant, Quicksort is used in preference to merge sort.
 * Quick sort threshold: if the length of the array to be sorted is less than this value, quick sort is preferred instead of merge sort
 */
private static final int QUICKSORT_THRESHOLD = 286;

/**
 * If the length of an array to be sorted is less than this
 * constant, insertion sort is used in preference to Quicksort.
 * Insertion sort threshold: if the length of the array to be sorted is less than this value, insertion sort is preferred instead of quick sort
 */
private static final int INSERTION_SORT_THRESHOLD = 47;

/**
 * If the length of a byte array to be sorted is greater than this
 * constant, counting sort is used in preference to insertion sort.
 * byte Array count sort threshold: if the byte array length to be sorted is greater than this value, count sort is preferred instead of insert sort
 */
private static final int COUNTING_SORT_THRESHOLD_FOR_BYTE = 29;

/**
 * If the length of a short or char array to be sorted is greater
 * than this constant, counting sort is used in preference to Quicksort.
 * short&char Array count sort threshold: if the array to be sorted is of type short or char and the array length is greater than this value, count sort will be used first rather than insert sort
 */
private static final int COUNTING_SORT_THRESHOLD_FOR_SHORT_OR_CHAR = 3200;

Train of thought 1 - fast platoon;

Steps:

  1. Find an intermediate value (generally the first number), and put the value less than the value on the left, and the value greater than the value on the right;
  2. The last ending position (excluding the position) is the operation of dividing line and dividing two blocks recursively for 1;
  3. The end condition is that left and right indexes meet;

Algorithm source code example

package leetcode;

/**
 * @author ZhouJie
 * @date 2020 12:02:03 am, March 31, 2015 
 * @Description: 912. Sort array
 *
 */
public class LeetCode_0912 {

}

class Solution_0912 {

	/**
	 * @author: ZhouJie
	 * @date: 2020 12:50:01 am, March 31, 2015 
	 * @param: @param nums
	 * @param: @return
	 * @return: int[]
	 * @Description: Quick sort
	 *
	 */
	public int[] sortArray(int[] nums) {
		quickSort(nums, 0, nums.length - 1);
		return nums;
	}

	/**
	 * @author: ZhouJie
	 * @date: 2020 12:50:04 am, March 31, 2010 
	 * @param: @param nums
	 * @param: @param start
	 * @param: @param end
	 * @return: void
	 * @Description: Fast row core
	 *
	 */
	private void quickSort(int[] nums, int start, int end) {
		if (start >= end) {
			return;
		}
		int midVal = nums[start];
		int left = start, right = end;
		while (left < right) {
			while (left < right && nums[right] >= midVal) {
				right--;
			}
			nums[left] = nums[right];
			while (left < right && nums[left] <= midVal) {
				left++;
			}
			nums[right] = nums[left];
		}
		nums[left] = midVal;

		quickSort(nums, start, left - 1);
		quickSort(nums, left + 1, end);
	}

}

Topics: Java less github network