I have a quicksort that works, and a selection sort that also works- I want to sort an int[] of random ints with quicksort up until the array.length = 1 and then for that last element call selection sort. I know I need to have a conditional that checks the array.length and when length = 1 then, return selectionSort() Im not sure how to structure it in the recursive calls within quickSort(). Here are my two sorting methods:
QuickSort:
public void quickSort(int array[], int start, int end) {
int i = start; // index val of left-to-right scan
int k = end; // index val of right-to-left scan
if (end - start >= 1){
PIVOT = array[start];
while (k > i){
while (array[i] <= PIVOT && i <= end && k > i)
i++;
while (array[k] > PIVOT && k >= start && k >= i)
k--;
if (k > i)
swap(array, i, k);
}
swap(array, start, k);
quickSort(array, start, k - 1);
System.out.println(k);
quickSort(array, k + 1, end);
}
return;
}
Selection Sort:
public void selectionSort(int[] nums) {
//nums[0] = array[0];
System.out.println(nums.length);
for (int i = 0; i < (nums.length - 1); i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] > nums[j]) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}
}
You’re already almost doing it. With this line,
end - startyou’re getting the length. If that length is less than yourSelectionSortThreshold, run insertion sort only on the current length. This means modifying yourselectionSortroutine to accept astartandendparameter so that you can selection sort only a small portion of the array.