分治算法三(随机化快速排序)
来源:互联网 发布:旋转矩阵计算器 编辑:程序博客网 时间:2024/05/22 17:39
1、快速排序对于输入数据的顺序比较敏感。主要在于每次划分对主元的选择。基本的快速排序选取第一个元素作为主元。这样在数组已经有序的情况下,每次划分将得到最坏的结果。这个时候,时间复杂度将会退化到O(n^2)。
2、一种比较常见的优化方法是随机化算法,即随机选取一个元素作为主元。这种情况下虽然最坏情况仍然是O(n^2),但最坏情况不再依赖于输入数据,而是取决于随机函数随机数的选取。实际上,随机化快速排序得到理论最坏情况的可能性仅为1/(2^n)。所以随机化快速排序可以对于绝大多数输入数据达到O(n*logn)的期望时间复杂度。
3、随机化快速排序的唯一缺点在于,一旦输入数据中有很多的相同数据,随机化的效果将直接减弱。对于极限情况,即对于n个相同的数排序,随机化快速排序的时间复杂度将毫无疑问的降低到O(n^2)。解决方法是用一种方法进行扫描,使没有交换的情况下主元保留在原位置。
/* file: random_quick_sort*//* 1、find the pivot of the Array by using random algorithm*//* 2、divide the Array into two subarrays*//* 3、conquer the two subarrays*//* 4、the Array is sorted,when conquer ended*/#include<stdio.h>#include <stdlib.h>/*===================================== swap:swap two numbers a and b=======================================*/inline void swap(int* a,int* b){int tmp;tmp = *a;*a = *b;*b = tmp;}/*===================================== Partition:Partition the Array from startto end into two subarrays.Return the pivot of the elementArray[start]=======================================*/int Partition(int* Array, int start, int end){//choose Array[start] as the pivot element //divide the array into two subarrays.//left of the Array's elements <= Array[start]//right of the array's elements > Array[start]int pivot = start,j;for(j = start + 1; j <= end; j++)if(Array[j] <= Array[start]){pivot++;swap(&Array[pivot], &Array[j]);}swap(&Array[start], &Array[pivot]);return pivot;}/*===================================== RandomPartition:using random algorithm.partition the Array into two subarrays.Return the pivot of the element Array[start] which is already swaped=======================================*/int RandomPartition(int* Array, int start, int end){//generate the random index of the pivotint random_index = start + rand() % (end - start + 1);//swap the pivot with the random_index elementswap(&Array[start], &Array[random_index]);//now random_index element act as the start elementreturn Partition(Array,start,end);}/*===================================== RandomQuickSort:Sort the Array using QuickSort algorithm.By partition an Array into two subarrays and then conquer the two subarrays.=======================================*/void RandomQuickSort(int* Array, int start,int end){int pivot;if(start < end){//find the pivot of the arraypivot = RandomPartition(Array, start, end);//conquer the left subarrayRandomQuickSort(Array, start, pivot - 1);//conquer the right subarrayRandomQuickSort(Array, pivot + 1, end);}}void main(){int n,i;printf("Please input the length of Array<0:exit>:\n");while(scanf("%d",&n),n){//create an arrays of length nint* Array = new int[n];//get the input integersprintf("Please input %d integers:\n",n);for(i = 0; i < n; i++)scanf("%d",&Array[i]);//use QuickSort algorithomRandomQuickSort(Array,0,n-1);//print the sorted arrayprintf("Sorted Array:\n");for(i = 0; i < n; i++)printf("%d ",Array[i]);system("pause");system("cls");//delete the arraydelete[] Array;printf("Please input the length of Array<0:exit>:\n");}}
- 分治算法三(随机化快速排序)
- 随机化快速排序算法
- 随机化快速排序算法
- 快速排序(分治法实现的随机化版本)
- 快速排序的随机化算法
- 快速排序的随机化算法
- 快速排序算法 随机化分析
- 快速排序(分治算法)
- 分治算法;随机化划分函数;快速排序;线性时间选择第K小元素;快速排序平均时间复杂度nlgn;
- 排序算法--快速排序(分治法)
- 挖掘算法中的数据结构(三):O(n*logn)排序算法之 快速排序(随机化、二路、三路排序) 及衍生算法
- 算法导论(一):快速排序与随机化快排
- 分治算法-快速排序
- 分治算法----快速排序
- 快速排序-分治算法
- 分治算法---快速排序
- 分治算法 快速排序
- 快速排序(随机化版本)
- java.lang.IllegalArgumentException: node to traverse cannot be null!
- PHP上传文件功能实现
- 基带芯片、射频芯片相关
- tomcat document
- linux内核学习1 start_kernel()
- 分治算法三(随机化快速排序)
- Rolling Hash(Rabin-Karp 算法)匹配字符串与anagram串
- mtk6572平台camera video 报错问题
- ARM公司相关讲解
- mfc,WM_CTLCOLOR,WM_PAINT
- java 字节流与字符流的区别
- 设置(更改)Mysql 自增ID的起始值
- poj-1191- 棋盘分割dp
- HTTP协议详解