推免複習之數據結構與算法 快速排序

本來吧,快排我已經寫了一兩篇文章記錄了,但是我有一點強迫症,我想讓這個系列能包括儘量全面的算法和知識點,所以我又重新寫了一篇文章放在這裏,其實就是籤個到hhh

具體的教程還是看別人的文章把,這些排序算法的講解都要做很多圖,我沒這個耐心https://www.cnblogs.com/lifexy/p/7597276.html

#include<iostream>
#include<vector>
using namespace std;

void QuickSort(vector<int> &arr,int left,int right)
{
	if (left >= right)
	{
		return;
	}
	int i = left, j = right;
	int temp = arr[left];
	while (i<j)
	{
		while (i<j && arr[j]>temp)  //找比temp小的元素
		{
			j--;
		}
		if (i < j)
		{
			arr[i] = arr[j];
			i++;
		}
		while (i < j && arr[i] < temp)  //找比temp大的元素
		{
			i++;
		}
		if (i < j)
		{
			arr[j] = arr[i];
			j--;
		}
	}
	arr[i] = temp;
	QuickSort(arr,left,i-1);    //分治
	QuickSort(arr, i+1, right);
}

int main()
{
	vector<int> Input = {1,6,3,9,7,8,4,2,0,74,93,12,16,4,3,64,25,20};
	int size = Input.size();
	int left = 0;
	int right = size - 1;
	QuickSort(Input,left,right);
	for (int i = 0; i < size; i++)
	{
		cout << Input[i] << "  ";
	}
	system("pause");
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章