Merge sort of Java common sorting algorithm

Source: Internet
Author: User

In the process of learning the algorithm, we will inevitably touch many and sort-related algorithms. All in all, the basic sorting algorithm must be mastered for any programmer.

Starting today, we're going to be explaining the basic sorting algorithm. Is it ready? Let ' s go~~~

1, the basic concept of sorting algorithm to explain

Time complexity: The number of times to compare the number of keywords that need to be sorted and the corresponding moves.

Spatial complexity: Analyze how much secondary memory is needed.

Stability: If you record A and B of two keywords whose values are equal, and if they are not exchanged in their relative positions after sorting, then we call this sort algorithm stable.

Otherwise we call this sort algorithm to be unstable.

Common classifications for sorting algorithms:

1, internal sorting (the most common sort of method, do not need to use third-party secondary storage tools)

2, external sorting (need to use external storage to assist with related sorting operations)

If the data element that participates in the sort is very much, the amount of data is very large, the computer cannot put the whole sort process into memory,

We have to use external memory such as disk to do, this sort of way, we call it external sort.

The most common of the external sort is the multi-way merge sort, the original file is decomposed into several parts that can be loaded into memory one at a time, each part is transferred into

The memory is sorted accordingly, and then the multiple sequential external files are sorted in a multi-merge order.

For most of us programmers, we often run into internal sorting. Next, we will explain the common internal ordering accordingly.

The internal ordering to be explained today is:

Merge Sort

1. A brief explanation of the basic concepts of merge sorting

Merge sort is an efficient sorting algorithm based on merge operation, which is a very typical application of divide-and-conquer method (Divide and Conquer). The ordered Subsequence is merged to obtain a fully ordered sequence, i.e., the order of each subsequence is ordered, and then the sequence of sub-sequences is ordered. If you merge two ordered tables into oneAn ordered table, called a two-way merge. The merging process is: compare A[i] and a[j] size, if A[I]≤A[J], then the first ordered table elements a[i] copied to r[k], and I and K respectively plus 1;Otherwise, the elements in the second ordered table A[j] are copied to R[k], and J and K are added 1 respectively, so loop until one of the ordered tables is finished, and thenThe remaining elements in another ordered table are copied to the cells in R from subscript K to subscript t. The algorithm of merging sort we usually use recursive implementation, first to sort the interval [s,t] tomidpoint two points, then the left sub-interval sort, and then the right sub-interval to sort, and finally the left and right intervals with a merge operation into an orderly interval [s,t]. Merge operation (merge), also called merge algorithm, refers to the method of merging two sequential sequences into a sequential sequence. If there is a series {6,202,100,301,38,8,1}Initial state: 6,202,100,301,38,8,1after first merge: {6,202},{100,301},{8,38},{1}, number of comparisons: 3;after the second merge: {6,100,202,301},{1,8,38}, number of comparisons: 4;after the third merge: {1,6,8,38,100,202,301}, number of comparisons: 4;the total number of comparisons is: 3+4+4=11,;the number of reverse order is;

2. Merge sorted Java code implementation

Package com.yonyou.test;/** * Merge sort with internal sorting algorithm * default to order from small to large * @author Small Hao * @ Created date 2015-3-27 */public class Test{public Stati c void Main (string[] args) {//array to be sorted int[] array=new int[]{8,3,2,1,7,4,6,5};//Output the contents of the original array Printresult (array);//Merge sort operation Sort (array,0,array.length-1);//output sorted results Printresult (array);} /** * Merge sort * @param array */private static void sort (int[] Array,int I,int j) {if (i<j) {int middle= (I+J)/2;//recursive processing related merge matters    Sort (array,i,middle); sort (array,middle+1,j); merge (array,i,middle,j);}}     /** * Merge Related array contents * At the same time make the merged array still ordered * @param array * @param i * @param middle * @param J * 4 5 6 9 * */private static void merge (int[] array, int i, int middle, int j) {//Create a temporary array to store the merged data int [] temp=new int[array.length];int m=i;int n=middle+1;int k=i;while (m<=middle&&n<=j) {if (array[m]< Array[n]) temp[k++]=array[m++];elsetemp[k++]=array[n++];} Process the remaining unincorporated portions while (m<=middle) {temp[k++]=array[m++];} while (n<=j) {temp[k++]=array[n++];} Stores the contents of the temporary array in the original array while (I&LT;=J) {array[i]=temp[i++];}} /** * * Output the result of the corresponding array * @param array */private static void Printresult (int[] Array      ) {for (int value:array) System.out.print ("" +value+ "); System.out.println ();} /** * Swap the values of two variables in the array * @param array * @param i * @param j */private static void Swap (int[] Array,int i,int j) {int temp=array[ I];array[i]=array[j];array[j]=temp;}}

  

Merge sort of Java common sorting algorithm

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.