QuickSort是分而治之的算法。在分而治之算法设计范式中,我们将问题递归地划分为子问题,然后解决子问题,最后结合解决方案以找到最终结果。
将问题划分为子问题时要记住的一件事是,子问题的结构不会像原始问题一样发生变化。
分而治之算法有3个步骤:
- 划分:将问题分解为子问题
- 征服:以递归方式解决子问题
- 合并:合并解决方案以获得最终结果
有多种基于分而治之范式的算法。快速排序和合并排序就是其中之一。
尽管QuickSort在最坏情况下的时间复杂度为O(n2),这比许多其他排序算法(如“合并排序”和“堆排序”)要高,但实践中QuickSort的速度更快,因为它的内部循环可以在大多数架构上以及大多数架构中高效实现真实数据。
让我们谈谈快速排序算法的实现。快速排序算法采用枢轴元素,并围绕枢轴元素对数组进行分区。Quicksot有多种变体,取决于您选择枢轴元素的方式。有多种选择枢轴元素的方法:
- 选择第一个元素
- 选择最后一个元素
- 选择一个随机元素
- 挑选中位数元素
接下来要了解的重要一点是Quick sort算法中的partition()函数。分区功能可获取一个枢轴元素,将其放置在右侧位置,将所有小于枢轴元素的元素向左移动,并将所有比枢轴元素大的元素向右移动。Quicksort需要花费线性时间。
然后将数组从数据透视元素中分为两部分(即,小于数据透视元素的元素和大于数据透视元素的元素),并使用Quicksort算法对两个数组进行递归排序。
现在,我们了解了QuickSort算法的工作原理。让我们了解如何在Java中实现Quicksort算法。
快速排序功能:
/ * Quicksort Function需要使用最低和最高索引对数组进行排序* /
void sort(int arr[], int lowIndex, int highIndex) {
// Until lowIndex = highIndex
if (lowIndex < highIndex) {
// partitioning of the array
int p = partition(arr, lowIndex, highIndex);
// Recursively sort elements before & after partition
sort(arr, lowIndex, p - 1);
sort(arr, p + 1, highIndex);
}
}
分区码
在分区代码中,我们将最后一个元素选为枢轴元素。我们遍历整个数组(即在我们的例子中使用变量j)。我们跟踪数组中的最后一个最小元素(即,在本例中使用变量i)。如果找到小于枢轴的任何元素,则将交换当前元素a [j]与arr [i]移动,否则我们继续遍历。
int partition(int arr[], int lowIndex, int highIndex) {
// Making the last element as pivot
int pivot = arr[highIndex];
// Using i to keep track of smaller elements from pivot
int i = (lowIndex - 1);
for (int j = lowIndex; j < highIndex; j++) {
// If current element is smaller than or equal to pivot
if (arr[j] <= pivot) {
i++; // increment i
// swap ith element with jth element
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
// moving pivot at its correct position
int temp = arr[i + 1];
arr[i + 1] = arr[highIndex];
arr[highIndex] = temp;
return i + 1;
}
现在您已经了解了Quicksort和分区功能,让我们快速看一下完整的代码
QuickSort Java代码
import java.util.Arrays;
class QuickSortDemo {
// Partition Method
int partition(int arr[], int lowIndex, int highIndex) {
int pivot = arr[highIndex];
System.out.println(pivot);
int i = (lowIndex - 1);
for (int j = lowIndex; j < highIndex; j++) {
if (arr[j] <= pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
System.out.println(Arrays.toString(arr));
}
int temp = arr[i + 1];
arr[i + 1] = arr[highIndex];
arr[highIndex] = temp;
return i + 1;
}
void sort(int arr[], int lowIndex, int highIndex) {
if (lowIndex < highIndex) {
int pi = partition(arr, lowIndex, highIndex);
sort(arr, lowIndex, pi - 1);
sort(arr, pi + 1, highIndex);
}
}
static void printArray(int arr[]) {
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
public static void main(String args[]) {
int arr[] = { 15, 85, 35, 95, 45, 65, 75 };
int n = arr.length;
QuickSortDemo ob = new QuickSortDemo();
ob.sort(arr, 0, n - 1);
System.out.println("sorted array");
printArray(arr);
}
}
输出:
75
[15, 85, 35, 95, 45, 65, 75]
[15, 85, 35, 95, 45, 65, 75]
[15, 35, 85, 95, 45, 65, 75]
[15, 35, 85, 95, 45, 65, 75]
[15, 35, 45, 95, 85, 65, 75]
[15, 35, 45, 65, 85, 95, 75]
65
[15, 35, 45, 65, 75, 95, 85]
[15, 35, 45, 65, 75, 95, 85]
[15, 35, 45, 65, 75, 95, 85]
45
[15, 35, 45, 65, 75, 95, 85]
[15, 35, 45, 65, 75, 95, 85]
35
[15, 35, 45, 65, 75, 95, 85]
85
[15, 35, 45, 65, 75, 95, 85]
sorted array
15 35 45 65 75 85 95