I'm trying to make my QuickSort faster than it is and I have got no more ideas about how to make it more efficient for all types of arrays but mostly very big arrays. It uses random to create the Pivot and it uses InsertionSort when the array is less than 15 elements. What do you think guys? I appreciate for any help here to make the code run faster.
public class QuickSort
private static Random rand = new Random();
public void sort(int[] v){
QuickSort(v, 0, v.length-1);
}
private void QuickSort (int[] v, int first, int last) {
if (first >= last)
return;
else {
if (last - first < 15) {
InsertionSort(v, first, last);
return;
}
int[] pivotLoc = partitionArray(v, first, last, makePivot(v,first,last));
QuickSort(v, first, pivotLoc[1]);
QuickSort(v, pivotLoc[0], last);
}
}
private int[] partitionArray (int[] v, int first, int last, int pivot) {
while(last => first) {
while(v[first] < pivot) first++;
while(v[last] > pivot) last--;
if (first > last) break;
swap(v, first, last);
first++;
last--;
}
return new int[] {first, last};
}
private void swap(int[] v, int first, int last) {
int temp = v[first];
v[first] = v[last];
v[last] = temp;
}
public void InsertionSort(int[] v, int first, int last) {
int temp;
for (int i=first + 1; i <= last; i++) {
int j = i;
while (j > 0 && v[j-1] > (v[j]) ) {
temp = v[j];
v[j] = v[j-1];
v[j-1] = temp;
j--;
}
}
}
private int makePivot (int[] v, int first, int last){
return v[rand.nextInt(last-first+1)+first];
}
}
{
afterpublic class QuickSort
. (3) There is no=>
in Java. \$\endgroup\$