I have extended my codes with having an additional insertion sort algorithm to be chosen by giving the boolean value insertionSort either true or false when constructing a class instance. But when I execute my codes, then i get Stackoverflow error. The codes are as follows:
import java.util.Random;
/**
* Write a description of class QuickSort1 here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class QuickSort1 implements IntSorter
{
private int[] v;
private Random randomGenerator;
private boolean insertionSort;
private InsertionSort insertionSorter;
public QuickSort1(boolean useInsertionSort)
{
randomGenerator = new Random();
insertionSort = useInsertionSort;
if(insertionSort)
insertionSorter = new InsertionSort();
}
public void sort(int[] v)
{
this.v = v;
if(this.v.length > 0) {
quickSort(this.v, 0, this.v.length-1);
}
else {
return;
}
}
private void quickSort(int[] v, int first, int last)
{
final int startInsertion = 20;
int First = first;
int Last = last;
int pivot = v[randomGenerator.nextInt(v.length)];
if(Last-First<2 && !insertionSort)
return;
else if(insertionSort) {
if(pivot >= Last-startInsertion)
v = insertionSorter.sort(v);
}
else {
while(First <= Last) {
while(v[First] < pivot) {
First++;
}
while(v[Last] > pivot) {
Last--;
if(Last==0)
break;
}
if(First<=Last) {
int temp = v[First];
v[First] = v[Last];
v[Last] = temp;
First++;
Last--;
}
}
if(first < Last
)
quickSort(v, first, Last);
if(First < last)
quickSort(v, First, last);
}
}
public boolean getInfo()
{
return insertionSort;
}
}
For the alternative insertion algorithm I have implemented a simple class with the following codes:
/**
* Write a description of class InsertionSort here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class InsertionSort
{
int[] v;
/**
* Constructor for objects of class InsertionSort
*/
public InsertionSort()
{
}
public int[] sort(int[] v)
{
for(int i=1;i<v.length;i++) {
int temp = v[i];
int j;
for(j=i-1;j>=0 && temp<v[j];j--) {
v[j+1] = v[j];
}
v[j+1] = temp;
}
return v;
}
}
The error messages I now get for executing this algorithm for arrays with the size of 10.000-100.000 elements are the following:
java.lang.StackOverflowError
at java.util.Random.nextInt(Random.java:307)
at QuickSort1.quickSort(QuickSort1.java:40)
at QuickSort1.quickSort(QuickSort1.java:68)
The error at line 68 gets reapeted in the terminal a lot of times and it indicates on the first recursive call of the quickSort method. The line 40 indicates on the line where the pivot element gets decided by Java's randomizing int generator.
I have a strong feeling that this algorithm perhaps cannot be better than it is now since for bigger number of elements, the stack will get empty during the execution for great numbers of elements to be sorted and therefore I perhaps get that StackOverflowError. But perhaps you have another opinion about this problem?
Thanks in advance for helping me out with this :D