[Sorting Algorithm] Quick Sort

Reference: "Comic Algorithm - Xiao Hui's Algorithmic Journey"

Table of contents

1. The idea of ​​quick sort algorithm

2. The process of quick sort method

3. Selection of benchmark elements

4. Exchange of elements (only the unilateral circulation method is introduced here)

5. Quick sort code


1. The idea of ​​quick sort algorithm

The quick sort algorithm uses the idea of ​​​​divide and conquer, selects a reference element in each round, and moves other elements larger than it to one side of the sequence, thereby splitting the sequence into two parts.

2. The process of quick sort method

 Each round of comparison and exchange needs to traverse all the elements of the array, and the time complexity is O(n) . How many rounds does this traversal take? If the number of elements is n, logn rounds are required on average , so the overall average time complexity of the quick sort algorithm is O(nlogn) .

3. Selection of benchmark elements

The easiest way to select the base element is to select the first element of the sequence. Drunk choice is fine in most cases. But when a sequence is in reverse order, each round can only determine the position of the reference element. The time complexity at this point (worst case) is O(n^2). In order to avoid this situation, an element can be randomly selected as the reference element, and the reference element and the first element of the array can be exchanged.

4. Exchange of elements (only the unilateral circulation method is introduced here)

The original sequence is as shown above, and it is sorted from small to large. First select a reference element pivot. At the same time, set a mark pointer to point to the starting position of the array, and this mark pointer represents the boundary of the area smaller than the reference element .

Start traversing the array from the next position of the reference element, if the traversed element is greater than the reference element, continue to traverse backwards. If the traversed element is smaller than the reference element, you need to do two things: first, move the mark pointer to the right by 1 bit, because the boundary of the area smaller than the pivot is increased by 1; second, let the newly traversed element and the mark pointer The elements at the location are swapped because the most recently traversed element belongs to an area smaller than the pivot.

5. Quick sort code

using namespace std;
#include<iostream>
#include<string>
int partition(int arr[], int startIndex, int endIndex) {
	//取第一个位置的元素作为基元素
	int pivot = arr[startIndex];
	int mark = startIndex;

	for (int i = startIndex + 1; i <= endIndex; i++) {
		if (arr[i] < pivot) {
			mark = mark + 1;
			int p = arr[mark];
			arr[mark] = arr[i];
			arr[i] = p;
		}
	}
	arr[startIndex] = arr[mark];
	arr[mark] = pivot;
	return mark;
}

void quickSort(int arr[], int startIndex, int endIndex) {
	//递归结束条件:startIndex>= endIndex
	if (startIndex >= endIndex) {
		return;
	}
	//得到基准元素的位置
	int pivotIndex = partition(arr, startIndex,endIndex);
	quickSort(arr, startIndex, pivotIndex-1);
	quickSort(arr, pivotIndex + 1, endIndex);
}

int main()
{
	int arr[] = { 4, 4, 6, 5, 3, 2, 8, 1 };
	int endIndex = sizeof(arr) / sizeof(arr[0])-1;
	quickSort(arr, 0, endIndex);
	for (int i = 0; i <= endIndex; i++) {
		cout << arr[i] << endl;
	}
	system("pause");
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45922730/article/details/129307254