Merge sort and optimization (Java implementation)

Source: Internet
Author: User
Tags comparable

General Merge Sort
public class MergeSort {/** * @param arr array to sort * @param left edge of this merge @param the middle position of this merge in mid, which is the dividing line * @param right boundary of this merge * @param <T> generics * @local aux auxiliary space (auxiliary spaces) */private static <t extends Comparable<? Super t>> void Merge (t[] arr, int left, int mid, Int. right) {t[] aux = Java.util.Arrays.copyOfRange (arr, lef        T, right + 1); Aux,i J is the starting pointer of the two halves respectively.        Merge the two closed zones [left ... mid] [mid + 1 ... right] int i = left;        Int J = mid + 1; for (int t = leave; t <= right; t++) {//to overwrite the [left...right] zone in the ARR array, it's over if (I > Mid) {//i = = Mid + 1 o'clock more            Bounds (jump out of the left half) arr[t] = aux[j++-Ieft];            } else if (J > right) {//j = R + 1 O'Clock out-of-bounds (jumps out half of the group) arr[t] = aux[i++-left]; } else if (Aux[i-left].compareto (Aux[j-left]) < 0) {//If I-left is small, then insert.            (The pointer to the left half of the array refers to a small number of pointers) arr[t] = aux[i++-to]; } else {//If J-left is small, then insert. (RightThe number of pointers to the half array is small) arr[t] = aux[j++-left]; }}} private static <t extends Comparable<?        Super t>> void Sort (t[] arr, int left, int. right) {if (left >= right) {return;        } int mid = (left + right)/2;        Sort (arr, left, mid);        Sort (arr, mid + 1, right);    Merge (arr, left, Mid, right); } public static <t extends Comparable<?    Super t>> void Sort (t[] arr) {sort (arr, 0, arr.length-1);            } private static void Printarr (object[] arr) {for (Object O:arr) {System.out.print (o);        System.out.print ("\ t");    } System.out.println ();        } public static void Main (String args[]) {integer[] arr = {3, 5, 1, 7, 2, 9, 8, 0, 4, 6};        Printarr (arr);//3 5 1 7 2 9 8 0 4 6 sort (arr); Printarr (arr);//0 1 2 3 4 5 6 7 8 9}}

  

Merge optimization: Using Insert sorting

When recursive to scale enough hours, use insert sort

public class MergeSort {/** * @param arr array to sort * @param left edge of this merge @param the middle position of this merge in mid, which is the dividing line * @param right boundary of this merge * @param <T> generics * @local aux auxiliary space (auxiliary spaces) */private static <t extends Comparable<? Super t>> void Merge (t[] arr, int left, int mid, Int. right) {t[] aux = Java.util.Arrays.copyOfRange (arr, lef        T, right + 1); Aux,i J is the starting pointer of the two halves respectively.        Merge the two closed zones [left ... mid] [mid + 1 ... right] int i = left;        Int J = mid + 1; for (int t = leave; t <= right; t++) {//to overwrite the [left...right] zone in the ARR array, it's over if (I > Mid) {//i = = Mid + 1 o'clock more            Bounds (jump out of the left half) arr[t] = aux[j++-Ieft];            } else if (J > right) {//j = R + 1 O'Clock out-of-bounds (jumps out half of the group) arr[t] = aux[i++-left]; } else if (Aux[i-left].compareto (Aux[j-left]) < 0) {//If I-left is small, then insert.            (The pointer to the left half of the array refers to a small number of pointers) arr[t] = aux[i++-to]; } else {//If J-left is small, then insert. (RightThe number of pointers to the half array is small) arr[t] = aux[j++-left];     }}}/** * @param arr takes an insertion sort for arr when it is small * @param left edge for sorted parts * @param right edge of the part to be sorted * @param <T> generics */private static <t extends Comparable<?            Super t>> void Insertionsort (t[] arr, int left, int. right) {for (int i = left + 1; I <= right; i++) {            T temp = arr[i];            int j = i-1;                while (J >= 0 && Temp.compareto (arr[j]) < 0) {arr[j + 1] = Arr[j];            j--;        } arr[j + 1] = temp; }} private static <t extends Comparable<? Super t>> void Sort (t[] arr, int left, int. right) {if (Right-left < 7) {//Insert sort on small data inse            Rtionsort (arr, left, right);        Return        } int mid = (left + right)/2;        Sort (arr, left, mid);        Sort (arr, mid + 1, right);    Merge (arr, left, Mid, right); } PubLic static <t extends Comparable<?    Super t>> void Sort (t[] arr) {sort (arr, 0, arr.length-1);            } private static void Printarr (object[] arr) {for (Object O:arr) {System.out.print (o);        System.out.print ("\ t");    } System.out.println ();        } public static void Main (String args[]) {integer[] arr = {3, 5, 1, 7, 2, 9, 8, 0, 4, 6};        Printarr (arr);//3 5 1 7 2 9 8 0 4 6 sort (arr); Printarr (arr);//0 1 2 3 4 5 6 7 8 9}}

  

merge sort continue optimization: Before merging, decide if it is necessary to merge
public class MergeSort {/** * @param arr array to sort * @param left edge of this merge @param the middle position of this merge in mid, which is the dividing line * @param right boundary of this merge * @param <T> generics * @local aux auxiliary space (auxiliary spaces) */private static <t extends Comparable<? Super t>> void Merge (t[] arr, int left, int mid, Int. right) {t[] aux = Java.util.Arrays.copyOfRange (arr, lef        T, right + 1); Aux,i J is the starting pointer of the two halves respectively.        Merge the two closed zones [left ... mid] [mid + 1 ... right] int i = left;        Int J = mid + 1; for (int t = leave; t <= right; t++) {//to overwrite the [left...right] zone in the ARR array, it's over if (I > Mid) {//i = = Mid + 1 o'clock more            Bounds (jump out of the left half) arr[t] = aux[j++-Ieft];            } else if (J > right) {//j = R + 1 O'Clock out-of-bounds (jumps out half of the group) arr[t] = aux[i++-left]; } else if (Aux[i-left].compareto (Aux[j-left]) < 0) {//If I-left is small, then insert.            (The pointer to the left half of the array refers to a small number of pointers) arr[t] = aux[i++-to]; } else {//If J-left is small, then insert. (RightThe number of pointers to the half array is small) arr[t] = aux[j++-left];     }}}/** * @param arr takes an insertion sort for arr when it is small * @param left edge for sorted parts * @param right edge of the part to be sorted * @param <T> generics */private static <t extends Comparable<?            Super t>> void Insertionsort (t[] arr, int left, int. right) {for (int i = left + 1; I <= right; i++) {            T temp = arr[i];            int j = i-1;                while (J >= 0 && Temp.compareto (arr[j]) < 0) {arr[j + 1] = Arr[j];            j--;        } arr[j + 1] = temp; }} private static <t extends Comparable<? Super t>> void Sort (t[] arr, int left, int. right) {//commented out first, for testing//if (Right-left < 7) {//For small-scale data in            Line Insert sort//Insertionsort (arr, left, right);//return;//} if (left >= right) {        Return        } int mid = (left + right)/2; Sort (arr, left, MID);        Sort (arr, mid + 1, right);        Before merging, if the largest on the left is smaller (or equal) than the smallest on the right, then it is not necessary to merge and be ordered.        if (Arr[mid].compareto (Arr[mid + 1]) <= 0) {return;    } merge (arr, left, Mid, right); } public static <t extends Comparable<?    Super t>> void Sort (t[] arr) {sort (arr, 0, arr.length-1);            } private static void Printarr (object[] arr) {for (Object O:arr) {System.out.print (o);        System.out.print ("\ t");    } System.out.println ();        } public static void Main (String args[]) {integer[] arr = {3, 5, 1, 7, 2, 9, 8, 0, 4, 6};        Printarr (arr);//3 5 1 7 2 9 8 0 4 6 sort (arr); Printarr (arr);//0 1 2 3 4 5 6 7 8 9}}

  

Merge sort and optimization (Java implementation)

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.