# 如何实现混合快速排序和插入排序的混合算法？

##### 问题描述投票：0回答：0

``````import java.util.Arrays;
/**
* A class for sorting array elements using quicksort algorithms
*/
public class QuicksortFixedPivotInsertion implements IntSorter{
InsertionSort insert = new InsertionSort();

public int partition(int [] array, int first, int last){
int middle = (first+last)/2;
swap(array, middle, last);
int pivot = array[last];
int i = first - 1;

for (int j = first; j < last; j++){
if (array[j]<pivot){
i++;
swap(array, i, j);
}
}
swap(array, i+1, last);
return i+1;
}

private void quickSort(int [] array, int first, int last){
if (array.length < 20){
insert.sort(array);
}
else if (first  < last){
int pivot = partition(array, first, last);
quickSort(array, first, pivot - 1);
quickSort(array, pivot + 1, last);
}
}

public void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
/**
* sorts the array in ascending order
* @param v the array to be sorted
*/
@Override
public void sort(int [] v){
quickSort(v, 0, v.length - 1);
}
}
``````

java algorithm data-structures quicksort insertion-sort