Java realizes rapid sorting, detailed comments

tags: notes  data structure  Quick sort  algorithm  java

Implementation process

1 Select a point element from the sequence (Pivot)
✓ Suppose each element is selected 0 positions as a point element
2 Separate the sequence into 2 subsequences using Pivot
✓ Place less than pivot in front of Pivot (left side)
✓ Place the elements greater than pivot in the back of the pivot (right side)
✓ Other elements of Pivot can be used
3 pair subsequences 1 2 operation
✓ until it is no longer divided (only 1 element in the child sequence)

Rapid Sorting - Point Construction Process

Quick Sorting - Elements equal to the axis point

The judgment of the CMP location is changed to <=,> = what effect will

The nature of the rapid sorting algorithm: converts each element into a point element.

Code

package com.mj.sort.cmp;

import com.mj.sort.Sort;

public class QuickSort<T extends Comparable<T>> extends Sort<T> {

	@Override
	protected void sort() {
		sort(0, array.length);
	}

	/**
	 * Rapid sorting elements in [Begin, End) range
	 * @param begin
	 * @param end
	 */
	private void sort(int begin, int end) { 
		if (end - begin < 2) return;
		
		/ / Determine the position of the point O (N)
		int mid = pivotIndex(begin, end);
		// Quick sequence of subsequences
		sort(begin, mid); 
		sort(mid + 1, end); 
	} 
	
	/**
	  * Configure the axis point elements in [Begin, End) range
	  * @Return point elements final position
	 */
	private int pivotIndex(int begin, int end) {
		/ / Randomly select an element with the begin position to exchange the clocked point of the point, the begin position is the point element
		swap(begin, begin + (int)(Math.random() * (end - begin)));
		
		// Back up the element of the Begin location
		T pivot = array[begin];
		// end points to the last element
		end--;
		
		while (begin < end) {
			while (begin < end) {
				if (cmp(pivot, array[end]) < 0) { // Right Element> Point Elements
					end--;
				} else { // Right Element <= point element
					array[begin++] = array[end];	// Cover the element of the Begin position with the right elements, then Begin ++
					break;							// After overriding, change the comparison direction
				}
			}
			while (begin < end) {
				if (cmp(pivot, array[begin]) > 0) { // Left element <axis point element
					begin++;
				} else { // Left Element> = Point Element
					array[end--] = array[begin];   // covers the element pointing to the right side of the array with the left element, which is greater than his right. Then end--
					break;						   // After overriding, change the comparison direction
				}
			}
		}
		// When Begin == End, the Begin location is the final position of the point element element.
		// put the point element into the final position
		array[begin] = pivot;
		// Return to the position of the point element
		return begin;
	}
}

Intelligent Recommendation

JavaScript realizes rapid sorting

Quick sorting ideas: The rapid sorting algorithm is sorted by multiple comparisons and exchanges, and its sorting process is as follows: (1) First set a boundary value, divide the array into the left ...

Android integrated Baidu map (1)

Environment configuration and MapView basis One Create an application to get the key 1. Baidu Map Open Platform 2. Create an application to get the key Note here: The SHA1 must be correct. The default...

Maven and gradle introduced a third party JAR package

First, Maven Maven Install the JAR Pack to the local repository, IDEA console or DOS page input command: Second, Gradle Gradle introduces a third-party JAR package, and build a libs folder at the same...

Java__ polymorphism foundation

Java__ polymorphism foundation What is multi-state? A: The polymorphism is the same behavior with multiple different expressions or forms. First, from inheritance, such as "people" words, wi...

@ProfileActive@ in SpringBoot configuration file

@profileActive@ is to cooperate with maven profile to select different configuration files for development Configuration in application.properties Configuration in pom Packaging commands related artic...

More Recommendation

Git Remote Warehouse Submission Code

Clone Current Warehouse doesn't know how to clone? Checkhelp 。 HTTPSSSH Create a new warehouse from the command line Push the warehouse that has been created from the command line     &...

[JUC programming advancement] eight lock phenomenon

Who is the lock? Object, Class 1. In the case of the standard, it is first to print sending text messages, or call it? Send a text message first, then call (who first get the lock, who will be execute...

MySQL index (1)

MySQL index detailed First, index common model Second, InnoDB index type Third, index maintenance Fourth, small knot First, index common model Index: It is a data structure that helps Mysql efficientl...

One question daily in the winter vacation -the luck in the matrix

Title Link:https://leetcode-cn.com/problems/lucky-numbers-in-a-matrix/ Description Exemplary example Thinking Use two array for pre -processing, and store the minimum value of each line and the maximu...

luogu3959

Summary of solutions to all 1~2 questions in NOIP2017:Another blog of Ben Konjac (but the blog is not backed up (source code), deleted, I had to come to CSDN first) is to search, onlyCan pass luogu...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top