快速排序算法

来源:互联网 发布:大数据架构师技能 编辑:程序博客网 时间:2024/05/10 21:57
快速排序是C.R.A.Hoare于1962年提出的一种划分交换排序。它采用了一种分治的策略,通常称其为分治法(Divide-and-ConquerMethod)。
该方法的基本思想是:
1.先从数列中取出一个数作为基准数。
2.分区过程,将比这个数大的数全放到它的右边,小于或等于它的数全放到它的左边。
3.再对左右区间重复第二步,直到各区间只有一个数。

挖坑填数+分治法:


对挖坑填数进行总结:

1.i =L; j = R; 将基准数挖出形成第一个坑a[i]

2.j--由后向前找比它小的数,找到后挖出此数填前一个坑a[i]中。

3.i++由前向后找比它大的数,找到后也挖出此数填到前一个坑a[j]中。

4.再重复执行23二步,直到i==j,将基准数填入a[i]中。


取序列的第一个或者最后一个元素作为基准

以下是代码:

递归:

// quick_sort.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include "iostream"using namespace std;void quick_sort(int s[], int L, int R){if(L < R){       int i = L, j = R, x = s[L];   while (i < j)   {   while( i < j && s[j] >= x)  //从右向左找到第一个小于x的数   j--;   if(i < j)   s[i++] = s[j];   while(i < j && s[i] < x)    //从左到右找到第一个大于等于x的数   i++;   if(i < j)   s[j--] = s[i];   }   s[i] = x;   quick_sort(s, L, i - 1);   quick_sort(s, i+1, R);}}int _tmain(int argc, _TCHAR* argv[]){/*int A[] = {72, 6, 57, 88, 60, 42, 83, 73, 48, 85};*/int A[] = {72, 6};quick_sort(A, 0, 1);for (int i = 0; i <= sizeof(A)/sizeof(int) - 1; i++ ){cout << A[i] << endl;}system("pause");return 0;}

c++:

// quick_sort.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include "iostream"#include <stack>using namespace std;struct Num{int low,high;Num(int low = 0, int high = 0){this->low = low;this->high = high;}};void sort(int val[], int, int);int _tmain(int argc, _TCHAR* argv[]){/*int A[] = {72, 6, 57, 88, 60, 42, 83, 73, 48, 85};*/int A[] = {6, 72};sort(A, 0, 1);for (int i = 0; i <= sizeof(A)/sizeof(int) - 1; i++ ){cout << A[i] << endl;}system("pause");return 0;}void sort(int arr[], int begin, int end){stack<Num> myStack;myStack.push(Num(begin, end));while(!myStack.empty()){int i = myStack.top().low;int j = myStack.top().high;int b = i;int e = j;myStack.pop();if (i >= j)    continue;int key = arr[i];while (i < j){while( i < j && arr[j] >= key)j--;if(i < j)arr[i++] = arr[j];while(i < j && arr[i] <= key)i++;if(i < j)arr[j--] = arr[i];}arr[i] = key;myStack.push(Num(b,i - 1));myStack.push(Num(i + 1, e));}}


另一代码:

// quick_sort.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include "iostream"#include <stack>using namespace std;void sort(int arr[], int, int);int partition(int arr[], int, int);int partition(int arr[], int L, int R){int i = L, j = R;int Key = arr[L];while( i < j ){while(i < j && arr[j] >= Key)j--;if (i < j) arr[i++] = arr[j];while (i < j && arr[i] < Key)i--;if (i < j) arr[j--] = arr[i];}arr[i] = Key;return i;}void sort(int arr[], int L, int R){if (L < R){int p = partition(arr, L, R);sort(arr, L, p-1);sort(arr,p+1, R);}}int _tmain(int argc, _TCHAR* argv[]){/*int A[] = {72, 6, 57, 88, 60, 42, 83, 73, 48, 85};*/int A[] = {6, 72, 32};sort(A, 0, 2);for (int i = 0; i <= sizeof(A)/sizeof(int) - 1; i++ ){cout << A[i] << endl;}system("pause");return 0;}


取中间作为基准:

// quick_sort.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include "iostream"#include <stack>using namespace std;void sort(int arr[], int, int);int partition(int arr[], int, int);void change(int arr[], int L, int R);void change(int arr[], int L, int R){int temp;temp = arr[L];arr[L] = arr[R];arr[R] = temp;}int partition(int arr[], int L, int R){int i = L, j = R;int mid = (i+j)/2;change(arr, L, mid);int Key = arr[L];while( i < j ){while(i < j && arr[j] >= Key)j--;if (i < j) arr[i++] = arr[j];while (i < j && arr[i] < Key)i++;if (i < j) arr[j--] = arr[i];}arr[i] = Key;return i;}void sort(int arr[], int L, int R){if (L < R){int p = partition(arr, L, R);sort(arr, L, p-1);sort(arr,p+1, R);}}int _tmain(int argc, _TCHAR* argv[]){int A[] = {72, 6, 57, 88, 60, 42, 83, 73, 48, 85};/*int A[] = {6, 72, 32};*/sort(A, 0, 9);for (int i = 0; i <= sizeof(A)/sizeof(int) - 1; i++ ){cout << A[i] << endl;}system("pause");return 0;}



0 0