Common sorting algorithms (I) mergesort

Source: Internet
Author: User

The algorithm concept is very important. common algorithms that use the divide and conquer idea include fast sorting, merging, binary search, and big integer multiplication (see http://blog.csdn.net/com_stu_zhang/article/details/7233761,1)

Simply sort an array by merging

Idea: Simply put, as long as the left and right parts of an array are ordered, it is okay to simply merge them, the left and right parts can be further divided into the left and right parts-obviously, recursion is required.

Algorithm: Merge Sorting

1. Split the array into two parts: subarray1 and subarray2

2. sort by merging subarray1

3. Merge and sort subarray2

4. Merge subarray1 subarray2

 

Start with merging subarray.

    //By default. Ascending sort    public int [] mergeArray(int[] a, int []b){        int []merge = new int[a.length+b.length];                int i = 0;        int j = 0;        int index  = 0;        while(i<a.length && j < b.length){                        if(a[i] <= b[j]){                merge[index ++] = a [i ++];                            }else if(a[i] > b[j]){                merge[index ++] = b [j ++];            }                    }                //if  array a go to the end ,copy array b        if(i >= a.length){            while (j < b.length){                merge[index ++] = b [j ++];            }        }                //else copy array a        if( j >= b.length){            while ( i <a.length){                merge[index ++] = a [i ++];            }                    }                return merge;    }

The next step is to write and merge the sorting itself. It is obvious that there are recursive calls. We only need to consider simple implementation, not efficiency.

/*** * Merge Sort-----Divide and Conquer http://blog.csdn.net/com_stu_zhang/article/details/7233761 *           -----from step2 and step 3,we can find recursive * Step 1://divide into subarray1 and sub array2 * Step 2://MergeSort sub array1 * Step 3://MergeSort sub array2 * Step 4://Merge sub array1 and sub array2     * @param a * @param start * @param end  */        //Divide---merge     public void divideAndMerge(int[] a){        int mid  = a.length / 2;        new int left[] ;//copy  0 to mid         new int right[];//copy mid +1 to a.length -1                if( a.length == 1){            return ;        }else{            divideAndMerge(left);            divideAndMerge(right);            mergeArray(left,right);                    }            }

 

Then we need to consider the performance issue. The above code blocks obviously need to constantly new sub-arrays to store small arrays that have been partitiOned, while the new operation is performance-consuming.

Considering that all objects in Java are passed in reference, but our merge operations only need to provide the subscript position of the array, we can know which sub-array to merge (pay attention to the adjacent sub-array to be merged, in the case of wooden jumps), you only need to provide the starting subscript to split it into two sub-arrays for merging, and at the same time, it is enough for a temporary array to use the temporary storage space for sorting, therefore, the parameters of Merge Sorting and the parameters of merge methods are available.

 1     public void mergeArrayNew(int[] a, int start_pos,int end_pos,int[] tmp){ 2         int mid_pos  = (end_pos - start_pos) /2 + start_pos; 3         int i = start_pos; 4         int j = mid_pos + 1; 5         int index = start_pos; 6          7         while( i <= mid_pos && j <= end_pos){ 8             if(a[i] <= a[j]){ 9                 tmp[index] = a[i];10                 i++;11                 index ++;12             }else {13                 tmp[index] = a [j];14                 j++;15                 index ++;16             }17         }18         19         if(j >  end_pos){20             while(i <= mid_pos ){21                 tmp[index] = a[i];22                 i++;23                 index ++;24             }25         }26         if(i > mid_pos ){27             while ( j <= end_pos){28                 tmp[index] = a [j];29                 j++;30                 index ++;31             }32         }33         34         //attention!!!35         for(int copy = start_pos;copy<=end_pos;copy++){36             a[copy] = tmp[copy];37         }38         39     }40     41     //Divide---merge 42     public void divideAndMergeNew(int[] a,int start_pos,int end_pos,int[] tmp){43         int mid_pos  = (end_pos - start_pos) /2 + start_pos;44         45         if( start_pos == end_pos){46             return ;47         }else{48             divideAndMergeNew(a,start_pos,mid_pos,tmp);49             divideAndMergeNew(a,mid_pos + 1,end_pos,tmp);50             mergeArrayNew(a,start_pos,end_pos,tmp);            51         }        52     }

 

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.