Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区 » 编程/算法/API  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 归并排序,一个让我郁闷了2晚的问题
yqf0215





发贴: 10
积分: 0
于 2005-12-01 21:15 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
这个代码也是对的,但是运行了就出错。
我的是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();
}

}

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


why edited on 2006-02-15 20:32


话题树型展开
人气 标题 作者 字数 发贴时间
7174 归并排序,一个让我郁闷了2晚的问题 yqf0215 1549 2005-12-01 21:15
5571 Re:归并排序,一个让我郁闷了2晚的问题 fly_fish_2005 737 2006-01-09 15:53
5380 Re:归并排序,一个让我郁闷了2晚的问题 ranchgirl 83 2006-01-09 23:27
5870 Re:归并排序,一个让我郁闷了2晚的问题 suntao19830709 1742 2006-02-15 16:26

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   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