[英]Trouble implementing quicksort recursion in java
我正在為學校做一個項目,我必須測試拼寫檢查實現的二進制搜索與線性搜索的速度。 我們必須使用此特定的SpellCheck。 我的教授為使用遞歸進行binary
搜索提供的示例是為int設計的一些單獨的代碼片段,他希望我們使它適用於帶有字符串的此實現。 任何幫助,我們將不勝感激。 SortedWordList
是我需要幫助的類。
import java.util.Scanner;
public class Spellcheck {
private static WordList listOfWords;// The list of correctly spelled words.
public static void main(String[] args) {
long start = System.nanoTime();
listOfWords = new SortedWordList();
//listOfWords = new WordList();
long end = System.nanoTime();
long WL = (end-start);
//end = 0;
//start = 0;
long SW = 0;
Scanner in = new Scanner(System.in);
while (true) { // Get and process one word from the user.
System.out.println();
System.out.print("Enter a word to be cheched (press return to end): ");
String word = in.nextLine().trim().toLowerCase();
start = System.nanoTime();
if (word.length() == 0)
break;
if (listOfWords.contains(word)) {
System.out.println("'" + word + "' is a legal word.");
}
else {
System.out.println("'" + word + "' is not a legal word.");
System.out.println("If there are similar words, they are shown here:");
trySubstitute(word);
tryInsert(word);
tryDelete(word);
trySwap(word);
tryBreak(word);
end = System.nanoTime();
SW = (end-start);
}
System.out.println();
System.out.printf("Time to create word list: %,d nanoseconds.%n",(WL));
System.out.println();
System.out.printf("Time to test for similar words: %,d nanoseconds.%n",(SW));
System.out.println();
System.out.printf("Time to test total: %,d nanoseconds.%n",(SW + WL));
}
}
private static void trySubstitute(String word) {
for (int i = 0; i < word.length(); i++) {
String before = word.substring(0, i);
String after = word.substring(i+1);
for (char ch = 'a'; ch < 'z'; ch++) {
String newword = before + ch + after;
if (listOfWords.contains(newword))
System.out.println(" " + newword);
}
}
}
private static void tryInsert(String word) {
for (int i = 0; i <= word.length(); i++) {
String before = word.substring(0,i);
String after = word.substring(i);
for (char ch = 'a'; ch < 'z'; ch++) {
String newword = before + ch + after;
if (listOfWords.contains(newword))
System.out.println(" " + newword);
}
}
}
private static void tryDelete(String word) {
for (int i = 0; i < word.length(); i++) {
String before = word.substring(0, i);
String after = word.substring(i+1);
String newword = before + after;
if (listOfWords.contains(newword))
System.out.println(" " + newword);
}
}
private static void trySwap(String word) {
for (int i = 0; i < word.length() - 1; i++) {
String before = word.substring(0, i);
char a = word.charAt(i);
char b = word.charAt(i+1);
String after = word.substring(i+2);
String newword = before + b + a + after;
if (listOfWords.contains(newword))
System.out.println(" " + newword);
}
}
private static void tryBreak(String word) {
for (int i = 1; i < word.length() - 1; i++) {
String before = word.substring(0, i);
String after = word.substring(i);
if (listOfWords.contains(before) && listOfWords.contains(after))
System.out.println(" " + before + " " + after);
}
}
} // end class Spellcheck
`
import java.util.ArrayList;
import java.util.Scanner;
import java.net.URL;
public class WordList {
protected final String[] words; // the list of words
public WordList() {
try {
URL listLocation = WordList.class.getClassLoader().getResource("unsorted_words.txt");
Scanner in = new Scanner( listLocation.openStream() );
ArrayList<String> wordList = new ArrayList<String>();
while (in.hasNextLine())
wordList.add(in.nextLine());
words = wordList.toArray(new String[] {});
in.close();
}
catch (Exception e) {
throw new IllegalStateException("Can't load list of words from file 'unsorted_words.txt'");
}
}
public boolean contains(String lowerCaseWord) {
for (String s : words) {
if (s.equals(lowerCaseWord))
return true;
}
return false;
}
public int size() {
return words.length;
}
public String get(int index) {
return words[index];
}
}
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;
public class SortedWordList extends WordList {
public String [] words;
public SortedWordList() {
URL listLocation = SortedWordList.class.getClassLoader().getResource("unsorted_words.txt");
Scanner in = null;
try {
in = new Scanner( listLocation.openStream() );
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ArrayList<String> wordList = new ArrayList<String>();
while (in.hasNextLine())
wordList.add(in.nextLine());
words = wordList.toArray(new String[] {});
in.close();
}
//public boolean contains(String lowerCaseWord) {
//}
static int binarySearch(String[] A, int loIndex, int hiIndex, String value) {
if (loIndex > hiIndex) {
return -1;
}
else {
int middle = (loIndex + hiIndex) / 2;
if (value == A[middle])
return middle;
else if ((A[middle].compareTo(value)) > 0)
return binarySearch(A, loIndex, middle - 1, value);
else // value must be > A[middle]
return binarySearch(A, middle + 1, hiIndex, value);
}
} // end binarySearch()
static int quicksortStep(String[] A, int lo, int hi) {
String pivot = A[lo]; // Get the pivot value.
while (hi > lo) {
while (hi > lo && ((pivot.compareTo(A[hi])) <= 0)) {
hi--;
}
if (hi == lo)
break;
A[lo] = A[hi];
lo++;
while (hi > lo && ((pivot.compareTo(A[lo])) >= 0)) {
lo++;
}
if (hi == lo)
break;
A[hi] = A[lo];
hi--;
} // end while
A[lo] = pivot;
return lo;
} // end QuicksortStep
static void quicksort(String[] A, int lo, int hi) {
if (hi <= lo) {
return;
}
else {
int pivotPosition = quicksortStep(A, lo, hi);
quicksort(A, lo, pivotPosition - 1);
quicksort(A, pivotPosition + 1, hi);
}
}
}
首先,您需要在哪里打電話給quicksort? 只是通過類的SortedWordList名稱進行猜測,在我看來,您需要在SortedWordList的構造函數中調用此quicksort方法。 現在,構造函數似乎正在加載未排序的單詞列表。
如果那不能解決問題,那么我還要再次查看SortedWordList類中的quicksortStep方法。 這個quicksortStep方法基本上是分區方法。 (例如,請參見https://www.programcreek.com/2012/11/quicksort-array-in-java/ )。 我將嘗試在quicksortStep方法中遵循與上面鏈接中的分區方法相同的代碼結構,然后將其修改為字符串,直到類似:
public static int partition(String[] arr, int start, int end){
String pivot = arr[end];
for(int i=start; i<end; i++){
if((arr[i].compareTo(pivot)) < 0){
String temp= arr[start];
arr[start]=arr[i];
arr[i]=temp;
start++;
}
}
String temp = arr[start];
arr[start] = pivot;
arr[end] = temp;
return start;
}
隨意重命名方法/變量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.