I. Overview
Recently, I was looking at some interview questions and found that many interviews require handwritten quick sorting. I checked some blogs and found that what others wrote was not particularly clear and difficult to remember. So in order to better grasp the algorithm, I will write this article In, record your own learning process, you will learn the quick sort algorithm and how to use Java to achieve quick sort.
Quick sort is a sorting algorithm based on divide and conquer, in which:
1. Divide the array into two sub-arrays by selecting a central element from the array. When dividing the array, place the elements smaller than the central element in the left sub-array, and place the elements larger than the central element in the right sub-array.
2. The left sub-array and the right sub-array are also divided in the same way. This process continues until each sub-array contains one element.
3. Finally, the elements are grouped together to form a sorted array.
Pivot element (pivot element): Some places are translated as: pivot element, primitive, reference element, I call it the center element here
Second, the working principle of the quick sort algorithm
1. Choose the central element
Select the center element at different positions, and there are different variants of quick sort. For example, you can choose: the first element, the last element, and the median value of the three elements at the left, right, and center positions as the center element. Here, We will select the last element of the array as the center element.
2. Rearrange the array
Now rearrange the array so that the one smaller than the center element is placed on the left, and the one larger than the center element is placed on the right.
The method to rearrange the array is as follows:
1. The pointer is fixed on the center element, and the center element is compared with the element starting from the first index.
2. If the element is larger than the central element, set a second pointer for the element.
3. Now compare the center element with other elements. If the arrived element is smaller than the center element, swap the smaller element with the larger element found last time.
4. Similarly, repeat the process to set the next larger element as the second pointer, and swap it with another smaller element.
5. The process continues until the penultimate element is reached.
6. Finally, the center element is swapped with the element pointed to by the second pointer.
3. Divide the sub-array
Once again, the center element is selected for the left and right subparts, and step 2 is repeated. The subarray is divided until there is only one element in each subarray. At this point, the array has been sorted in ascending order by the quick sort algorithm.
4. Quick sort visual illustrations
You can use the following illustration to understand how the quick sort algorithm works.
Three, quick sort algorithm pseudo code
1. Pseudo code description
quickSort(array, leftmostIndex, rightmostIndex)
if (leftmostIndex < rightmostIndex)
pivotIndex <- partition(array,leftmostIndex, rightmostIndex)
quickSort(array, leftmostIndex, pivotIndex - 1)
quickSort(array, pivotIndex, rightmostIndex)
partition(array, leftmostIndex, rightmostIndex)
set rightmostIndex as pivotIndex
storeIndex <- leftmostIndex - 1
for i <- leftmostIndex + 1 to rightmostIndex
if element[i] < pivotElement
swap element[i] and element[storeIndex]
storeIndex++
swap pivotElement and element[storeIndex+1]
return storeIndex + 1
Four, Java implements quick sort
The code for implementing quick sort in Java is as follows:
public class QuickSort {
public static int partition(int[] array, int low, int high) {
// 取最后一个元素作为中心元素
int pivot = array[high];
// 定义指向比中心元素大的指针,首先指向第一个元素
int pointer = low;
// 遍历数组中的所有元素,将比中心元素大的放在右边,比中心元素小的放在左边
for (int i = low; i < high; i++) {
if (array[i] <= pivot) {
// 将比中心元素小的元素和指针指向的元素交换位置
// 如果第一个元素比中心元素小,这里就是自己和自己交换位置,指针和索引都向下一位移动
// 如果元素比中心元素大,索引向下移动,指针指向这个较大的元素,直到找到比中心元素小的元素,并交换位置,指针向下移动
int temp = array[i];
array[i] = array[pointer];
array[pointer] = temp;
pointer++;
}
System.out.println(Arrays.toString(array));
}
// 将中心元素和指针指向的元素交换位置
int temp = array[pointer ];
array[pointer] = array[high];
array[high] = temp;
return pointer;
}
public static void quickSort(int[] array, int low, int high) {
if (low < high) {
// 获取划分子数组的位置
int position = partition(array, low, high);
// 左子数组递归调用
quickSort(array, low, position -1);
// 右子数组递归调用
quickSort(array, position + 1, high);
}
}
public static void main(String[] args) {
int[] array = {6,72,113,11,23};
quickSort(array, 0, array.length -1);
System.out.println("排序后的结果");
System.out.println(Arrays.toString(array));
}
}
The result of the sorting process is as follows:
[6, 72, 113, 11, 23]
[6, 72, 113, 11, 23]
[6, 72, 113, 11, 23]
[6, 11, 113, 72, 23]
[6, 11, 23, 72, 113]
[6, 11, 23, 72, 113]
排序后的结果
[6, 11, 23, 72, 113]
From this sorting result, we can know the whole sorting process.
Five, the complexity of quick sort
time complexity | O means |
---|---|
the best | O(n * log n) |
Worst | O(n * n) |
average | O(n * log n) |
Space complexity | O(log n) |
stability | Unstable |
1. Time complexity
- Worst case complexity [Big-O]:
Occurs when the selected center element is the largest or smallest element. This situation causes the center element to be at the end of the sorted array. One sub-array is always empty, and the other sub-array contains elements. Therefore, only in this sub-array Call quicksort on the above, the quick sort algorithm has better performance for scattered data.
- best case complexity [Big-O]:
This happens when the center element is always the middle element or close to the middle element.
- average complexity [Big-O]:
Occurs when the above conditions do not occur.
2. Space complexity
The space complexity of quicksort is O(log n).
Six, the application of quick sort
Use the Quicksort algorithm in the following situations
- Programming language is suitable for recursion
- Time complexity is important
- Space complexity is important
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。