java merge_Java MergeSort

/**

*

*

*

Copyright 1994-2018 JasonInternational

*

All rights reserved.

*

Created on 2018年4月10日

*

Created by Jason

*

*

*/

package cn.ucaner.algorithm.sorts;

/**

* Merge sort is an O(n log n) comparison-based sorting algorithm. Most

* implementations produce a stable sort, which means that the implementation

* preserves the input order of equal elements in the sorted output.

*

* Family: Merging.

* Space: In-place.

* Stable: True.

*

* Average case = O(n*log n)

* Worst case = O(n*log n)

* Best case = O(n*log n)

*

* @see Merge Sort (Wikipedia)

*

* @author Justin Wetherell

*/

@SuppressWarnings("unchecked")

public class MergeSort> {

public static enum SPACE_TYPE { IN_PLACE, NOT_IN_PLACE }

private MergeSort() { }

public static > T[] sort(SPACE_TYPE type, T[] unsorted) {

sort(type, 0, unsorted.length, unsorted);

return unsorted;

}

private static > void sort(SPACE_TYPE type, int start, int length, T[] unsorted) {

if (length > 2) {

int aLength = (int) Math.floor(length / 2);

int bLength = length - aLength;

sort(type, start, aLength, unsorted);

sort(type, start + aLength, bLength, unsorted);

if (type == SPACE_TYPE.IN_PLACE)

mergeInPlace(start, aLength, start + aLength, bLength, unsorted);

else

mergeWithExtraStorage(start, aLength, start + aLength, bLength, unsorted);

} else if (length == 2) {

T e = unsorted[start + 1];

if (e.compareTo(unsorted[start]) < 0) {

unsorted[start + 1] = unsorted[start];

unsorted[start] = e;

}

}

}

private static > void mergeInPlace(int aStart, int aLength, int bStart, int bLength, T[] unsorted) {

int i = aStart;

int j = bStart;

int aSize = aStart + aLength;

int bSize = bStart + bLength;

while (i < aSize && j < bSize) {

T a = unsorted[i];

T b = unsorted[j];

if (b.compareTo(a) < 0) {

// Shift everything to the right one spot

System.arraycopy(unsorted, i, unsorted, i+1, j-i);

unsorted[i] = b;

i++;

j++;

aSize++;

} else {

i++;

}

}

}

private static > void mergeWithExtraStorage(int aStart, int aLength, int bStart, int bLength, T[] unsorted) {

int count = 0;

T[] output = (T[]) new Comparable[aLength + bLength];

int i = aStart;

int j = bStart;

int aSize = aStart + aLength;

int bSize = bStart + bLength;

while (i < aSize || j < bSize) {

T a = null;

if (i < aSize) {

a = unsorted[i];

}

T b = null;

if (j < bSize) {

b = unsorted[j];

}

if (a != null && b == null) {

output[count++] = a;

i++;

} else if (b != null && a == null) {

output[count++] = b;

j++;

} else if (b != null && b.compareTo(a) <= 0) {

output[count++] = b;

j++;

} else {

output[count++] = a;

i++;

}

}

int x = 0;

int size = aStart + aLength + bLength;

for (int y = aStart; y < size; y++) {

unsorted[y] = output[x++];

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值