Mergesort com String

0 respostas
C

Olá pessoal, estou com um problema, estou com um trabalho p entregar na segunda-feira, eu deve fazer um mergesort com strings, porém so consigo achar alguns na net com inteiro, e quando converto p string, senchi de erro, gosdtaria de saber se alguem pode me ajudar, o codigo q eu estava tentando mudar foi esse:
import java.util.Vector;

public class MergeSort {

private KeyboardReader keyboard;

private Vector numbers;
/*
 * Constructor 
 */
public MergeSort() {
	this.keyboard = new KeyboardReader();
	this.createArray();	
	System.out.println("Your array is " + this.numbers.toString());
	this.Mergesort(this.numbers, 0, this.numbers.size()-1);
	System.out.println("Merge Sort Answer: "); //Display the string.
	System.out.println(this.numbers.toString());
}

/*
 * Create a array numbers[i]
 */
private void createArray() {
	int n = this.keyboard.readInt("How many integers do you want to sort? ");
	System.out.println("(If you didn't input integers, system would ask you to input again.)");
	this.numbers = new Vector(n);
	for (int i = 0; i < n; i++){
		int x = this.keyboard.readInt("Please give No. " + i + ": ");
		this.numbers.add(i, new Integer(x));
	}
}

/*
 * Divide and Conquer: recurrence
 */
private void Mergesort(Vector A, int p, int r) {
	if (p < r){
		int q = (p + r) / 2;
		//System.out.println("Recursive begins: ");
		//System.out.println("p is " + p + " . q is " + q + ". r is " + r);
		Mergesort(A, p, q);
		Mergesort(A, q + 1, r);
		Merge(A, p, q, r);
	}
}

/*
 * Merge two sorted array: takes Big Theta (n)
 */
private void Merge(Vector A, int p, int q, int r){
	int i = 0;
	int j = 0;
	//create arrays L[1..n1 + 1] and R[1..n2 + 1]
	Vector left = new Vector();
	Vector right = new Vector();
	for (int n = 0; n < q-p+1; n++){
		 left.add(A.get(p + n));
	}
	for (int n = 0; n < r-q; n++){
		 right.add(A.get(q+n+1));
	}
	//add the sentinel value to left and right array
	left.add(new Integer(100));
	right.add(new Integer(200));
	
	for (int k = p; k <= r; k++){
		if (((Integer)left.get(i)).compareTo((Integer)right.get(j)) <= 0 ){
			A.set(k, left.get(i));
			i++;
		} 
		else {
			A.set(k, right.get(j));
			j++;
		}
		//System.out.println(A.toString());
	}
}
/*
 * Main method
 */
public static void main(String[] args) {
	new MergeSort();
}

}

essa classe aqui é p ler os tipos dados, entre eles tem o de string, mas na hora da ordenação ele  erro:

import <a href="http://java.io">java.io</a>.*;

public class KeyboardReader extends Object{
private BufferedReader keyboardReader;
//creates a new KeyboardReader
public KeyboardReader(){
this.keyboardReader = new BufferedReader(new InputStreamReader(System.in));
}

/*Prints out the prompt message on the screen and reads as an answer the entered character
 *string. The character string includes all characters that the user writes on one line 
 *before pressing Enter*/
public String readString(String prompt){
System.out.print(prompt);
String input = null;
try {
    input = this.keyboardReader.readLine();
    
} catch (IOException error) {
}
return input; 
}

/*Prints out prompt message on the screen and reads as an answer the entered integer. 
 *If the input doesn't count as a valid integer value, the prompt is repeated. */

public int readInt(String prompt){
int bool = 0;
int input = 0;
while (bool == 0) {
    try{
	input = Integer.parseInt(this.readString(prompt));
	bool = 1;
    }catch(Exception error){
    }
}
return input;
}

/*Prints out the prompt message on the screen and reads as an answer the entered double. 
 *If the input doesn't count as a valid double value, the prompt is repeated.*/

public double readDouble(String prompt){
int bool = 0;
double input = 0;
while (bool == 0) {
    try{
	input = Double.parseDouble(this.readString(prompt));
	bool = 1;
    }catch(Exception error){
    }
}
return input;
}

/*Prints out the prompt message on the screen and reads as an answer the entered character. 
 *If the user enters several symbols, only the first one is returned. If the user enters 
 *an empty character string, the prompt is repeated.*/


public char readChar(String prompt){
int bool = 0;
String input = this.readString(prompt);
while (input.equals("")){
    input = this.readString(prompt);
}
char st = input.charAt(0);
return st;
}

}

Criado 9 de novembro de 2006
Respostas 0
Participantes 1