//
If, like me, the goal is to use the algorithm in some other code (and not to code the algorithm itself), Arrays.sort
(Documentation here) did the job!
//
I keep getting this error :
Exception in thread "main" java.lang.StackOverflowError
at language.LanguageDetection.sort(LanguageDetection.java:140)
at language.LanguageDetection.sort(LanguageDetection.java:141)
at language.LanguageDetection.sort(LanguageDetection.java:141)
And this line :
at language.LanguageDetection.sort(LanguageDetection.java:141)
Fills the rest of the console output.
I am a beginner in java and I am trying to implement a quick sort algorithm in order to sort a text file containing all dictionary words, so I can then quickly search through it quickly.
I really don't know if this is the way to go or if there would be a more suitable sorting algorithm for this problem, but here is the code :
public static void sort(String[] unsorted, int startIndex, int endIndex)
{
if(endIndex - startIndex <= 0) return; //Ends the recursion when unsorted array is of size 1
String temp;
char pivot = unsorted[endIndex].charAt(0);
int j = startIndex;
for(int i = startIndex; i < endIndex; i++)
{
if(unsorted[i].charAt(0) < pivot)
{
temp = unsorted[j];
unsorted[j] = unsorted[i];
unsorted[i] = temp;
j++;
}
}
temp = unsorted[j];
unsorted[j] = unsorted[endIndex];
unsorted[endIndex] = temp;
sort(unsorted, startIndex, j - 1);
sort(unsorted, j + 1, endIndex);
}
I don't want to have all the corrected version of the code, just some answers about, what I am coding wrong and if there is a better way of doing this.
Thanks in advance.