Eight sort algorithms that must be known [java Implementation] (3) Merge sort algorithms and Heap Sort Algorithms

Source: Internet
Author: User

Eight sort algorithms that must be known [java Implementation] (3) Merge sort algorithms and Heap Sort Algorithms

I. Merge Sorting Algorithm

Basic Idea:

The Merge Sorting method combines two (or more) ordered tables into a new ordered table, that is, the sequence to be sorted is divided into several subsequences, each of which is ordered. Then combine the ordered subsequences into the overall ordered sequence.

Examples of Merge Sorting:

 

 

Merge method:

Set r [I... N] Two ordered sub-tables r [I... M] and r [m + 1... N], the two sub-tables are n-I + 1 and n-m respectively.

Algorithm Implementation:

/*** Merge Sorting ** Introduction: merges two (or more) ordered tables into a new ordered table, which divides the sequence to be sorted into several subsequences, each sub-sequence is ordered. Then merge the ordered subsequences into the overall ordered sequence * the time complexity is O (nlogn) * stable sorting mode * @ param nums array to be sorted * @ return output ordered array */public static int [] sort (int [] nums, int low, int high) {int mid = (low + high)/2; if (low 

 

 

 

Ii. Heap Sorting Algorithm

 

1. Basic Ideas:

Heap sorting is a kind of tree-based sorting that effectively improves the Direct selection and sorting.

Defined by heap: sequences with n elements (h1, h2 ,..., hn), if and only if (hi> = h2i, hi> = 2i + 1) or (hi <= h2i, hi <= 2i + 1) (I = 1, 2 ,..., n/2) is called heap. Here we only discuss the heap that meets the conditions of the former. From the definition of heap, we can see that the heap top element (that is, the first element) must be a heap top element (large top heap ). A fully binary tree can intuitively represent the heap structure. The heap top is the root, and the others are the left and right subtree.

Thought: Initially, we thought of the sequence of numbers to be sorted as a binary tree for sequential storage, and adjusted their storage order to make it a heap. At this time, the number of root nodes in the heap is the largest. Then, the root node is exchanged with the last node in the heap. Then adjust the preceding (n-1) number to make it heap. Wait until there are only two nodes in the heap and exchange them. Finally, an ordered sequence of n nodes is obtained. According to the algorithm description, heap sorting requires two processes: creating a heap and switching the last element of the heap. Therefore, heap sorting consists of two functions. One is the heap build penetration function, and the other is the function that calls the penetration function repeatedly to implement sorting.

2. Instance

Initial Sequence :,

Heap creation:

Swap, maximum number of kicks out from the heap

And so on: The last two remaining nodes in the last heap are exchanged, kicked out, and sorted.

3. Algorithm Implementation:

Public class HeapSort {public static void main (String [] args) {int [] a = {,}; int arrayLength =. length; // cyclically build heap for (int I = 0; I <arrayLength-1; I ++) {// build heap buildMaxHeap (a, arrayLength-1-i ); // swap heap top and last element swap (a, 0, arrayLength-1-i); System. out. println (Arrays. toString (a) ;}}// create a public static void buildMaxHeap (int [] data, int lastIndex) for the data array from 0 to lastIndex) {// start from the parent node of the node (last node) at lastIndex for (int I = (lastIndex-1)/2; I> = 0; I --) {// k Save the node int k = I; // if the child node of the current k node has a while (k * 2 + 1 <= lastIndex) {// index of the Left subnode of k node int biggerIndex = 2 * k + 1; // If biggerIndex is smaller than lastIndex, that is, if (biggerIndex <lastIndex) {// if the value of the right subnode is large, if (data [biggerIndex] <data [biggerIndex + 1]) {// biggerIndex always records the index of a large subnode biggerIndex ++ ;}} // if the value of a k node is smaller than the value of a large sub-node, if (data [k] <data [biggerIndex]) {// exchange their swap (data, k, biggerIndex); // assign biggerIndex to k to start the next loop of the while loop, and re-ensure that the value of k nodes is greater than the value of its left and right subnodes k = biggerIndex ;} else {break ;}}}// exchange private static void swap (int [] data, int I, int j) {int tmp = data [I]; data [I] = data [j]; data [j] = tmp ;}}

 

Bubble sort, Quick Sort To view: http://www.cnblogs.com/0201zcr/p/4763806.html

Select sort, insert sort, Hill sort to view: http://www.cnblogs.com/0201zcr/p/4764427.html

Thank you! Thank you for your patience!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.