public static void swap(int[] array, int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
public static int partition(int[] array, int left, int right) {
//设定基准值
int sign = left;
//移动index,确定分区界限的位置,index是之后与基准值交换的值
int index = sign + 1;
for (int i = index; i <= right; i++) {
if (array[sign] > array[i]) {
swap(array, i, index);
index++;
}
}
swap(array, sign, index -1);
return index - 1;
}
public static int[] sort(int[] array, int left, int right) {
if (left < right) {
int partitionIndex = partition(array, left, right);
sort(array, left, partitionIndex - 1);
sort(array, partitionIndex + 1, right);
}
return array;
}
【算法学习12.21】快速排序算法
最新推荐文章于 2024-09-03 20:45:39 发布