SWUST OJ 之 0413 Quick Sort

题目

Quicksort is a well-known sorting algorithm developed by C. A. R. Hoare that, on average, makes Θ(n log n) comparisons to sort n items. However, in the worst case, it makes Θ(n2) comparisons. Typically, quicksort is significantly faster in practice than other Θ(n log n) algorithms, because its inner loop can be efficiently implemented on most architectures, and in most real-world data it is possible to make design choices which minimize the possibility of requiring quadratic time. Quicksort sorts by employing a divide and conquer strategy to divide a list into two sub-lists. The steps are: 1. Pick an element, called a pivot, from the list. 2. Reorder the list so that all elements which are less than the pivot come before the pivot and so that all elements greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation. 3. Recursively sort the sub-list of lesser elements and the sub-list of greater elements. The base case of the recursion are lists of size zero or one, which are always sorted. The algorithm always terminates because it puts at least one element in its final place on each iteration (the loop invariant). Quicksort in action on a list of random numbers. The horizontal lines are pivot values. Write a program to sort ascending int number by QuickSort ,n less than 50000.

输入

two lows, the first low is numbers , less and equal than 50000. the second low is a set integer numbers

输出

a set integer numbers of sort ascending

样例输入

10
4 2 1 5 7 6 9 8 0 3

样例输出

0 1 2 3 4 5 6 7 8 9

分析

快速排序大家都会用,在头文件下调用sort(数组名,长度,升降序)函数即可;
这道题也可以这样AC了它,但是这道题的用意就是让我们明白快速排序的内部机制是怎么样的,所以我们下面来分析一下
(不懂得可参考:sort(函数详解):https://www.cnblogs.com,快速排序算法:https://baike.baidu.com)

现在我们来思考一下一趟快速排序的算法:
1)设置两个变量i、j,排序开始的时候:i = 0,j = n - 1;
2)以第一个数组元素作为关键数据,赋值给key,即key=arr[0];
3)从 j 由后开始向前搜索( j- - ),找到第一个小于 key 的值arr[ j ],swap(arr[ i ],arr[ j ]),即将arr[ i ]和arr[ j ]互换;
4)从 i 由前开始向后搜索( i++),找到第一个大于key的arr[ i ],swap(arr[ i ],arr[ j ]);
5)重复第3、4步,直到 i = j;
这样,我们就有一个思路了,现在的数组以 i 为界限,左边的都小于 key,右边的都大于等于 key;
同理我们递归调用函数,让其一直划分下去,直到只剩下一个元素或没有时停止调用,我们的数组就排好了;

这样我们就有了一个清晰的解题思路了;

代码

// test.cpp: 定义控制台应用程序的入口点。
//

#include
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f
using namespace std;

int arr[50005];

void quick_sort(int left,int right)
{
	if (left < right)
	{
		int i = left, j = right, k = arr[left];
		while (i < j)
		{
			while (i < j && arr[j] >= k)            //从右向左,找小于K的数
				j--;
			if (i < j)
				swap(arr[i++], arr[j]);

			while (i < j && arr[i] < k)		//从左向右,找大于K的数
				i++;
			if (i < j)
				swap(arr[i], arr[j--]);
		}
		quick_sort(left, i - 1);			//左右递归调用
		quick_sort(i + 1, right);
	}
}

int main()
{
	int n;
	cin >> n;

	for (int i = 0; i < n; i++)	//输入
	{
		cin >> arr[i];
	}

	quick_sort(0, n - 1);		//快排

	for (int i = 0; i < n; i++)	//输出
	{
		cout << arr[i] << " ";
	}
	cout << endl;

	return 0;
}




 

 

 

 

 

 

 

你可能感兴趣的:(SWUST,OJ题库,算法练习,编程练习)