Topic: 归并排序,一个让我郁闷了2晚的问题

  Print this page

1.归并排序,一个让我郁闷了2晚的问题 Copy to clipboard
Posted by: yqf0215
Posted on: 2005-12-01 21:15

这个代码也是对的,但是运行了就出错。
我的是eclipe 3.2M3,JDK 1.5,debug时发现,merge()中的j总是莫名奇妙的增加很快。

程序代码如下:

public class MergeSort {

private int[] theArray = { 3, 1, 2, 4};

private int size;

private int[] workspace;

public MergeSort( ) {

}

public MergeSort(int size) {
this.size = size;
}

public void mergeIt() {
workspace = new int[theArray.length];
recMerge(workspace, 0, theArray.length-1);
}

public void recMerge(int[] workspace, int begin, int end) {
if (begin == end)
return;

int mid = (begin + end) / 2;
recMerge(workspace, begin, mid);
recMerge(workspace, mid + 1, end);
merge(workspace, begin, mid + 1, end);

}

public void merge(int[] workspace, int begin, int up, int end) {
//就是这个j,老是莫名其妙的增加
int j = 0;
int n = end-begin+1;
int mid = up-1;

while (begin <= mid && up <= end)
if (theArray[begin] < theArray[up])
workspace[j++] = theArray[begin++];
else
workspace[j++] = theArray[up++];

while (begin <= mid)
workspace[j++] = theArray[begin++];

while (up <= end)
workspace[j++] = theArray[up++];

for( j=0; j<n; j++)
theArray[begin+j]=workspace[j];
}

public void output() {
for (int i : theArray)
System.out.println(i);

}

/**
* @param args
*/
public static void main(String[] args) {
MergeSort s = new MergeSort();
s.mergeIt();
s.output();
}

}

请各位大侠帮帮我吧,试了半天都不行啊!!!

2.Re:归并排序,一个让我郁闷了2晚的问题 [Re: yqf0215] Copy to clipboard
Posted by: fly_fish_2005
Posted on: 2006-01-09 15:53

我解决了j增加的问题,但是不知道为什么打印出来的数组是我们初始化时的数组,而非排序后的数组,请牛人指教! 下边是我的代码:
public void merge(int theArray[],int[] workspace, int begin, int up, int end) {
// 就是这个j,老是莫名其妙的增加
int j = 0;
int n = end-begin+1;
int mid = up-1;
int temp=0;
temp=begin;
while (begin <= mid && up <= end){
if (theArray[begin] < theArray[up]){
workspace[j++] = theArray[begin++];
}else{
workspace[j++] = theArray[up++];
}
}
while (begin <= mid) {
workspace[j++] = theArray[begin++];
}
while (up <= end){
workspace[j++] = theArray[up++];
}
for( ;j<n; j++) {
theArray[temp++]=workspace[j];
}

}

3.Re:归并排序,一个让我郁闷了2晚的问题 [Re: yqf0215] Copy to clipboard
Posted by: ranchgirl
Posted on: 2006-01-09 23:27

Do a search on "Merge Sort Java", you will find many ready-to-run source code...Big Smile

4.Re:归并排序,一个让我郁闷了2晚的问题 [Re: yqf0215] Copy to clipboard
Posted by: suntao19830709
Posted on: 2006-02-15 16:26

归并排序我没写,以前我写过一个快速排序的方法,基本思想应该差不多的。下面贴出来,希望能帮上忙。

/**
* QuickOrder
* @param num
*/
public void doQuickOrder(int[] num) {
if (num.length > 1) {
//设置无序列的第一个元素为中间参考元素
int middle = num[0];

//----得到比中间元素小的元素的个数----begin//
int minNum = 0;
for (int i = 1; i < num.length; i++) {
if (num[i] < num[0]) {
minNum++;
}
}
//----得到比中间元素小的元素的个数----end//

//----分别创建比中间元素小的元素序列和比中间元素大(包括等于)的元素序列并赋值begin//
int[] minArr = new int[minNum];
int[] maxArr = new int[num.length - minNum - 1];
int numMinArr = 0;
int numMaxArr = 0;
for (int i = 1; i < num.length; i++) {
if (num[i] < num[0]) {
minArr[numMinArr] = num[i];
numMinArr++;
} else {
maxArr[numMaxArr] = num[i];
numMaxArr++;
}
}
//----分别创建比中间元素小的元素序列和比中间元素大(包括等于)的元素序列并赋值end//

//----对小序列和大序列分别递归执行快速排序begin//
doQuickOrder(minArr);
doQuickOrder(maxArr);
//----对小序列和大序列分别递归执行快速排序end//

//----将最终结果按照 小序列+中间元素+大序列 的顺序合并begin//
for (int i = 0; i < minNum; i++) {
num[i] = minArr[i];
}
num[minNum] = middle;
for (int i = 0; i < maxArr.length; i++) {
num[minNum + 1 + i] = maxArr[i];
}
//----将最终结果按照 小序列+中间元素+大序列 的顺序合并end//
}
}


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923