Java Algorithms - Sorting (2)

Keywords: Java

Yesterday, I wrote five exchange rankings. My girlfriend gave me a big compliment. Today, I am full of passion. After work, I have time to sort out some other sorting algorithms, so I'll write the selection sorting tonight.


2. Selective Sorting (Direct Selective Sorting + Heap Sorting)

1. Direct Selection Sorting

The core idea of direct selection sorting is to select the largest (smallest) value in an unordered sequence and then exchange it with the first value.

Judgment of ascending or descending order based on comparative class Comparator
When the comparison function in Comparator is a > b? 1: (a < b? - 1: 0), it is ascending order.
When the comparison function is a > b? -1: (a < b? 1: 0), it is in descending order.

The idea of direct selection and sorting is as follows:

The whole array is an unordered region
(2) Traversing from the first element of the disordered region
(3) Find out the minimum value of the element after the initial element in the disordered region.
(4) Minimum Value and the Beginning Element Exchange Location of Unordered Region
The first element of the disordered region is added to the ordered region.
Repetition 1~_


Time complexity: O(n^2)

Spatial complexity: O(1)


public static <T> void selectSort(T[] t, Comparator<? super T> comparator) {
		int size = t == null ? 0 : t.length;
		T temp = null;
		for(int i = 0; i < size; i++) {
			int k = i;
			for(int j = size - 1; j > i; j--) {
				if(comparator.compare(t[j], t[k]) < 0) k = j;  // The equivalent of t[k] is always the minimum
			}
			// When the minimum value is found, it is exchanged with the value at the beginning of the sequence.
			temp = t[i];
			t[i] = t[k];
			t[k] = temp;
		}
	}

2. heap sort

Judgment of ascending sequence or descending sequence according to Comparator class of comparative class
If the return value of comparison function in Comparator is a > b? 1: (a < b? - 1: 0), it is an ascending sequence.
If the return value of the compare function is a > b? -1: (a < b? 1: 0), it is a descending sequence.

The idea of heap sorting:

Initially, the sequence of n numbers is regarded as a binary tree arranged in sequence, the top elements of the heap are taken out, and then a new binary tree is formed.
1. How to compose the sorting sequence into a sequential binary tree, that is, to build the initial heap
(1) Complete binary tree with n nodes, then the last node is the child node of (int)n/2 nodes.
(2) Starting from the (int)n/2 node-rooted subtree, making the subtree a heap
(3) Screening the subtrees with roots at each node in turn to make the subtrees a heap.
The initial heap is built up until the root node
2. After removing the top elements of the heap, the structure of the binary tree has been destroyed. How to reconstitute a new sequential binary tree?
Take out the top element and put the bottom element into the top of the heap (skillfully, exchange the last element with the top element)
Exchange root nodes with smaller elements in left and right subtrees
Exchange with the left subtree: If the left subtree heap is destroyed, that is, the root node of the left subtree does not satisfy the nature of the heap, then repeat the method.
If the right subtree is exchanged with the right subtree, if the right subtree heap is destroyed, the root node of the right subtree does not satisfy the nature of the heap. Repeat method_
_Until the leaf nodes, the stack is built.

Time complexity: O(nlog2n)

Spatial complexity: O(1)

public static <T> void heapSort(T[] t, Comparator<? super T> comparator) {
		int size = t == null ? 0 : t.length;
		/**********Establishment of Initial Reactor*****************/
		// Location length / 2 - 1 of the last child node
		for(int i = (size / 2 - 1); i >= 0; i--) {
			adjustHeap(t, i, size, comparator);
		}
		/**********Remove the top element and rebuild the heap**/
		// Adjust the sequence from the last element
		for(int i = size - 1; i > 0; i--) {
			//Exchange the top element t[0] and the last element in the heap
			T temp = t[0];
			t[0] = t[i];
			t[i] = temp;
			// Rearrangement heap
			adjustHeap(t, 0, i, comparator);
		}
	}
/**
	 * Adjust t[position] to make it a small (large) top heap according to comparator. Source tree with root at position node will be screened.
	 * @param t Heap array to be adjusted
	 * @param position Location of array elements to be adjusted
	 * @param length Length of the array to be adjusted
	 * @param comparator Comparison class
	 * @date 2017 March 7th 2013
	 */
	private static <T> void adjustHeap(T[] t, int position, int length, Comparator<? super T> comparator) {
		T temp = t[position];   // Record the value of the array element d to be adjusted, that is, the value of the root node
		int child = 2 * position + 1;  // Location of the Left Child
		while(child < length) {
			// Find out the smaller (larger) values of left and right children
			if(child + 1 < length && comparator.compare(t[child + 1], t[child]) > 0) {
				child++;
			}
			// If the smaller (larger) values of the left and right children are smaller (larger) than the root node, they are replaced.
			if(comparator.compare(t[child], t[position]) > 0) {
				t[position] = t[child];   // Exchange small (large) values with root nodes
				position = child;         // Reset position, the location of the next node to be adjusted
				child = 2 * position + 1; // Reset the position of the left child
			} else {
				break;  // If the current node to be adjusted is smaller (larger) than its left and right children, then it does not need to be adjusted and withdraws directly.
			}
			t[position] = temp;  // Set the child node to the root node
		}
	}

Test program

disturbOrder(T [] t) See the java algorithm - sort - exchange sort

public static void main(String[] args) {
		Integer[] numbers = new Integer[9999];
		for(int i = 0; i < numbers.length; i++) {
			numbers[i] = i;
		}
		
		disturbOrder(numbers);
		long start2 = System.currentTimeMillis();
		selectSort(numbers, new Comparator<Integer>() {
			@Override
			public int compare(Integer a, Integer b) {
				return a > b ? 1 : (a < b ? -1 : 0);
			}
		});
		long end2 = System.currentTimeMillis();
		System.out.println("Selection sort is time-consuming:" + (end2 - start2));
		
		disturbOrder(numbers);
		long start3 = System.currentTimeMillis();
		heapSort(numbers, new Comparator<Integer>() {
			@Override
			public int compare(Integer a, Integer b) {
				return a > b ? 1 : (a < b ? -1 : 0);
			}
		});
		long end3 = System.currentTimeMillis();
		System.out.println("Heap sorting is time-consuming:" + (end3 - start3));
}

Posted by Jakebert on Fri, 12 Apr 2019 10:57:32 -0700