Merge and sort animation of Wing

Source: Internet
Author: User

A year ago, I wrote the merge wing sorting animation, which was a merge wing sorting animation. At that time, because I did not have a deep understanding of the Merge Sorting, I always thought that the Merge Sorting requires additional array space. At that time, I always wanted to write a Merge Sorting animation, unfortunately, it never succeeded. Later, I gave up.

When I looked at the introduction to algorithms yesterday, I saw the pseudo code of Merge Sorting, which suddenly inspired me. This algorithm may be able to run the Merge Sorting in the previous sorting animation.

The Merge Sorting Algorithm is as follows:

Private Static void mergesort (INT [] data, int start, int end) {If (start <End) {int mid = (start + end)/2; mergesort (data, start, mid); mergesort (data, Mid + 1, end); merge (data, start, mid, end) ;}} Private Static void merge (INT [] data, int start, int mid, int end) {int capacity1 = mid-start + 1; int capacity2 = end-mid; int dataright [] = new int [capacity1 + 1]; int dataleft [] = new int [capacity2 + 1]; int I, j = 0; for (I = 0; I <capacity1; I ++) {dataright [I] = data [start + I];} dataright [I] = 10000; // The value of the Sentinel, take a value greater than any number in the data for (j = 0; j <capacity2; j ++) {dataleft [J] = data [Mid + J + 1];} dataleft [J] = 10000; // The value of the Sentinel, int Indexa = 0; int indexb = 0; // notice the arrayindexoutofbounds, first we must check the Indexa and indexbtry {for (int K = start; k <End + 1; k ++) {If (dataright [Indexa] <= dataleft [indexb]) {data [k] = dataright [Indexa]; mergehistogram. showhistogram (data, k); thread. sleep (20); Indexa ++;} else if (dataright [Indexa]> dataleft [indexb]) {data [k] = dataleft [indexb]; mergehistogram. showhistogram (data, k); thread. sleep (20); indexb ++;} mergehistogram. showhistogram (data);} catch (interruptedexception ex) {ex. printstacktrace ();}}

Why should the Sentinel value be a larger number than the sorted data? Why is the declared dataright and dataleft array length 1 longer than capacity?

Everything is for the following merging process

for(int k = start; k < end + 1; k++){if(dataRight[indexA] <= dataLeft[indexB] ){data[k] = dataRight[indexA];mergeHistogram.showHistogram(data, k);Thread.sleep(20);indexA++;}else if(dataRight[indexA] > dataLeft[indexB]){data[k] = dataLeft[indexB];mergeHistogram.showHistogram(data, k);Thread.sleep(20);indexB++;}}

If the length of dataright and dataleft is capacity, if (dataright [Indexa] <= dataleft [indexb]) will always cross the border once and only cross the border once, an exception is thrown during each operation, so we declare that the array length is capacity + 1. Although the last data is not used, we can compare it by the maximum value of the Last sentinel, the merging process merges the elements of two ordered arrays into an ordered array in ascending order. Therefore, when the for loop iterates from start to end + 1, iteration stops, at this time, the guard value will not be merged, so it does not affect the sorting result, and can easily avoid the array out-of-bounds problem.

Running Effect



Source code merge sort animation source code

Runable jar merge sort runable jar

Merge video addresses and sort animated videos

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.