java排序--归并排序

来源:互联网 发布:南京八大碗网络怎么样 编辑:程序博客网 时间:2024/04/29 19:30

1.概念:

归并(Merge)排序法是将两个(或两个以上)有序表合并成一个新的有序表,即把待排序序列分为若干个有序的子序列,再把有序的子序列合并为整体有序序列

归并排序是建立在归并操作上的一种有效的排序算法。该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。值得注意的是归并排序是一种稳定的排序方法。
将已有序的子序列合并,得到完全有序的序列;即先使每个子序列有序,再使子序列段间有序。若将两个有序表合并成一个有序,称为二路归并

2.归并操作:

归并操作(merge),也叫归并算法,指的是将两个顺序序列合并成一个顺序序列的方法。
如 设有数列{6,202,100,301,38,8,1}
初始状态:6,202,100,301,38,8,1
第一次归并后:{6,202},{100,301},{8,38},{1},比较次数:3;
第二次归并后:{6,100,202,301},{1,8,38},比较次数:4;
第三次归并后:{1,6,8,38,100,202,301},比较次数:4;
总的比较次数为:3+4+4=11,;
逆序数为14;

3.示例代码:

//排序器

 

/**

 * 排序器接口(策略模式将算法封装到具有共同接口的独立的类中使得它们可以相互替换)

 * 

 *

 */

 

public interface Sorter {

/**

 * 排序

 * @param list 待排序的数组

 */

public <T extends Comparable<T>> void sort(T[] list);

/**

 * 排序

 * @param list 待排序的数组

 * @param comp 比较两个对象的比较器

 */

public <T> void sort(T[] list, Comparator<T> comp);

}

 

public class MergeSorter implements Sorter {

 

@Override

public <T extends Comparable<T>> void sort(T[] list) {

T[] temp = (T[]) new Comparable[list.length];

mSort(list, temp, 0, list.length - 1);

}

private <T extends Comparable<T>> void mSort(T[] list, T[] temp, int low, int high) {

if(low == high) {

return ;

}

else {

int mid = low + ((high - low) >> 1);

mSort(list, temp, low, mid);

mSort(list, temp, mid + 1, high);

merge(list, temp, low, mid + 1, high);

}

}

private <T extends Comparable<T>> void merge(T[] list, T[] temp, int left, int right, int last) {

int j = 0;  

        int lowIndex = left;  

        int mid = right - 1;  

        int n = last - lowIndex + 1;  

        while (left <= mid && right <= last) {  

            if (list[left].compareTo(list[right]) < 0) {  

                temp[j++] = list[left++];  

            } else {  

                temp[j++] = list[right++];  

            }  

        }  

        while (left <= mid) {  

            temp[j++] = list[left++];  

        }  

        while (right <= last) {  

            temp[j++] = list[right++];  

        }  

        for (j = 0; j < n; j++) {  

            list[lowIndex + j] = temp[j];  

        }  

}

 

@Override

public <T> void sort(T[] list, Comparator<T> comp) {

T[] temp = (T[]) new Comparable[list.length];

mSort(list, temp, 0, list.length - 1, comp);

}

private <T> void mSort(T[] list, T[] temp, int low, int high, Comparator<T> comp) {

if(low == high) {

return ;

}

else {

int mid = low + ((high - low) >> 1);

mSort(list, temp, low, mid, comp);

mSort(list, temp, mid + 1, high, comp);

merge(list, temp, low, mid + 1, high, comp);

}

}

private <T> void merge(T[] list, T[] temp, int left, int right, int last, Comparator<T> comp) {

int j = 0;  

        int lowIndex = left;  

        int mid = right - 1;  

        int n = last - lowIndex + 1;  

        while (left <= mid && right <= last) {  

            if (comp.compare(list[left], list[right]) < 0) {  

                temp[j++] = list[left++];  

            } else {  

                temp[j++] = list[right++];  

            }  

        }  

        while (left <= mid) {  

            temp[j++] = list[left++];  

        }  

        while (right <= last) {  

            temp[j++] = list[right++];  

        }  

        for (j = 0; j < n; j++) {  

            list[lowIndex + j] = temp[j];  

        }  

}

 

}

 

0 0
原创粉丝点击