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();
}
}
Output:
Enter a series of number with a space between each value: 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
Question:
Why it only sort each two queues next to each other, and just did once. I can't find out the place to fix it. Anyone helps me, please!!!
You have quite a few problems in your code. But taking the first issue first, the reason you are not processing all the data is an error in the following line:
while(loc < (S1.getQsize()-1))
The code inside the following block removes items from S1
so the size decreases with each iteration.
You could solve this by:
int size = S1.getQsize();
while (loc < size - 1)
Or (better)
while (!S1.emptyCheck)
However even if you process all the input data the sorting algorithm still has errors. Perhaps you could fix this problem, investigate further and then come back with a specific question if you get stuck.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.