分治算法——归并排序的java实现

1.分治算法的基本思想是将一个规模为N的问题分解为K个规模较小的子问题,这些子问题相互独立且与原问题性质相同。求出子问题的解,就可得到原问题的解。
分治法解题的一般步骤:
(1)分解,将要解决的问题划分成若干规模较小的同类问题;
(2)求解,当子问题划分得足够小时,用较简单的方法解决;
(3)合并,按原问题的要求,将子问题的解逐层合并构成原问题的解。

2.归并排序:是建立在归并操作上的一种有效的排序算法。该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。

MergerSorter.java



/**
* 归并操作的实现类
*/

public class MergerSorter {

private static final int[] ARRAY = {2, 5, 10, 30, 60, 40, 5, 6, 66};

public int[] sort(int[] array) {
if (array.length == 1)
return array;
final int dividePos = array.length / 2;
int[] array1 = new int[dividePos];
System.arraycopy(array, 0, array1, 0, array1.length);
int[] array2 = new int[array.length - dividePos];
System.arraycopy(array, dividePos, array2, 0, array2.length);
return merge(sort(array1), sort(array2));
}

public int[] merge(int[] a1, int[] a2) {
int[] result = new int[a1.length + a2.length];
int cursor = 0;
int cursor1 = 0;
int cursor2 = 0;
while (cursor1 < a1.length && cursor2 < a2.length) {
if (a1[cursor1] > a2[cursor2]) {
result[cursor++] = a2[cursor2++];
} else {
result[cursor++] = a1[cursor1++];
}
}

while (cursor1 < a1.length) {
result[cursor++] = a1[cursor1++];
}

while (cursor2 < a2.length) {
result[cursor++] = a2[cursor2++];
}

return result;
}

public static void main(String args[]) {
int[] r = new MergerSorter().sort(ARRAY);
for (int t : r) {
System.out.print(t + ",");
}
}
}



MergerSorterTest.java

import static org.junit.Assert.assertArrayEquals;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
* TestCase of MergerSorter
*/

public class MergerSorterTest {

private MergerSorter mergerSorter = null;

@Before
public void setUp() throws Exception {
mergerSorter = new MergerSorter();
}

@After
public void tearDown() throws Exception {
mergerSorter = null;
}

@Test
public void merge() {
int[] a1 = {2, 6, 8, 9};
int[] a2 = {5, 8, 10, 12};
int[] expected = {2, 5, 6, 8, 8, 9, 10, 12};
assertArrayEquals(expected, mergerSorter.merge(a1, a2));
}

@Test
public void sort() {
int[] srcArray = {9, 12, 6, 10, 8, 2, 8, 5};
int[] expected = {2, 5, 6, 8, 8, 9, 10, 12};

assertArrayEquals(expected, mergerSorter.sort(srcArray));
}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值