【发布时间】:2017-05-03 05:44:17
【问题描述】:
public class NumberQueue {
private int firstLoc=0;
private int lastLoc=0;
private int[] numArray = new int[MAXSIZE];
public static final int MAXSIZE=100;
public int getQsize() {
return (MAXSIZE+lastLoc-firstLoc) % MAXSIZE;
}
public boolean fullCheck() {
return (getQsize() == MAXSIZE-1);
}
public boolean emptyCheck() {
return (firstLoc == lastLoc);
}
public int getValue(int loc) {
return numArray[loc];
}
public int front() {
return numArray[firstLoc];
}
public void remove() {
if (!emptyCheck())
firstLoc = (firstLoc + 1) % MAXSIZE;
}
public int insert(int val) {
if (fullCheck()) {
return -1;
} else {
numArray[lastLoc] = val;
lastLoc = (lastLoc +1) % MAXSIZE;
return 0;
}
}
public void display() {
int i = firstLoc;
while(i < lastLoc) {
System.out.print(numArray[i]+" ");
i++;
}
System.out.println();
}
}
public class NumberQueueMain {
public static void main(String[] args) {
//Create three queues
NumberQueue S1 = new NumberQueue();
NumberQueue S2 = new NumberQueue();
NumberQueue S3 = new NumberQueue();
//Get the array of queues
Scanner S = new Scanner(System.in);
System.out.println("Enter a series of number with a space between each value: ");
String str = S.nextLine();
String[] arr = str.split(" ");
int array[] = new int[arr.length];
for(int i = 0; i < arr.length; i++) {
array[i] = Integer.parseInt(arr[i]);
}
//Put the array into the NumberQueues-S1.
for(int i = 0; i < arr.length; i++) {
S1.insert(array[i]);
}
//Merge Sort S1
int loc = -1;
while(loc < (S1.getQsize()-1)) {
do {
S2.insert(S1.front());
S1.remove();
loc++;
} while(S1.getValue(loc) < S1.getValue(loc+1) && loc < S1.getQsize()-1);
S2.display();
while(loc < S1.getQsize()-1) {
do{
S3.insert(S1.front());
S1.remove();
loc++;
} while(S1.getValue(loc) < S1.getValue(loc+1) && loc < S1.getQsize()-1);
S3.display();
}
while(!S2.emptyCheck() && !S3.emptyCheck()) {
if(S2.front() > S3.front()) {
S1.insert(S3.front());
S3.remove();
} else if(S2.front()==S3.front()) {
S1.insert(S2.front());
S1.insert(S3.front());
S2.remove();
S3.remove();
} else {
S1.insert(S2.front());
S2.remove();
}
}
if (S2.emptyCheck()){
int sizeS3 = S3.getQsize();
for(int i = 0;i < sizeS3; i++){
S1.insert(S3.front());
S3.remove();
}
} else if(S3.emptyCheck()){
int sizeS2 = S2.getQsize();
for(int i = 0;i < sizeS2; i++) {
S1.insert(S2.front());
S2.remove();
}
}
}
System.out.println();
S1.display();
}
}
输出:
输入一系列数字,每个值之间有一个空格:3 5 7 2 3 5 11 34 12 10 15 18 3 12 17 22 12 18 25 22 30 3 5 7 2 3 5 11 34 2 3 5 11 34 12 2 3 5 11 34 12 10 15 18 3 12 17 22 12 18 25 22 30
2 3 3 5 5 7 11 34 12 10 15 3 12 17 18 22 12 18 25 22 30
问题:
为什么它只对彼此相邻的每两个队列进行排序,并且只做了一次。我找不到修复它的地方。任何人都可以帮助我,请!!!
【问题讨论】:
-
我认为不需要添加“android”标签。不是吗?
-
我可能只是用谷歌搜索算法。看起来你付出了很大的努力,但是这段代码太混乱了,无法理解。
-
我的教授希望我们通过使用队列和给定的类(即“类 NumberQueue”)来编写合并排序。在 Google 上找不到。
-
您好,欢迎来到 SO。我想帮助您改写您的问题“为什么它只对彼此相邻的每两个队列进行排序,并且只做了一次。” 很不清楚您在问什么。如果您可以通过“将两个队列彼此相邻排序”或“只做了一次”来解释您的意思,那么我们可能会提供更多帮助。
-
输出结果应该是有序的。但是,它只对前两次运行进行排序;对第三次和第四次运行进行排序;第五和第六;第七和第八。最后在合并两次运行后合并这些运行。所以结果表明它只对堆栈的一半进行了排序。我找不到修复它的地方。请大家帮帮我!!!
标签: java arrays queue mergesort